In: Computer Science
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:
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:
RoundRobin thread:
#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.