csenotes12

Sudesh Sharama

Apr 9th, 2019
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.93 KB | None | 0 0
  1.  
  2. // Sudesh Sharma is a Linux expert who wants to have an online system where he can handle student queries. Since there can be multiple
  3. // requests at any time he wishes to dedicate a fixed amount of time to every request so that everyone gets a fair share of his time.
  4. // He will log into the system from 10am to 12am only.  He wants to have separate requests queues for students and faculty.
  5. // Implement a strategy for the same. The summary at the end of the session should include the total time he spent on handling queries
  6. // and average query time.
  7.  
  8. #include<stdio.h>
  9. #include<string.h>
  10.  
  11. struct process_Struct {
  12.     char process_name[20];
  13.     int arrival_time, burst_time, completion_time, remaining;
  14. }temp_Struct;
  15.  
  16.  
  17. void faculty_Queue(int no_of_process) {
  18.  
  19.     int count, arrival_Time, burst_Time, quantum_time;
  20.     struct process_Struct faculty_Process[no_of_process];
  21.  
  22.     for(count = 0; count < no_of_process; count++) {
  23.         printf("Enter the details of Process[%d]", count+1);
  24.         puts("");
  25.         printf("Process Name : ");
  26.         scanf("%s", faculty_Process[count].process_name);
  27.  
  28.         printf("Arrival Time : ");
  29.         scanf("%d", &faculty_Process[count].arrival_time);
  30.  
  31.         printf("Burst Time : ");
  32.         scanf("%d", &faculty_Process[count].burst_time);
  33.         puts("");
  34.     }
  35.     printf("Now, enter the quantum time for FACULTY queue : ");
  36.     scanf("%d", &quantum_time);
  37.  
  38.  
  39.     // sorting the processes by their ARRIVAL time.
  40.     // if the ARRIVAL time is same then scheduling is based on FCFS.
  41.     for(count = 0; count < no_of_process; count++) {
  42.         for(int x = count +1; x < count; x++){
  43.             if(faculty_Process[count].arrival_time > faculty_Process[x].arrival_time) {
  44.                 temp_Struct = faculty_Process[count];
  45.                 faculty_Process[count] = faculty_Process[x];
  46.                 faculty_Process[x] = temp_Struct;
  47.             }
  48.         }
  49.     }
  50.  
  51.     // initialy all the burst time is remaining and completion of process is zero.
  52.     for(count = 0; count < no_of_process; count++) {
  53.         faculty_Process[count].remaining = faculty_Process[count].burst_time;
  54.         faculty_Process[count].completion_time = 0;
  55.     }
  56.  
  57.     int total_time, queue, round_robin[20];
  58.     total_time = 0;
  59.     queue = 0;
  60.     round_robin[queue] = 0;
  61.  
  62.    
  63.     int flag, x, n, z, waiting_time = 0;
  64.     do {
  65.         for(count = 0; count < no_of_process; count++){
  66.             if(total_time >= faculty_Process[count].arrival_time){
  67.                 z = 0;
  68.                 for(x = 0; x <= queue; x++) {
  69.                     if(round_robin[x] == count) {
  70.                         z++;
  71.                     }
  72.                 }
  73.                 if(z == 0) {
  74.                     queue++;
  75.                     round_robin[queue] == count;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         if(queue == 0) {
  81.             n = 0;
  82.         }
  83.         if(faculty_Process[n].remaining == 0) {
  84.             n++ ;
  85.         }
  86.         if(n > queue) {
  87.             n = (n - 1) % queue;
  88.         }
  89.         if(n <= queue) {
  90.             if(faculty_Process[n].remaining > 0) {
  91.                 if(faculty_Process[n].remaining < quantum_time){
  92.                     total_time += faculty_Process[n].remaining;
  93.                     faculty_Process[n].remaining = 0;
  94.                 }else {
  95.                     total_time += quantum_time;
  96.                     faculty_Process[n].remaining -= quantum_time;
  97.                 }
  98.                 faculty_Process[n].completion_time = total_time;
  99.             }
  100.             n++;
  101.         }
  102.         flag = 0;
  103.        
  104.         for(count = 0; count < no_of_process; count++) {
  105.             if(faculty_Process[count].remaining > 0) {
  106.                 flag++;
  107.             }
  108.         }
  109.     }while(flag != 0);
  110.  
  111.  
  112.     puts("\n\t\t\t********************************************");
  113.     puts("\t\t\t*****   ROUND ROBIN ALGORITHM OUTPUT   *****");
  114.     puts("\t\t\t********************************************\n");
  115.     printf("\n|\tProcess Name\t  |\tArrival Time\t  |\tBurst Time\t |\tCompletion Time  \t|\n");
  116.  
  117.     for(count = 0; count < no_of_process; count++){
  118.         waiting_time = faculty_Process[count].completion_time - faculty_Process[count].burst_time - faculty_Process[count].arrival_time;
  119.  
  120.         printf("\n|\t  %s\t    |\t  %d\t   |\t  %d\t   |\t  %d\t   |\n", faculty_Process[count].process_name, faculty_Process[count].arrival_time, faculty_Process[count].burst_time, faculty_Process[count].completion_time);
  121.     }
  122.  
  123. }
  124.  
  125.  
  126. void student_Queue(int no_of_process) {
  127.  
  128.     int count, arrival_Time, burst_Time, quantum_time;
  129.     struct process_Struct student_Process[no_of_process];
  130.  
  131.     for(count = 0; count < no_of_process; count++) {
  132.         printf("Enter the details of Process[%d]", count+1);
  133.         puts("");
  134.         printf("Process Name : ");
  135.         scanf("%s", student_Process[count].process_name);
  136.  
  137.         printf("Arrival Time : ");
  138.         scanf("%d", &student_Process[count].arrival_time);
  139.  
  140.         printf("Burst Time : ");
  141.         scanf("%d", &student_Process[count].burst_time);
  142.     }
  143.     printf("Now, enter the quantum time for STUDENT queue : ");
  144.     scanf("%d", &quantum_time);
  145.  
  146.  
  147.     // sorting the processes by their ARRIVAL time.
  148.     // if the ARRIVAL time is same then scheduling is based on FCFS.
  149.     for(count = 0; count < no_of_process; count++) {
  150.         for(int x = count +1; x < count; x++){
  151.             if(student_Process[count].arrival_time > student_Process[x].arrival_time) {
  152.                 temp_Struct = student_Process[count];
  153.                 student_Process[count] = student_Process[x];
  154.                 student_Process[x] = temp_Struct;
  155.             }
  156.         }
  157.     }
  158.  
  159.     // initialy all the burst time is remaining and completion of process is zero.
  160.     for(count = 0; count < no_of_process; count++) {
  161.         student_Process[count].remaining = student_Process[count].burst_time;
  162.         student_Process[count].completion_time = 0;
  163.     }
  164.  
  165.     int total_time, queue, round_robin[20];
  166.     total_time = 0;
  167.     queue = 0;
  168.     round_robin[queue] = 0;
  169. }
  170.  
  171.  
  172. int main(int argc, char const *argv[]) {
  173.     int select_queue, no_of_process;
  174.  
  175.     puts("Please choose a queue to post your query : ");
  176.     puts("1. FACULTY queue.");
  177.     puts("2. STUDENT queue.");
  178.     printf("> ");
  179.     scanf("%d", &select_queue);
  180.  
  181.     switch(select_queue) {
  182.         case 1 :
  183.                 printf("Enter number of process for FACULTY queue : ");
  184.                 scanf("%d", &no_of_process);
  185.                
  186.                 faculty_Queue(no_of_process);
  187.                
  188.                 break;
  189.  
  190.         case 2 :
  191.                 printf("Enter number of process for STUDENT queue : ");
  192.                 scanf("%d", &no_of_process);
  193.  
  194.                 student_Queue(no_of_process);
  195.                
  196.                 break;
  197.  
  198.         default :
  199.                 printf("Please selet the correct option by running the program again.");
  200.     }
  201.  
  202.     return 0;
  203. }
Add Comment
Please, Sign In to add comment