A-5 - Lab
A-5 - Lab
Include necessary header files for standard input/output operations and console I/O
functions.
struct process
This declares a structure named process with four members: pid (process ID), bt (burst
time), tt (turnaround time), and wt (waiting time).
{
int pid, bt, tt, wt;
};
int main()
It declares arrays of structures (x and p), along with some variables (i, j, k, tot, m, n,
wttime, tottime, a1, a2).
clrscr() is used to clear the console screen, but remember that it's not a standard
function.
The total burst time (tot) is calculated by summing up the burst times of all processes.
}
printf("\nTotal Burst Time:\t%d", tot);
The total burst time is displayed, and then the user is prompted to input the time slice
(m).
p[0].tt = 0;
k = 1;
printf("\nEnter the Time Slice:\t");
scanf("%d", &m);
{
if (x[i].bt != 0)
{
p[k].pid = i;
if (x[i].bt - m < 0)
{
p[k].wt = p[k - 1].tt;
p[k].bt = x[i].bt;
p[k].tt = p[k].wt + x[i].bt;
x[i].bt = 0;
k++;
}
It iterates over each time slice and each process, updating the waiting time, burst
time, and turnaround time accordingly.
else
{
p[k].wt = p[k - 1].tt;
p[k].tt = p[k].wt + m;
x[i].bt = x[i].bt - m;
k++;
}
}
}
}
}
printf("\n\nAverage Waiting Time:\t%f", a1);
printf("\n\nAverage TurnAround Time:\t%f", a2);
It then calculates and displays the average waiting time and average turnaround time.
getch();
The getch() function is used to wait for a key press before closing the program.
return 0;
}
Note that the calculation of averages (a1 and a2) should be outside the loop that
iterates over the processes to get correct results.