Question

In: Computer Science

In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling...

In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling algorithm.

It should implement threads for the algorithm plus the main thread using a linked list to represent the list of jobs available to run. Each node will represent a Job with the following information:

  • int ProcessID
  • int time
    • time needed to finish executing

The thread representing the scheduler algorithm should continue running until all jobs end. This is simulated using the time variable in each node; the program thread will iterate through the linked list jobs until the list is empty. In each iteration, you will subtract a quantum amount from each job to simulate the processing time executing on a CPU. If a node time variable is less than or equal to zero, this will simulate that a process has completed, and it is time to remove this job/node from the list.

Main thread:

  • Ask the user to specify a value to be used as the quantum.
    • request the user to input the number of jobs, and then proceed to fill the information in each job node; each data item will be stored in a separate node.
    • Start the RoundRobin Thread and wait till all jobs finish.

RoundRobin thread:

  • The thread will simulate the Round Robin algorithm.
    • Displays data in the list in the order they were inserted.
    • Each iteration of the algorithm, the thread should deduct the quantum amount from each job.
    • If a job time reaches zero, remove this job from the linked list and continue to iterate the list.
    • When there are no more jobs in the list, exit the thread.

Solutions

Expert Solution

#include <stdio.h>
struct process{
      char process_name;
      int arrival_time,arrival_time1, burst_time,burst_time1, ct, waiting_time, turnaround_time,current_time,responsefinish,response;
      int status;
      float mtat;
}process_queue[10];
void sort(int limit){
   int i,j;
   struct process time;
   for(i=0;i<limit-1;i++){
       for(j=i+1;j<limit;j++){
           if(process_queue[i].arrival_time > process_queue[j].arrival_time){
               time = process_queue[i];
               process_queue[i] = process_queue[j];
               process_queue[j] = time;
           }
       }
   }
}
int main(){
   int limit;
   int burst_time,i;
   char c;
   printf("\nEnter Total Number of Processes:\t");
    scanf("%d", &limit);
      for(i = 0, c = 'A'; i < limit; i++, c++)
      {
            process_queue[i].process_name = c;
            printf("\nEnter Details For Process[%C]:\n", process_queue[i].process_name);
            printf("Enter Arrival Time : ");
            scanf("%d",&process_queue[i].arrival_time);
            process_queue[i].arrival_time1 = process_queue[i].arrival_time;
            printf("Enter Burst Time:\t");
            process_queue[i].arrival_time1 = process_queue[i].arrival_time;
            scanf("%d", &process_queue[i].burst_time);
            process_queue[i].burst_time1 = process_queue[i].burst_time;
            process_queue[i].status = 0;
          
      }
      int quantum;
      printf("Enter a quantum number:");
      scanf("%d",&quantum);
      sort(limit);
      printf("\nProcess Name\tArrival Time\tBurst Time\tWaiting Time\tTurnaround_time\tresponse\tmtat\n");
      int wait_time = 0,turnaround_time = 0;
      int time;
      int t = 0;
      i = 0;
      int j = 0;
      int k=0,index = 0,switches = 0,context = 0;
      while(1){
                   int min = 1000,index = limit;
                   if(k == limit){
                       break;
                   }
                   for(j=0;j<limit;j++){
                       if((process_queue[j].status != 1) && (process_queue[j].arrival_time <= t) && (process_queue[j].arrival_time < min)){
                               min = process_queue[j].arrival_time;
                               index = j;
                           }
                   }
                   if(min == 1000){
                       t++;
                   }else{
                       if(index != context){
                           context = index;
                           switches++;
                       }
                       if(process_queue[index].responsefinish == 0){
                           process_queue[index].response = t - process_queue[index].arrival_time1;
                           process_queue[index].responsefinish = 1;
                       }
                       for(i=0; ((i<quantum) && (process_queue[index].burst_time != 0)); i++){
                               process_queue[index].current_time=t+1;
                               t = t+1;  
                               process_queue[index].burst_time--;
                       }
                       if(process_queue[index].burst_time == 0){
                           k++;
                           process_queue[index].turnaround_time = process_queue[index].current_time - process_queue[index].arrival_time1;
                           turnaround_time = turnaround_time + process_queue[index].turnaround_time;
                           process_queue[index].waiting_time = process_queue[index].turnaround_time - process_queue[index].burst_time1;
                           wait_time = wait_time + process_queue[index].waiting_time;  
                           process_queue[index].status = 1;
                           process_queue[index].mtat = (float)process_queue[index].turnaround_time/process_queue[index].burst_time1;
                           printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%0.2f", process_queue[index].process_name, process_queue[index].arrival_time1, process_queue[index].burst_time1, process_queue[index].waiting_time,process_queue[index].turnaround_time,process_queue[index].response,process_queue[index].mtat);
                       }
                       else{
                           if(k != limit-1){
                               process_queue[index].arrival_time = t + 1;
                           }
                       }
               }
      }
      float average_waiting_time,average_turnaround_time;
      average_waiting_time = wait_time / limit;
      average_turnaround_time = turnaround_time / limit;
      printf("\n\nAverage waiting time:\t%f\n", average_waiting_time);
      printf("Average Turnaround Time:\t%f\n", average_turnaround_time);
      printf("context switches\t%d\n",switches);
   return 0;
}  

Output

Please upvote.please comment if queries.


Related Solutions

Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
There are n processes in a queue. Each process has namei and timei. The round-robin scheduling...
There are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue. For example, we have the following queue with the quantum of 100ms. A(150) - B(80)...
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is...
The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue. Process            Burst Time Arrival Time...
First-Come, First-Served (FCFS) Scheduling Shortest-Job-Next (SJN) Scheduling Priority Scheduling Shortest Remaining Time Round Robin(RR) Scheduling Multiple-Level...
First-Come, First-Served (FCFS) Scheduling Shortest-Job-Next (SJN) Scheduling Priority Scheduling Shortest Remaining Time Round Robin(RR) Scheduling Multiple-Level Queues Scheduling Provide a timing example of each of the algorithms above. List some processes (at least four) with the appropriate properties for creating a time diagram (such as Process ID, Arrival Time, Burst Time, and Execution Time). Walk through the timing diagram identifying the algorithm you’re using and state which process goes first, which process finishes first, etc.
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each...
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue. Process          Burst Time      Arrival Time   Priority P1                15            0            8...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
For Round Robin (RR) Scheduling, What is the arrival time, completion time, burst time, turnaround time,...
For Round Robin (RR) Scheduling, What is the arrival time, completion time, burst time, turnaround time, and the waiting time? discuss these terms further as they pertain to the efficiency of the algorithm as well as properties such as starvation. Pros and Cons of RR
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT