159 lines
4.0 KiB
C++
159 lines
4.0 KiB
C++
#include<stdio.h>
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
|
|
using namespace std;
|
|
|
|
struct process{
|
|
int pid; //Process ID
|
|
int burst_time; //CPU Burst Time
|
|
int working_time; //Time this process executed, if working_time==burst_time, process is complete
|
|
int t_round; //turn around time, time needed for the process to complete
|
|
int remainTime;
|
|
int wait_time;
|
|
int execTime;
|
|
};
|
|
|
|
|
|
|
|
|
|
int main()
|
|
{
|
|
|
|
int timeArray[10000], pidArray[10000];
|
|
int timeIndex=0;
|
|
int count,j,n,time,remain,flag=0,time_quantum;
|
|
int wait_time=0,turnaround_time=0,at[n];
|
|
printf("Enter Total Process:\t ");
|
|
scanf("%d",&n);
|
|
remain=n;
|
|
struct process pr[n];
|
|
|
|
for(count=0;count<n;count++)
|
|
{
|
|
cout << "\nEnter PID for process " << count+1 << " (ms): ";
|
|
cin >> pr[count].pid;
|
|
cout << "\nEnter Burst Time for process " << count + 1 << " (ms): ";
|
|
cin >> pr[count].burst_time;
|
|
pr[count].remainTime=pr[count].burst_time;
|
|
}
|
|
|
|
// arrival time is zero
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
at[i] = 0;
|
|
}
|
|
printf("Enter Time Quantum:\t");
|
|
scanf("%d",&time_quantum);
|
|
for(time=0,count=0;remain!=0;)
|
|
{
|
|
// the process still has some work time left
|
|
if(pr[count].remainTime<=time_quantum && pr[count].remainTime>0)
|
|
{
|
|
pidArray[timeIndex] = pr[count].pid;
|
|
timeArray[timeIndex] = pr[count].remainTime;
|
|
time+=pr[count].remainTime;
|
|
pr[count].remainTime=0;
|
|
flag=1;
|
|
// n = timeDuringQuantum - remainingTime
|
|
}
|
|
//
|
|
else if(pr[count].remainTime>0)
|
|
{
|
|
pr[count].remainTime-=time_quantum;
|
|
time+=time_quantum;
|
|
pidArray[timeIndex]= pr[count].pid;
|
|
timeArray[timeIndex] = time_quantum;
|
|
}
|
|
else{
|
|
timeIndex--;
|
|
}
|
|
|
|
if(pr[count].remainTime==0 && flag==1)
|
|
{
|
|
remain--;
|
|
pr[count].t_round = time-at[count];
|
|
pr[count].wait_time = time-at[count]-pr[count].burst_time;
|
|
// printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-pr[count].burst_time);
|
|
wait_time+=time-at[count]-pr[count].burst_time;
|
|
turnaround_time+=time-at[count];
|
|
flag=0;
|
|
}
|
|
if(count==n-1)
|
|
{
|
|
count=0;
|
|
timeIndex++;
|
|
}
|
|
else if(at[count+1]<=time)
|
|
{
|
|
count++;
|
|
timeIndex++;
|
|
}
|
|
else
|
|
count=0;
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < timeIndex; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
|
}
|
|
cout << endl;
|
|
cout << "| Time (ms) |";
|
|
for (int i = 0; i < timeIndex; i++) {
|
|
cout << setw(9) << setfill(' ') << timeArray[i] << setw(4) << setfill(' ')<< "|";
|
|
}
|
|
cout << endl;
|
|
for (int i = 0; i < timeIndex; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
|
}
|
|
cout << endl;
|
|
cout << "| PID |";
|
|
for (int i = 0; i < timeIndex; i++) {
|
|
cout << setw(9) << setfill(' ') << pidArray[i] << setw(4) << setfill(' ')<< " |";
|
|
}
|
|
cout<<endl;
|
|
for (int i = 0; i < timeIndex; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
|
}
|
|
cout << endl << endl;
|
|
// Waiting time
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
|
}
|
|
cout << endl;
|
|
cout << "| PID |";
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(6) << setfill(' ') << pidArray[i] << setw(4) << setfill(' ')<< " |";
|
|
}
|
|
cout << endl;
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
|
}
|
|
cout << endl;
|
|
|
|
cout << "| Wait Time (ms) |";
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(4) << setfill(' ') << pr[i].wait_time << setw(5) << setfill(' ')<< "|";
|
|
}
|
|
cout << endl;
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
|
}
|
|
cout << endl;
|
|
cout << "| Turn Around Time (ms) |";
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(4) << setfill(' ') << pr[i].t_round << setw(4) << setfill(' ')<< " |";
|
|
}
|
|
cout << endl;
|
|
for (int i = 0; i < n; i++) {
|
|
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
|
|
|
}
|
|
|
|
|
|
printf("\nAverage Waiting Time= %d\n",wait_time/n);
|
|
|
|
return 0;
|
|
} |