fork download
  1. #include <stdio.h>
  2.  
  3. #define N 5
  4.  
  5. typedef struct {
  6. char name[3];
  7. float arrival;
  8. float service;
  9. float start;
  10. float finish;
  11. float turnaround;
  12. float waiting;
  13. int done;
  14. } Process;
  15.  
  16. int main() {
  17. Process p[N] = {
  18. {"P1", 0.0, 3.0},
  19. {"P2", 2.0, 6.0},
  20. {"P3", 4.0, 4.0},
  21. {"P4", 6.0, 5.0},
  22. {"P5", 8.0, 2.0}
  23. };
  24.  
  25. int completed = 0;
  26. float time = 0.0;
  27. float sumTAT = 0.0, sumWait = 0.0, totalService = 0.0;
  28. int i, shortest;
  29.  
  30. printf("\n===== SIMULATION SJF (non préemptif, float) =====\n");
  31.  
  32. while (completed < N) {
  33. shortest = -1;
  34. float minService = 9999.0;
  35.  
  36. // Find shortest job available at 'time'
  37. for (i = 0; i < N; i++) {
  38. if (!p[i].done && p[i].arrival <= time && p[i].service < minService) {
  39. minService = p[i].service;
  40. shortest = i;
  41. }
  42. }
  43.  
  44. // If no process has arrived yet
  45. if (shortest == -1) {
  46. time += 0.5; // move time forward slightly
  47. continue;
  48. }
  49.  
  50. // Execute selected process
  51. p[shortest].start = time;
  52. p[shortest].finish = p[shortest].start + p[shortest].service;
  53. p[shortest].turnaround = p[shortest].finish - p[shortest].arrival;
  54. p[shortest].waiting = p[shortest].turnaround - p[shortest].service;
  55. p[shortest].done = 1;
  56.  
  57. time = p[shortest].finish;
  58. completed++;
  59.  
  60. sumTAT += p[shortest].turnaround;
  61. sumWait += p[shortest].waiting;
  62. totalService += p[shortest].service;
  63. }
  64.  
  65. float avgTAT = sumTAT / N;
  66. float avgWait = sumWait / N;
  67. float throughput = (float)N / time;
  68. float cpuUtil = (totalService / time) * 100.0;
  69.  
  70. printf("\n%-8s%-10s%-10s%-10s%-10s%-10s%-10s\n",
  71. "Process", "Arrive", "Service", "Start", "Finish", "Tr", "Tw");
  72. printf("---------------------------------------------------------------\n");
  73.  
  74. for (i = 0; i < N; i++) {
  75. printf("%-8s%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f\n",
  76. p[i].name, p[i].arrival, p[i].service,
  77. p[i].start, p[i].finish, p[i].turnaround, p[i].waiting);
  78. }
  79.  
  80. printf("\n--- Résultats globaux ---\n");
  81. printf("Temps moyen de rotation : %.2f\n", avgTAT);
  82. printf("Temps moyen d'attente : %.2f\n", avgWait);
  83. printf("Débit du système : %.2f processus/unité de temps\n", throughput);
  84. printf("Utilisation du CPU : %.2f %%\n", cpuUtil);
  85.  
  86. // ===== Diagramme de Gantt =====
  87. printf("\n--- Diagramme de Gantt ---\n\n");
  88.  
  89. // First line: bars
  90. for (i = 0; i < N; i++) {
  91. printf("|");
  92. int blocks = (int)(p[i].service * 2); // each unit = 0.5
  93. for (int j = 0; j < blocks; j++)
  94. printf("█");
  95. }
  96. printf("|\n");
  97.  
  98. // Second line: process names
  99. for (i = 0; i < N; i++) {
  100. printf("%-3s", p[i].name);
  101. int blocks = (int)(p[i].service * 2);
  102. for (int j = 0; j < blocks; j++)
  103. printf(" ");
  104. }
  105. printf("\n");
  106.  
  107. // Third line: time scale
  108. for (i = 0; i < N; i++) {
  109. if (i == 0)
  110. printf("%.1f", p[i].start);
  111. printf("%*.1f", (int)(p[i].service * 2), p[i].finish);
  112. }
  113. printf("\n");
  114.  
  115. return 0;
  116. }
  117.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
===== SIMULATION SJF (non préemptif, float) =====

Process Arrive    Service   Start     Finish    Tr        Tw        
---------------------------------------------------------------
P1      0.0       3.0       0.0       3.0       3.0       0.0       
P2      2.0       6.0       3.0       9.0       7.0       1.0       
P3      4.0       4.0       11.0      15.0      11.0      7.0       
P4      6.0       5.0       15.0      20.0      14.0      9.0       
P5      8.0       2.0       9.0       11.0      3.0       1.0       

--- Résultats globaux ---
Temps moyen de rotation : 7.60
Temps moyen d'attente   : 3.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.0   3.0         9.0    15.0      20.011.0