System Software Rohan
Ashra
Scheduling
11811116
Roll no
05
1.First come First Serve
//FCFS
#include <iostream>
using namespace std;
int main(){
int
t=0,
i,
j,
numOfProcesses = 0,
s=0,
print=0,
idle=0;
float sum=0;
cout << "Please enter the number of proces : ";
cin >> numOfProcesses;
int a[numOfProcesses][4];
int b[numOfProcesses][5];
cout<<"\n\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
for(i = 0; i < numOfProcesses; i++){
for(j = 0; j < 3; j++){
cin >> a[i][j];
}
a[i][3] = a[i][2];
}
cout << "\n\nTime-Line is as follows (Verticle View)....\n\n";
/**************************Processing Starts Here*************/
for(i = 0, t = a[0][1]; i < numOfProcesses; i++){
while(a[i][2] != 0){
if(t >= a[i][1]){
if(print == 0)
printf("%5d-----------\n |p-%-4d|\n", t,
a[i][0]);
print = 1;
idle = 0;
a[i][2]--;
}
else{
if(idle == 0){
printf("%5d-----------\n |Idle |\n", t);
idle = 1;
}
}
t++;
}
print = 0;
b[s][0] = a[i][0];
b[s][1] = a[i][1];
b[s][2] = t;
b[s][3] = a[i][3];
b[s][4] = ((t - a[i][1]) - a[i][3]);
sum += b[s][4];
s++;
}
printf("%5d-----------\n", t);
/**************************Processing Ends Here*************/
cout << endl << endl;
cout << "Table of processes with completion record as they were
completed\n\n";
cout<<"\n\nProcess\tArrival\tBurst\tFinish\tTAT\tWait\n------------------
------------------\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][3]<<"\t"<<b[i][2]<<"\t"<<b[i][2]-
b[i][1]<<"\t"<<b[i][4]<<"\n";
cout << "\n\nAvg. Wait time = " << (sum/numOfProcesses) << endl << endl;
return 0;
}
Output:
2.Round Robin
#include <iostream>
using namespace std;
int main(){
int i,j,k=0,p=0,q,temp,s=0,idle=0,done=0;
float sum=0;
//taking input
cout<<"Please enter the number of proces : ";
cin>>p;
int a[p][4];
int b[p][5];
cout<<"Please enter the Time Quantum : ";
cin>>q;
cout<<"\n\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
for(i=0;i<p;i++){
for(j=0;j<3;j++){
cin>>a[i][j];
}
a[i][3]=a[i][2];//storing total time of each process to another cell
cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
i=a[0][1];
while(done!=p){
if(k==p)
k=0;
if(a[k][1]<=i){
if(a[k][2]!=0){
if(a[k][2]>=q){
printf("%5d-----------\n |p-%-
4d|\n",i,a[k][0]);
a[k][2]-=q;
i+=q;
}
else{
printf("%5d-----------\n |p-%-
4d|\n",i,a[k][0]);
i+=a[k][2];
a[k][2]=0;
}
if(a[k][2]==0){
b[s][0]=a[k][0];
b[s][1]=a[k][1];
b[s][2]=i;
b[s][3]=a[k][3];
b[s][4]=((i-a[k][1])-a[k][3]);
sum+=((i-a[k][1])-a[k][3]);
s++;
done++;
}
idle=0;
}
k++;
}
else{
if(idle==0){
idle=1;
k=0;
}
else if(idle==1){
printf("%5d-----------\n |Idle |\n",i);
idle=2;
i++;
}
else
i++;
}
}
printf("%5d-----------\n",i);
cout<<endl<<endl;
cout<<"Table of processes with completion record as they were completed\n
\n";
cout<<"\n\nProcess\tArrival\tBurst\tFinish\tTAT\tWait\n------------------
------------------\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][3]<<"\t"<<b[i][2]<<"\t"<<b[
i][2]-b[i][1]<<"\t"<<b[i][4]<<"\n";
cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
return 0;
}
Output:
3.Priority-Preemptive
#include <iostream>
using namespace std;
int main(){
int i,j,k,p,s=0, got=0, idle=0, temp_burst, temp_row, pre_process_row, do
ne=0;
float sum=0;
cout<<"Please enter the number of proces : ";
cin>>p;
int a[p][5];
int b[p][5];
cout<<"\n\nProcess\tArrival\tBurst\tPriority\n-------\t-------\t-----\t--
------\n";
for(i=0;i<p;i++){
for(j=0;j<5;j++){
if(j!=3){
cin>>a[i][j];
}
}
a[i][3]=a[i][2];
}
cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
i=a[0][1];
while(done!=p){
got=0;
k=0;
while(k<p){
if(a[k][1]<=i){
if(a[k][2]!=0){
got=1;
temp_burst=a[k][4];
temp_row=k;
idle=0;
break;
}
else
k++;
}
else{
if(idle==0)
printf("%5d-----------\n |Idle |\n",i);
idle=1;
break;
}
}
if(got!=0){
k=0;
while(a[k][1]<=i && k<p){
if(a[k][2]!=0){
if(temp_burst>a[k][4]){
temp_burst=a[k][4];
temp_row=k;
}
}
k++;
}
a[temp_row][2]-=1;
if(i==a[0][1])
printf("%5d-----------\n |p-%-4d|\n",i,a[temp_row][0]);
else{
if(pre_process_row!=temp_row)
printf("%5d-----------\n |p-%-
4d|\n",i,a[temp_row][0]);
}
pre_process_row=temp_row;
if(a[temp_row][2]==0){
done++;
b[s][0]=a[temp_row][0];
b[s][1]=a[temp_row][1];
b[s][2]=i+1;
b[s][3]=a[temp_row][3];
b[s][4]=((i-a[temp_row][1])-a[temp_row][3])+1;
sum+=((i-a[temp_row][1])-a[temp_row][3])+1;
s++;
}
}
i++;
}
printf("%5d-----------\n",i);
cout<<endl<<endl;
cout<<"Table of processes with completion record as they were completed\n
\n";
cout<<"\n\nProcess\tArrival\tFinish\tTAT\tWait\n-------------------------
-----------\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i][2]-
b[i][1]<<"\t"<<b[i][4]<<"\n";
cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
//system("pause");
return 0;
}
Output:
4)SJF Preemptive
#include <iostream>
using namespace std;
int main(){
int i,j,k,p,s=0, got=0, idle=0, temp_burst, temp_row, pre_process_row, do
ne=0;
float sum=0;
cout<<"Please enter the number of process : ";
cin>>p;
int a[p][5];
int b[p][5];
cout<<"\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
for(i=0;i<p;i++){
for(j=0;j<3;j++){
cin>>a[i][j];
}
a[i][3]=a[i][2];
}
cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
i=a[0][1];
while(done!=p){
got=0;
k=0;
while(k<p){
if(a[k][1]<=i){
if(a[k][2]!=0){
got=1;
temp_burst=a[k][2];
temp_row=k;
idle=0;
break;
}
else
k++;
}
else{
if(idle==0)
printf("%5d-----------\n |Idle |\n",i);
idle=1;
break;
}
}
if(got!=0){
k=0;
while(a[k][1]<=i && k<p){
if(a[k][2]!=0){
if(temp_burst>a[k][2]){
temp_burst=a[k][2];
temp_row=k;
}
}
k++;
}
a[temp_row][2]-=1;
if(i==a[0][1])
printf("%5d-----------\n |p-%-
4d|\n",i,a[temp_row][0]);
else{
if(pre_process_row!=temp_row)
printf("%5d-----------\n |p-%-
4d|\n",i,a[temp_row][0]);
}
pre_process_row=temp_row;
if(a[temp_row][2]==0){
done++;
b[s][0]=a[temp_row][0];
b[s][1]=a[temp_row][1];
b[s][2]=i+1;
b[s][3]=a[temp_row][3];
b[s][4]=((i-a[temp_row][1])-a[temp_row][3])+1;
sum+=((i-a[temp_row][1])-a[temp_row][3])+1;
s++;
}
}
i++;
}
printf("%5d-----------\n",i);
cout<<endl<<endl;
cout<<"Table of processes with completion record as they were completed\n
\n";
cout<<"\n\nProcess\tArrival\tFin\tTAT\tWait\n-------\t-------\t---\t-----
\t----\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i] [2]-
b[i][1]<<"\t"<<b[i][4]<<"\n";
cout<<"\nAvg. Wait time = "<<sum/p<<endl<<endl;
return 0;
}
Output:
5)SJF- Non Preemptive
// C++ program to implement Shortest Job first with Arrival Time
#include<iostream>
using namespace std;
int mat[10][6];
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void arrangeArrival(int num, int mat[][6])
{
for(int i=0; i<num; i++)
{
for(int j=0; j<num-i-1; j++)
{
if(mat[j][1] > mat[j+1][1])
{
for(int k=0; k<5; k++)
{
swap(mat[j][k], mat[j+1][k]);
}
}
}
}
}
void completionTime(int num, int mat[][6])
{
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];
for(int i=1; i<num; i++)
{
temp = mat[i-1][3];
int low = mat[i][2];
for(int j=i; j<num; j++)
{
if(temp >= mat[j][1] && low >= mat[j][2])
{
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for(int k=0; k<6; k++)
{
swap(mat[val][k], mat[i][k]);
}
}
}
int main()
{
int num, temp;
cout<<"Enter number of Process: ";
cin>>num;
cout<<"\n\nProcess\tArrival\tBurst\n-------\t-----\t--------\n";
for(int i=0; i<num; i++)
{
cin>>mat[i][0]>>mat[i][1]>>mat[i][2];
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout<<"\n\nProcess\tArrival\tBurst\tWait\tTAT\n-------------------------------
-----\n";
float sum=0;
for(int i=0; i<num; i++)
{
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i][
4]<<"\t\t"<<mat[i][5]<<"\n";
sum+=mat[i][4];
}
cout<<"\n\nAvg. Wait time = "<<sum/num<<endl<<endl;
}
Output:
6.Priority – Non Preemptive
#include <bits/stdc++.h>
using namespace std;
#define totalprocess 5
struct process
{
int at,bt,pr,pno;
};
process proc[50];
bool comp(process a,process b)
{
if(a.at == b.at)
{
return a.pr<b.pr;
}
else
{
return a.at<b.at;
}
}
void get_wt_time(int wt[])
{
int service[50];
service[0] = proc[0].at;
wt[0]=0;
for(int i=1;i<totalprocess;i++)
{
service[i]=proc[i-1].bt+service[i-1];
wt[i]=service[i]-proc[i].at;
if(wt[i]<0)
{
wt[i]=0;
}
}
void get_tat_time(int tat[],int wt[])
{
for(int i=0;i<totalprocess;i++)
{
tat[i]=proc[i].bt+wt[i];
}
void findgc()
{
int wt[50],tat[50];
double wavg=0,tavg=0;
get_wt_time(wt);
get_tat_time(tat,wt);
int stime[50],ctime[50];
stime[0] = proc[0].at;
ctime[0]=stime[0]+tat[0];
for(int i=1;i<totalprocess;i++)
{
stime[i]=ctime[i-1];
ctime[i]=stime[i]+tat[i]-wt[i];
}
cout<<"Process\tArrival\tFinish\tTAT\tWait\n----------------------------------
-"<<endl;
for(int i=0;i<totalprocess;i++)
{
wavg += wt[i];
tavg += tat[i];
cout<<proc[i].pno<<"\t\t"<<
stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
tat[i]<<"\t\t\t"<<wt[i]<<endl;
}
cout<<"Average waiting time is : ";
cout<<wavg/(float)totalprocess<<endl;
cout<<"average turnaround time : ";
cout<<tavg/(float)totalprocess<<endl;
int main()
{
int num;
cout<<"Enter number of Process: ";
cin>>num;
int arrivaltime[num],bursttime[num],priority[num];
cout<<"\n\nProcess\tArrival\tBurst\tPriority\n------\t-------\t-----\t--------
\n";
int pro;
for(int i=0; i<num; i++)
{
cin>>pro>>arrivaltime[i]>>bursttime[i]>>priority[i];
}
for(int i=0;i<totalprocess;i++)
{
proc[i].at=arrivaltime[i];
proc[i].bt=bursttime[i];
proc[i].pr=priority[i];
proc[i].pno=i+1;
}
sort(proc,proc+totalprocess,comp);
findgc();
return 0;
}
Output: