OSLAB2
OSLAB2
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];
};
//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;
}
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];
}
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);}
};
}
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++;
}
}
}
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);
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;
}
}
}
shmdt(&id);
shmctl(id, IPC_RMID,NULL);
return 0;
}