Write a program to implement round robin scheduling concept using a linked list. Each node will represent a process with attributes as <PID, CPU_time>. A quantum time T will be input by the user.
A process will be executed (when user wants) for quantum time T and the CPU_time will be updated with CPU_time T, and the current process node is deleted from the beginning and added to the end of the list. If the updated CPU_time is <=0, then the process is deleted from the list.
After a process is executed for T time, the the next process in the queue is executed upon user choice. The entire process gets completed when all process nodes are deleted.
#include<stdio.h>
#include<stdbool.h>
void findWaitingTime(int process[], int n,
int Cpu_time[], int PID[], int Q)
{
int rem_Q[n];
for (int i = 0 ; i < n ; i++)
rem_Q[i] = Cpu_time[i];
int t = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_Q[i] > 0)
{
done = false;
if (rem_Q[i] > Q)
{
t += Q;
rem_Q[i] -= Q;
}
else
{
t = t + rem_Q[i];
PID[i] = t - Cpu_time[i];
rem_Q[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int process[], int n,
int Cpu_time[], int PID[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = Cpu_time[i] + PID[i];
}
void findavgTime(int process[], int n, int Cpu_time[],
int Q)
{
int PID[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(process, n, Cpu_time, PID, Q);
findTurnAroundTime(process, n, Cpu_time, PID, tat);
printf("Processes ");
printf(" Burst time ");
printf(" Waiting time " );
printf(" Turn around time\n");
for (int i=0; i<n; i++)
{
total_wt = total_wt + PID[i];
total_tat = total_tat + tat[i];
printf(" " );
printf("%d",i+1);
printf("\t\t" );
printf("%d",Cpu_time[i] );
printf("\t ");
printf("%d",PID[i] );
printf("\t\t " );
printf("%d",tat[i] );
printf("\n");
}
printf("Average waiting time = ");
printf("%lf",(float)total_wt / (float)n);
printf("\nAverage turn around time = ");
printf("%lf",(float)total_tat / (float)n);
}
int main()
{
int process[] = { 1, 2, 3};
int n = sizeof process / sizeof process[0];
int BTime[] = {10, 5, 8};
int Q = 2;
findavgTime(process, n, BTime, Q);
return 0;
}
Comments
Leave a comment