fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define N 5
  4.  
  5. typedef struct {
  6. char name[3];
  7. int arrival;
  8. int service;
  9. int start;
  10. int finish;
  11. int turnaround;
  12. int waiting;
  13. } Process;
  14.  
  15. int main() {
  16. Process p[N] = {
  17. {"P1", 0, 3},
  18. {"P2", 2, 6},
  19. {"P3", 4, 4},
  20. {"P4", 6, 5},
  21. {"P5", 8, 2}
  22.  
  23. };
  24.  
  25. int i, time = 0;
  26. float sumTAT = 0, sumWait = 0, totalService = 0;
  27.  
  28. printf("\n===== SIMULATION FCFS =====\n");
  29.  
  30. for (i = 0; i < N; i++) {
  31. if (time < p[i].arrival)
  32. time = p[i].arrival;
  33.  
  34. p[i].start = time;
  35. p[i].finish = p[i].start + p[i].service;
  36. p[i].turnaround = p[i].finish - p[i].arrival;
  37. p[i].waiting = p[i].turnaround - p[i].service;
  38.  
  39. time = p[i].finish;
  40.  
  41. sumTAT += p[i].turnaround;
  42. sumWait += p[i].waiting;
  43. totalService += p[i].service;
  44. }
  45.  
  46. float avgTAT = sumTAT / N;
  47. float avgWait = sumWait / N;
  48. float throughput = (float)N / p[N-1].finish;
  49. float cpuUtil = (totalService / p[N-1].finish) * 100;
  50.  
  51. printf("\n%-8s%-10s%-10s%-10s%-10s%-10s%-10s\n",
  52. "Process", "Arrive", "Service", "Start", "Finish", "Tr", "Tw");
  53. printf("---------------------------------------------------------------\n");
  54.  
  55. for (i = 0; i < N; i++) {
  56. printf("%-8s%-10d%-10d%-10d%-10d%-10d%-10d\n",
  57. p[i].name, p[i].arrival, p[i].service,
  58. p[i].start, p[i].finish, p[i].turnaround, p[i].waiting);
  59. }
  60.  
  61. printf("\n--- Résultats globaux ---\n");
  62. printf("Temps moyen de rotation : %.2f\n", avgTAT);
  63. printf("Temps moyen d'attente : %.2f\n", avgWait);
  64. printf("Débit du système : %.2f processus/unité de temps\n", throughput);
  65. printf("Utilisation du CPU : %.2f %%\n", cpuUtil);
  66.  
  67. // ===== Diagramme de Gantt =====
  68. printf("\n--- Diagramme de Gantt ---\n\n");
  69.  
  70. // Première ligne : barres
  71. for (i = 0; i < N; i++) {
  72. printf("|");
  73. for (int j = 0; j < p[i].service; j++)
  74. printf("█");
  75. }
  76. printf("|\n");
  77.  
  78. // Deuxième ligne : noms des processus
  79. for (i = 0; i < N; i++) {
  80. printf("%-3s", p[i].name);
  81. for (int j = 0; j < p[i].service; j++)
  82. printf(" ");
  83. }
  84. printf("\n");
  85.  
  86. // Troisième ligne : temps
  87. int current = 0;
  88. for (i = 0; i < N; i++) {
  89. if (i == 0)
  90. printf("%d", p[i].start);
  91. printf("%*d", p[i].service * 2, p[i].finish);
  92. }
  93. printf("\n");
  94.  
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
===== SIMULATION FCFS =====

Process Arrive    Service   Start     Finish    Tr        Tw        
---------------------------------------------------------------
P1      0         3         0         3         3         0         
P2      2         6         3         9         7         1         
P3      4         4         9         13        9         5         
P4      6         5         13        18        12        7         
P5      8         2         18        20        12        10        

--- Résultats globaux ---
Temps moyen de rotation : 8.60
Temps moyen d'attente   : 4.60
Débit du système        : 0.25 processus/unité de temps
Utilisation du CPU      : 100.00 %

--- Diagramme de Gantt ---

|███|██████|████|█████|██|
P1       P2             P3         P4           P5     
0     3           9      13        18  20