Facebook
From Schedule , 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 157
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. float avgwt, avgtt;
  5. char pname[10][10], c[10][10];
  6. int at[10], wt[10], tt[10], bt[10], n, sum = 0, ttime, j, ss = 0, count = 0, indexar[10], ix = 0, k, ix1, ix2, choice;
  7.  
  8. void gantt_chart() {
  9.     int i;
  10.     printf("\nGANTT CHART\n");
  11.     for (i = 0; i < n; i++) {
  12.         printf("|\t%s\t", pname[i]);
  13.     }
  14.     printf("|\n");
  15.  
  16.     for (i = 0; i < n; i++) {
  17.         printf("%d\t\t", wt[i]);
  18.     }
  19.     printf("%d\n", ss);
  20. }
  21.  
  22. int main() {
  23.     printf("Enter the number of processes:");
  24.     scanf("%d", &n);
  25.  
  26.     printf("Enter the NAME, ARRIVAL TIME, BURST TIME of the process\n");
  27.  
  28.     for (int i = 0; i < n; i++) {
  29.         printf("\nPROCESS NAME: ");
  30.         scanf("%s", pname[i]);
  31.         printf("ARRIVAL TIME: ");
  32.         scanf("%d", &at;[i]);
  33.         printf("BURST TIME: ");
  34.         scanf("%d", &bt;[i]);
  35.     }
  36.  
  37.     for (int i = 0; i < n; i++) {
  38.         for (int j = i + 1; j < n; j++) {
  39.             if (at[i] > at[j]) {
  40.                 int temp = bt[i];
  41.                 bt[i] = bt[j];
  42.                 bt[j] = temp;
  43.  
  44.                 temp = at[i];
  45.                 at[i] = at[j];
  46.                 at[j] = temp;
  47.  
  48.                 char tempStr[10];
  49.                 strcpy(tempStr, pname[i]);
  50.                 strcpy(pname[i], pname[j]);
  51.                 strcpy(pname[j], tempStr);
  52.             }
  53.         }
  54.     }
  55.  
  56.     printf("\nEnter your choice:\n");
  57.     printf("1. FCFS\n2. Non-Preemptive SJF\n3. Preemptive SJF\n4. Round Robin\n5. Non-Preemptive Priority\n");
  58.     scanf("%d", &choice;);
  59.  
  60.     switch (choice) {
  61.         case 1:
  62.             wt[0] = at[0];
  63.             sum = 0;
  64.             ss = bt[0];
  65.             tt[0] = wt[0] + bt[0];
  66.             for (int i = 1; i < n; i++) {
  67.                 wt[i] = wt[i - 1] + bt[i - 1];
  68.                 sum = sum + (wt[i] - at[i]);
  69.                 tt[i] = wt[i] + bt[i];
  70.                 ss = ss + bt[i];
  71.             }
  72.             avgwt = (float)sum / n;
  73.             avgtt = (float)ss / n;
  74.             printf("\nAverage waiting time=%f", avgwt);
  75.             printf("\nAverage turn-around time=%f", avgtt);
  76.             printf("\nGANTT CHART FCFS SCHEDULING\n");
  77.             gantt_chart();
  78.             break;
  79.  
  80.         case 2:
  81.             // Non-Preemptive SJF
  82.             break;
  83.  
  84.         case 3:
  85.             // Preemptive SJF
  86.             break;
  87.  
  88.         case 4:
  89.             // Round Robin
  90.             break;
  91.  
  92.         case 5:
  93.             // Non-Preemptive Priority
  94.             for (int i = 0; i < n; i++) {
  95.                 count = 0;
  96.                 ix = 0;
  97.                 for (int j = i + 1; j < n; j++) {
  98.                     if (at[j] < (bt[i] + at[i])) {
  99.                         count++;
  100.                         indexar[ix] = j;
  101.                         ix++;
  102.                     }
  103.                 }
  104.                 if (count > 1) {
  105.                     for (k = 1; k < count; k++) {
  106.                         ix1 = indexar[k - 1];
  107.                         ix2 = indexar[k];
  108.                         if (bt[ix1] > bt[ix2]) {
  109.                             int temp = bt[ix1];
  110.                             bt[ix1] = bt[ix2];
  111.                             bt[ix2] = temp;
  112.  
  113.                             temp = at[ix1];
  114.                             at[ix1] = at[ix2];
  115.                             at[ix2] = temp;
  116.  
  117.                             char tempStr[10];
  118.                             strcpy(tempStr, pname[ix1]);
  119.                             strcpy(pname[ix1], pname[ix2]);
  120.                             strcpy(pname[ix2], tempStr);
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.             break;
  126.  
  127.         default:
  128.             printf("Invalid choice!\n");
  129.     }
  130.  
  131.     return 0;
  132. }