Edición didáctica en español. El código y sus salidas conservan el original.
Planificación Round Robin en C
Simula turnos de CPU con un quantum y calcula tiempos sin confundir duración y espera.

Round Robin concede a cada proceso listo un máximo de tiempo llamado quantum. Si no termina, vuelve al final de la cola con su tiempo restante. El quantum debe ser positivo para que la simulación avance.
En cada turno, ejecuta el mínimo entre quantum y tiempo restante. Registra cuándo termina cada proceso. El tiempo de retorno es finalización menos llegada; la espera es retorno menos tiempo de CPU, bajo las hipótesis del modelo.
Comprueba si el ejemplo supone que todos llegan en el instante cero. Para llegadas diferentes hay que gestionar la cola de listos y periodos sin trabajo. La simulación didáctica no incluye necesariamente el coste real de cambiar de contexto.
Código C
#include <stdio.h>
#include <stdbool.h>
struct Process {
int id;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};
void calculate_round_robin(struct Process p[], int n, int quantum) {
int current_time = 0;
bool done;
for (int i = 0; i < n; i++) {
p[i].remaining_time = p[i].burst_time;
}
do {
done = true;
for (int i = 0; i < n; i++) {
if (p[i].remaining_time > 0) {
done = false;
if (p[i].remaining_time > quantum) {
current_time += quantum;
p[i].remaining_time -= quantum;
} else {
current_time += p[i].remaining_time;
p[i].waiting_time = current_time - p[i].burst_time;
p[i].remaining_time = 0;
}
}
}
} while (!done);
for (int i = 0; i < n; i++) {
p[i].turnaround_time = p[i].burst_time + p[i].waiting_time;
}
}
int main(void) {
struct Process proc[] = {
{1, 10, 0, 0, 0},
{2, 5, 0, 0, 0},
{3, 8, 0, 0, 0}
};
int n = sizeof(proc) / sizeof(proc[0]);
int time_quantum = 2;
calculate_round_robin(proc, n, time_quantum);
printf("PID\tBurst\tWaiting\tTurnaround\n");
float total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += proc[i].waiting_time;
total_tat += proc[i].turnaround_time;
printf("P%d\t%d\t%d\t%d\n", proc[i].id, proc[i].burst_time, proc[i].waiting_time, proc[i].turnaround_time);
}
printf("\nAverage Waiting Time = %.2f\n", total_wt / n);
printf("Average Turnaround Time = %.2f\n", total_tat / n);
return 0;
}Abrir en el compiladorSalida esperada
PID Burst Waiting Turnaround P1 10 13 23 P2 5 10 15 P3 8 13 21 Average Waiting Time = 12.00 Average Turnaround Time = 19.67
Preguntas frecuentes
¿Por qué el código y la salida siguen en inglés?
Las palabras clave, los identificadores y los textos que verifican las pruebas se conservan para no cambiar el programa. Las explicaciones y los controles están traducidos.