Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#include <stdio.

h>
#include <sys/types.h>
#include <unistd.h>
#include<iostream>
#include<cstdlib>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
using namespace std;
struct wt
{
double fcfs;
double sjf;
double priority;
double rr;
};

struct process
{
int id[10];
int bTime[10];
int aTime[10];
int priority[10];
int qSize[10];
int tAround[10];
int timeline[10];
};

struct chart
{
int id[20];
int bTime[20];
int timeline[20];
int tAround[20];
};

void gChart(chart p, int c)


{
int i, j;

//top bar
cout<<" ";
for(i=0; i<c; i++) {
for(j=0; j<p.bTime[i]; j++) cout<<"--";
cout<<" ";
}
cout<<"\n|";

// printing process id
for(i=0; i<c; i++) {
for(j=0; j<p.bTime[i] - 1; j++) cout<<" ";
cout<<"P"<< p.id[i];
for(j=0; j<p.bTime[i] - 1; j++) cout<<" ";
cout<<"|";
}
cout<<"\n ";
//bottom bar
for(i=0; i<c; i++) {
for(j=0; j<p.bTime[i]; j++) cout<<"--";
cout<<" ";
}
cout<<"\n";

//time line
cout<<"0";
for(i=0; i<c; i++) {
for(j=0; j<p.bTime[i]; j++) cout<<" ";
if(p.tAround[i] > 9) cout<<"\b";
cout<<p.timeline[i];

}
cout<<endl;
}

void fsfc(process q, int c)


{
if (fork()==0){
int temp;
int finish_time=0;
int waiting_time=0;
chart chrt;
double total_waiting_time=0;
//FSFC sorting - According to Arrival Time
for(int i=0;i<c;i++)
{
for(int j=i+1;j<c;j++)
{
if(q.aTime[i]>q.aTime[j])
{
temp =q.aTime[i];
q.aTime[i]=q.aTime[j];
q.aTime[j]=temp;

temp =q.id[i];
q.id[i]=q.id[j];
q.id[j]=temp;

temp =q.bTime[i];
q.bTime[i]=q.bTime[j];
q.bTime[j]=temp;

temp =q.priority[i];
q.priority[i]=q.priority[j];
q.priority[j]=temp;

temp =q.qSize[i];
q.qSize[i]=q.qSize[j];
q.qSize[j]=temp;
}
}
}
for(int i=0; i<c; i++)
{
finish_time = finish_time + q.bTime[i];
q.tAround[i] = finish_time - q.aTime[i];
waiting_time = q.tAround[i] - q.bTime[i];
chrt.tAround[i] =q.tAround[i];
chrt.timeline[i]=finish_time;
chrt.bTime[i]=q.bTime[i];
chrt.id[i]=q.id[i];
total_waiting_time += waiting_time;
}
cout<<"\n\t\t GHANTT Chart for FCFS Algorithm\n";

gChart(chrt,c);
int id= shmget(123,sizeof(wt),0666|IPC_CREAT);
wt *b=(wt *)shmat(id,0,0);
b->fcfs=total_waiting_time/c;
shmdt(&id);
exit(0);}
};
void sjf(process q, int c)
{
if (fork()==0){
int temp[10];
int seq[20];
chart chrt;
int i, smallest,temps=99,tempb=0, count = 0, time, limit=c,s=0;
double wTime = 0,twTime=0, turnaround_time = 0, end;
double average_waiting_time;
q.bTime[9] = 9999;
for(int i=0;i<c;i++){
temp[i]=q.bTime[i];
}

for(time = 0; count < limit; time++)


{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(q.aTime[i] <= time && q.bTime[i] < q.bTime[smallest] && q.bTime[i] > 0)
{

smallest = i;
}
}

q.bTime[smallest]--;
tempb++;
if(temps!=smallest)
{
chrt.id[s]=q.id[smallest];
chrt.bTime[s]=tempb;
tempb=0;
if(s!=0)
{
chrt.timeline[s-1]=time;
}
s++;
}
if(q.bTime[smallest] == 0)
{
count++;
end = time + 1;
chrt.tAround[s]=turnaround_time;
wTime = end - q.aTime[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - q.aTime[smallest];
twTime=twTime+wTime;

}
temps=smallest;
}

chrt.timeline[s-1]=time;
average_waiting_time = wTime / limit;
cout<<"\n\t\t GHANTT Chart for SJF Algorithm\n";
gChart(chrt,s);
int id= shmget(123,sizeof(wt),0666|IPC_CREAT);
wt *b=(wt *)shmat(id,0,0);
b->sjf=average_waiting_time;
shmdt(&id);
exit(0);}

};

void priorityBased(process q, int c)


{
if (fork()==0){
int temp[10];
chart chrt;
int i, smallest,temps=99,tempb=0,s=0, count = 0, time, limit=c;
double wTime = 0,twTime=0, turnaround_time = 0, end;
double average_waiting_time;
q.bTime[9] = 9999;
q.priority[9] = 9;
for(int i=0;i<c;i++){
temp[i]=q.bTime[i];
}
int in=0;
for(time = 0; count < limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(q.aTime[i] <= time && q.priority[i] < q.priority[smallest] && q.bTime[i] > 0)
{
smallest = i;
}
}
q.bTime[smallest]--;
tempb++;
if(temps!=smallest)
{
chrt.id[s]=q.id[smallest];
chrt.bTime[s]=tempb;
tempb=0;
if(s!=0)
{
chrt.timeline[s-1]=time;
}
s++;
}
if(q.bTime[smallest] == 0)
{
count++;
end = time + 1;
chrt.tAround[in]=turnaround_time;
wTime = end - q.aTime[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - q.aTime[smallest];
twTime=twTime+wTime;

}
temps=smallest;
}

chrt.timeline[s-1]=time;
average_waiting_time = twTime / c;
cout<<"\n\t\t GHANTT Chart for Priority Based Algorithm\n";

gChart(chrt,s);
int id= shmget(123,sizeof(wt),0666|IPC_CREAT);
wt *b=(wt *)shmat(id,0,0);
b->priority=average_waiting_time;
shmdt(&id);
exit(0);}

};
void roundRobin(process q, int c)
{
if (fork()==0){
int temp=0,time=0;
int seq[20]={0}, tempb[10];
int wTime=0;
double twTime=0;
chart chrt;
//RR sorting - According to Arrival Time
for(int i=0;i<c;i++)
{
for(int j=i+1;j<c;j++)
{
if(q.aTime[i]>q.aTime[j])
{
temp =q.aTime[i];
q.aTime[i]=q.aTime[j];
q.aTime[j]=temp;

temp =q.id[i];
q.id[i]=q.id[j];
q.id[j]=temp;

temp =q.bTime[i];
q.bTime[i]=q.bTime[j];
q.bTime[j]=temp;

temp =q.qSize[i];
q.qSize[i]=q.qSize[j];
q.qSize[j]=temp;
}
}
}
temp=0;
for(int h=0;h<c;h++){
tempb[h]=q.bTime[h];
}
int count=0;
int d=0,s=0;
for(int l=0;temp<c;l++)
{
for(int o=0;o<c;o++){
if(l==q.aTime[o])
{
seq[s]=o;
s++;
}
}

if(tempb[seq[d]]>q.qSize[0] && count==q.qSize[0])


{
tempb[seq[d]]=tempb[seq[d]]-q.qSize[0];
chrt.id[d]=seq[d];
chrt.bTime[d]=q.qSize[0];
chrt.timeline[d]=l;
chrt.tAround[d]=l-q.aTime[seq[d]];
seq[s]=seq[d];
d++;
s++;
count=0;
}

if(tempb[seq[d]]<=q.qSize[0] && count==tempb[seq[d]])


{
chrt.id[d]=seq[d];
chrt.bTime[d]=tempb[seq[d]];
chrt.timeline[d]=l;
chrt.tAround[d]=l-q.aTime[seq[d]];
tempb[seq[d]]=0;
wTime=l-q.aTime[seq[d]]-q.bTime[seq[d]];
temp++;
twTime=twTime+wTime;
d++;
s++;
count=0;

}
count++;

}
cout<<"\n\t\t GHANTT Chart for Roundrobin Algorithm\n";
gChart(chrt,d-1);
int id= shmget(123,sizeof(wt),0666|IPC_CREAT);
wt *b=(wt *)shmat(id,0,0);
b->rr=twTime/c;
shmdt(&id);
exit(0);}
};

int main()
{
int count;
int avgs[4];
char prog[4];
cout<<"Enter No. of tasks: "<<endl;
cin>>count;
process q;

wt *a;
int id= shmget(123,sizeof(wt),0666|IPC_CREAT);
a=(wt *)shmat(id,0,0);

for(int i=0; i<count; i++)


{ cout<<"\nEnter process id:"<<endl;
cin>>q.id[i];
cout<<"Enter burst time:"<<endl;
cin>>q.bTime[i];
cout<<"Enter arrival time:"<<endl;
cin>>q.aTime[i];
cout<<"Enter Process Priority:"<<endl;
cin>>q.priority[i];
cout<<"Enter quantum size:"<<endl;
cin>>q.qSize[i];
}
fsfc(q,count);
wait(NULL);
cout<<"Avg of fcfs:"<<a->fcfs<<endl;
avgs[0]=a->fcfs;
prog[0]='F';

sjf(q,count);
wait(NULL);
cout<<"Avg of sjf:"<<a->sjf<<endl;
avgs[1]=a->sjf;
prog[1]='S';

priorityBased(q,count);
wait(NULL);
cout<<"Avg of pb:"<<a->priority<<endl;
avgs[2]=a->priority;
prog[2]='P';

roundRobin(q,count);
wait(NULL);
cout<<"Avg of Round Robin:"<<a->rr<<endl;
avgs[3]=a->rr;
prog[3]='R';

int temp;
char temp2;
for(int i=0;i<count;i++)
{
for(int j=i+1;j<count;j++)
{
if(avgs[i]>avgs[j])
{
temp =avgs[i];
avgs[i]=avgs[j];
avgs[j]=temp;

temp2 =prog[i];
prog[i]=prog[j];
prog[j]=temp2;

}
}
}

cout<<"\n\t\tAlgorithm With Least Waiting Time"<<endl;


for(int i=0;i<4;i++)
{
cout<<prog[i]<<" : "<<avgs[i]<<endl;
}

shmdt(&id);
shmctl(id, IPC_RMID,NULL);
return 0;
}

You might also like