Advertisement
coffeebeforecode

master_sceduling.c

Dec 6th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. //#define INT_MAX 2147483647
  6.  
  7. typedef struct job{
  8.     int id;
  9.     int at;
  10.     int bt;
  11.     int st;
  12.     int ct;
  13.     int wt;
  14.     int tat;
  15.     int pr;
  16. } job;
  17.  
  18. void takeInput(job jobs[], int n, int choice, int k){
  19.     int i;
  20.     for(i=0; i < n; i++){
  21.         jobs[i].id = i;
  22.         if (k != 1){
  23.             printf("Enter Arrival Time of Process %d: ", i);
  24.             scanf("%d", &jobs[i].at);
  25.         }
  26.         else{
  27.             jobs[i].at = 0;
  28.         }
  29.         printf("Enter Burst Time of Process %d: ", i);
  30.         scanf("%d", &jobs[i].bt);
  31.  
  32.         if (choice == 5 || choice == 6){
  33.             printf("Enter Priority of Process %d: ", i);
  34.             scanf("%d", &jobs[i].pr);
  35.         }
  36.         printf("\n");
  37.     }    
  38. }
  39.  
  40. void printOutput(job jobs[], int n, int choice){
  41.     int i;
  42.     printf("\nID\tAT\tBT");
  43.     if (choice == 5){
  44.         printf("\tPR");
  45.     }
  46.     printf("\tST\tCT\tWT\tTAT\tRT");
  47.     for (i = 0; i < n; i++){
  48.         printf("\n%d\t%d\t%d\t%d", jobs[i].id, jobs[i].at, jobs[i].bt, jobs[i].st-jobs[i].at);
  49.         if (choice == 5){
  50.             printf("\t%d", jobs[i].pr);
  51.         }
  52.         printf("\t%d\t%d\t%d\t%d" , jobs[i].st, jobs[i].ct, jobs[i].wt, jobs[i].tat);
  53.     }
  54.    
  55.     float avgWT = 0;
  56.     float avgTAT = 0;
  57.     float avgResp = 0;
  58.  
  59.     for(i = 0; i < n; i++){
  60.         avgWT += jobs[i].wt;
  61.         avgTAT += jobs[i].tat;
  62.         avgResp += jobs[i].st - jobs[i].at;
  63.     }
  64.     avgWT /= n;
  65.     avgTAT /= n;
  66.     avgResp /= n;
  67.     printf("\nAverage Waiting Time = %f\nAverage Turnaround Time = %f\nAverage Response Time = %f", avgWT, avgTAT, avgResp);
  68. }
  69.  
  70. void sortJobsAT(job jobs[], int n){
  71.     int i, j;
  72.     job t;
  73.     for(i = 0; i < n-1; i++){
  74.         for(j = i+1; j < n; j++){
  75.             if (jobs[i].at > jobs[j].at){
  76.                 t = jobs[i];
  77.                 jobs[i] = jobs[j];
  78.                 jobs[j] = t;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. void sortJobsST(job jobs[], int n){
  85.     int i, j;
  86.     job t;
  87.     for(i = 0; i < n-1; i++){
  88.         for(j = i+1; j < n; j++){
  89.             if (jobs[i].st > jobs[j].st){
  90.                 t = jobs[i];
  91.                 jobs[i] = jobs[j];
  92.                 jobs[j] = t;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. void sortJobsID(job jobs[], int n){
  99.     int i, j;
  100.     job t;
  101.     for(i = 0; i < n-1; i++){
  102.         for(j = i+1; j < n; j++){
  103.             if (jobs[i].id > jobs[j].id){
  104.                 t = jobs[i];
  105.                 jobs[i] = jobs[j];
  106.                 jobs[j] = t;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void calcFCFS(job jobs[], int n){
  113.     jobs[0].st = jobs[0].at;
  114.     jobs[0].ct = jobs[0].at + jobs[0].bt;
  115.     jobs[0].wt = 0;
  116.     jobs[0].tat = jobs[0].bt;
  117.  
  118.     int i;
  119.     for (i = 1; i < n; i++){
  120.         jobs[i].st = (jobs[i].at > jobs[i-1].ct) ? jobs[i].at : jobs[i-1].ct;
  121.        
  122.         //printf("\nAT[%d] CT[%d] | %d %d\n", i, i-1, jobs[i].at, jobs[i-1].ct);
  123.         //printf("jobs[%d].st = %d\n", i, jobs[i].st);
  124.         jobs[i].ct = jobs[i].st + jobs[i].bt;
  125.         jobs[i].tat = jobs[i].ct - jobs[i].at;
  126.         jobs[i].wt = jobs[i].tat - jobs[i].bt;
  127.        
  128.     }
  129. }
  130.  
  131. void calcSJF(job jobs[], int n){
  132.     int *rt = (int*)malloc(sizeof(int)*n);
  133.     int i, j;
  134.     for(i = 0; i < n; i++){
  135.         rt[i] = jobs[i].bt;
  136.     }
  137.    
  138.     int complete = 0, t = 0, minm = 2147483647;
  139.     int shortest = 0;
  140.     bool check = false;
  141.    
  142.     while (complete != n){
  143.         for(j = 0; j < n; j++){
  144.             if ((jobs[j].at <= t) && ((rt[j] < minm) && (rt[j] > 0))){
  145.                 minm = rt[j];
  146.                 shortest = j;
  147.                 check = true;
  148.             }
  149.         }
  150.        
  151.         if (check == false){
  152.             t++;
  153.             continue; //no process in ready queue
  154.         }
  155.        
  156.         rt[shortest] = 0;
  157.         jobs[shortest].st = t;    
  158.         jobs[shortest].ct = t + jobs[shortest].bt;
  159.         jobs[shortest].wt = jobs[shortest].ct - jobs[shortest].bt - jobs[shortest].at;
  160.            
  161.         jobs[shortest].wt = (jobs[shortest].wt < 0 ) ? 0 : jobs[shortest].wt;
  162.            
  163.         jobs[shortest].tat = jobs[shortest].ct - jobs[shortest].at;
  164.        
  165.         complete++;
  166.         check = false;
  167.         minm = 2147483647;
  168.         t+= jobs[shortest].bt;
  169.     }
  170.  
  171.     sortJobsST(jobs, n);
  172. }
  173.  
  174. void calcSRTF(job jobs[], int n){
  175.     int *rt = (int*)malloc(sizeof(int)*n);
  176.     int i, j;
  177.     for(i = 0; i < n; i++){
  178.         rt[i] = jobs[i].bt;
  179.     }
  180.    
  181.     int complete = 0, t = 0, minm = 2147483647;
  182.     int shortest = 0, finish_time;
  183.     bool check = false;
  184.    
  185.     while (complete != n){
  186.         for(j = 0; j < n; j++){
  187.             if ((jobs[j].at <= t) && ((rt[j] < minm) && (rt[j] > 0))){
  188.                 minm = rt[j];
  189.                 shortest = j;
  190.                 check = true;
  191.             }
  192.         }
  193.        
  194.         if (check == false){
  195.             t++;
  196.             continue; //no process in ready queue
  197.         }
  198.        
  199.         if (rt[shortest] == jobs[shortest].bt){
  200.             jobs[shortest].st = t;
  201.         }
  202.  
  203.         rt[shortest]--;
  204.        
  205.         minm = rt[shortest];
  206.        
  207.         if (minm == 0){
  208.             minm = 2147483647;
  209.         }
  210.        
  211.         if (rt[shortest] == 0){
  212.             complete++;
  213.             check = false;
  214.            
  215.             jobs[shortest].ct = t+1;
  216.             jobs[shortest].wt = jobs[shortest].ct - jobs[shortest].bt - jobs[shortest].at;
  217.            
  218.             jobs[shortest].wt = (jobs[shortest].wt < 0 ) ? 0 : jobs[shortest].wt;
  219.            
  220.             jobs[shortest].tat = jobs[shortest].ct - jobs[shortest].at;
  221.         }
  222.        
  223.         t++;
  224.     }
  225. }
  226.  
  227. bool notIn(int arr[], int size, int k){
  228.     int i;
  229.     bool check = true;
  230.     for(i = 0; i < size; i++){
  231.         if (arr[i] == k){
  232.             check = false;
  233.             break;
  234.         }
  235.     }
  236.     return check;
  237. }
  238.  
  239.  
  240. void checkNew(job jobs[], int n, int t, int rt[], int queue[], int *k, int already){
  241.     int j;
  242.     for(j = 0; j < n; j++){
  243.         if ( (notIn(queue, *k, j) && j != already) && ( (jobs[j].at <= t) && (rt[j] > 0) ) ){
  244.             queue[*k] = j;
  245.             (*k)++;
  246.         }
  247.     }
  248. }
  249.  
  250.  
  251. void calcRR(job jobs[], int n, int q){
  252.     int *rt = (int*)malloc(sizeof(int)*n);
  253.     int *queue = (int*)malloc(sizeof(int)*n);
  254.  
  255.     int i;
  256.  
  257.     for(i = 0; i < n; i++){
  258.         rt[i] = jobs[i].bt;
  259.         queue[i] = -1;
  260.     }
  261.  
  262.     int t = 0;
  263.     int complete = 0;
  264.     int j;
  265.     int k = 0; // elements in queue
  266.     while(complete != n){
  267.         checkNew(jobs, n, t, rt, queue, &k, -1);
  268.         /*
  269.         printf("t=%d, Stack = ", t);
  270.         printArr(queue, k);
  271.         */
  272.         if(k == 0){
  273.             t++;
  274.             continue;
  275.         }
  276.  
  277.         int temp = queue[0];
  278.         int y;
  279.         for(y = 0; y <= k-2; y++){
  280.             queue[y] = queue[y+1];
  281.         }
  282.         k--;
  283.         queue[k] = -1;
  284.  
  285.         if (rt[temp] == jobs[temp].bt){
  286.             jobs[temp].st = t;
  287.         }
  288.  
  289.         if (rt[temp] > q){
  290.             rt[temp] -= q;
  291.             t+=q;
  292.             checkNew(jobs, n, t, rt, queue, &k, temp);
  293.             queue[k] = temp;
  294.             k++;
  295.             //checkNew(jobs, n, t, rt, queue, &k);
  296.         }
  297.         else{
  298.             t+= rt[temp];
  299.             rt[temp] = 0;
  300.             jobs[temp].ct = t;
  301.             jobs[temp].tat = jobs[temp].ct - jobs[temp].at;
  302.             jobs[temp].wt = jobs[temp].tat - jobs[temp].bt;
  303.             complete++;
  304.         }
  305.         /*
  306.         printf("t=%d, Stack = ", t);
  307.         printArr(queue, k);
  308.         */
  309.     }
  310. }
  311.  
  312. void calcPriority(job jobs[], int n){
  313.     int *rt = (int*)malloc(sizeof(int)*n);
  314.     int i, j;
  315.     for(i = 0; i < n; i++){
  316.         rt[i] = jobs[i].bt;
  317.     }
  318.    
  319.     int complete = 0, t = 0, minm = 2147483647;
  320.     int mostImportant = 0, finish_time;
  321.     bool check = false;
  322.    
  323.     while (complete != n){
  324.         for(j = 0; j < n; j++){
  325.             if ((jobs[j].at > t) || ((rt[j] == 0))){
  326.                continue;
  327.             }
  328.             if (jobs[j].pr < minm){
  329.                 minm = jobs[j].pr;
  330.                 mostImportant = j;
  331.                 check = true;
  332.             }
  333.         }
  334.        
  335.         if (check == false){
  336.             t++;    
  337.             continue; //no process in ready queue
  338.         }
  339.        
  340.         //printf("\nt=%d, Process %d", t, mostImportant);
  341.  
  342.         if (rt[mostImportant] == jobs[mostImportant].bt){
  343.             jobs[mostImportant].st = t;
  344.         }
  345.  
  346.         rt[mostImportant]--;
  347.        
  348.         if (rt[mostImportant] == 0){
  349.             complete++;
  350.             check = false;
  351.             minm = 2147483647;
  352.            
  353.             jobs[mostImportant].ct = t+1;
  354.             jobs[mostImportant].wt = jobs[mostImportant].ct - jobs[mostImportant].bt - jobs[mostImportant].at;
  355.            
  356.             jobs[mostImportant].wt = (jobs[mostImportant].wt < 0 ) ? 0 : jobs[mostImportant].wt;
  357.            
  358.             jobs[mostImportant].tat = jobs[mostImportant].ct - jobs[mostImportant].at;
  359.         }
  360.        
  361.         t++;
  362.     }
  363. }
  364.  
  365. void all(job jobs[], int n, int q){
  366.     printf("\nFCFS\n");
  367.     calcFCFS(jobs, n);
  368.     printOutput(jobs, n, 1);
  369.     printf("\n");
  370.     printf("\nSJF\n");
  371.     calcSJF(jobs, n);
  372.     printOutput(jobs, n, 2);
  373.     printf("\n");
  374.     printf("\nSRTF\n");
  375.     calcSRTF(jobs, n);
  376.     printOutput(jobs, n, 3);
  377.     printf("\n");
  378.     printf("\nRR\n");
  379.     calcRR(jobs, n, q);
  380.     printOutput(jobs, n, 4);
  381.     printf("\n");
  382.     printf("\nPriority\n");
  383.     calcPriority(jobs, n);
  384.     printOutput(jobs, n, 5);
  385. }
  386.  
  387. int main(){
  388.     int k;
  389.     int choice;
  390.     printf("\n\t1. FCFS Scheduling Algorithm\n");
  391.     printf("\n\t2. SJF Scheduling Algorithm\n");
  392.     printf("\n\t3. SRTF Scheduling Algorithm\n");
  393.     printf("\n\t4. RR Scheduling Algorithm\n");
  394.     printf("\n\t5. Priority Scheduling Algorithm\n");
  395.     printf("\n\t6. All\n>>");
  396.     scanf("%d", &choice);
  397.     if (!(choice >= 1 && choice <=6)){
  398.         printf("\nINVALID");
  399.         exit(1);
  400.     }
  401.  
  402.     printf("\n1. Same Arrival time (0)");
  403.     printf("\n2. Different Arrival Times\n>>");
  404.     scanf("%d", &k);
  405.     if (!(k == 1) && !(k == 2)){
  406.         printf("\nINVALID");
  407.         exit(1);
  408.     }
  409.     int n;
  410.     printf("Enter number of processes: ");
  411.     scanf("%d", &n);
  412.     job *jobs;
  413.     jobs = (job*)malloc(sizeof(job)*n);
  414.    
  415.     int quantum;
  416.     if (choice == 4 || choice == 6){
  417.         printf("\nEnter Time Quantum: ");
  418.         scanf("%d", &quantum);
  419.     }
  420.  
  421.     takeInput(jobs, n, choice, k);
  422.     sortJobsAT(jobs, n);
  423.  
  424.     switch (choice)
  425.     {
  426.     case 1:
  427.         calcFCFS(jobs, n);
  428.         printOutput(jobs, n, choice);
  429.         break;
  430.     case 2:
  431.         calcSJF(jobs, n);
  432.         printOutput(jobs, n, choice);
  433.         break;
  434.     case 3:
  435.         calcSRTF(jobs, n);
  436.         printOutput(jobs, n, choice);
  437.         break;
  438.     case 4:
  439.         calcRR(jobs, n, quantum);
  440.         printOutput(jobs, n, choice);
  441.         break;
  442.     case 5:
  443.         calcPriority(jobs, n);
  444.         printOutput(jobs, n, choice);
  445.         break;
  446.     case 6:
  447.         all(jobs, n, quantum);
  448.         break;
  449.     default:
  450.         break;
  451.     }
  452.  
  453.     return 0;
  454. }
  455.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement