0% found this document useful (0 votes)
33 views12 pages

Program To Implement Shortest Job First (Preemptive)

The document contains code implementing the Shortest Job First (preemptive) and Round Robin scheduling algorithms. For SJF, it sorts processes by burst time and calculates completion times, turnaround times, and waiting times. For RR, it implements a queue with a time quantum to cycle between processes, tracking burst times, completion times, turnaround times and waiting times.

Uploaded by

Mann Veer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Program To Implement Shortest Job First (Preemptive)

The document contains code implementing the Shortest Job First (preemptive) and Round Robin scheduling algorithms. For SJF, it sorts processes by burst time and calculates completion times, turnaround times, and waiting times. For RR, it implements a queue with a time quantum to cycle between processes, tracking burst times, completion times, turnaround times and waiting times.

Uploaded by

Mann Veer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Program to implement Shortest Job First (preemptive)

#include <iostream>

using namespace std;

void fix(int n,int ari[],int bt[],int p[])

int temp;

ari[0] = 0;

bt[0] = 0;

for(int i=0;i<n-1;i++)

for(int j=0;j<n;j++)

if(ari[j]>ari[j+1])

temp = ari[j];

ari[j] = ari[j+1];

ari[j+1] = temp;

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

temp = bt[j];

bt[j] = bt[j+1];

bt[j+1] = temp;
}

void sortbt(int n,int ari[],int bt[],int p[])

int temp;

for(int i=2;i<n;i++)

for(int j=2;j<n;j++)

if(bt[j]>bt[j+1])

temp = bt[j];

bt[j] = bt[j+1];

bt[j+1] = temp;

temp = ari[j];

ari[j] = ari[j+1];

ari[j+1] = temp;

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

}
}

void func(int n,int ari[],int bt[],int p[])

int ct[10],wt[10],tat[10];

ct[0] = 0;

wt[0] = 0;

tat[0] = 0;

ct[0+1] = ct[0] + bt[0+1];

tat[0+1] = ct[0+1] - ari[0+1];

wt[0+1] = tat[0+1] - bt[0+1];

sortbt(n,ari,bt,p);

cout<<"\n\npno.\tAt\tBt\tCt\tTat\tWt\n";

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

ct[i+2] = ct[i+1] + bt[i+2];

tat[i+2] = ct[i+2] - ari[i+2];

wt[i+2] = tat[i+2] - bt[i+2];


cout<<p[i+1]<<"\t"<<ari[i+1]<<"\t"<<bt[i+1]<<"\t"<<ct[i+1]<<"\t"<<tat[i+1]<<"\t"<<w
t[i+1]<<"\n";

}
int main()

int n,p[10],ari[10],bt[10];

ari[0] = 0;

bt[0] = 0;

cout<<"enter number of processes:";

cin>>n;

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

p[i+1]=i;

cout<<"enter burst time:";

cin>>bt[i+1];

cout<<"enter arrival time:";

cin>>ari[i+1];

fix(n,ari,bt,p);

func(n,ari,bt,p);

return 0;

}
PROGRAM TO IMPLEMENT ROUND ROBIN
#include <iostream>

#include<conio.h>

using namespace std;

void searchStack01(int pnt,int tm,int n);

void searchStack02(int pnt,int tm,int n);

void addqueue(int pnt);

int
at[50],bt[50],ct[50]={0},qt,rqi[50]={0},c=0,st,flg=0,tm=0,noe=0,pnt=0,btm[50]={
0},tt,wt;

float att,awt;

int main()

int n;

cout<<"enter number of processes:";

cin>>n;

cout<<"round robin algo:input 5 processes\n";

cout<<"\n\t\there some attrib use in program\n";

cout<<"At = arrival time\nBt = burst time\nTT = turn arround time\nWt


= waiting time\n\n";

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

cout<<"\nprocess"<<i+1;
cout<<"\nAt = ";

cin>>at[i];

cout<<"Bt = ";

cin>>bt[i];

btm[i]=bt[i];

cout<<"enter time quamtum:";

cin>>qt;

cout<<"\nGANTT chart\n"<<at[0];

do

if(flg==0)

st=at[0];

if(btm[0]<=qt)

tm=st+btm[0];

btm[0]=0;

searchStack01(pnt,tm,n);

else

{
btm[0]=btm[0]-qt;

tm=st+qt;

searchStack01(pnt,tm,n);

addqueue(pnt);

else

pnt=rqi[0]-1;

st=tm;

for(int x=0;x<noe;x++)

rqi[x]=rqi[x+1];

noe--;

if(btm[pnt]<=qt)

tm=st+btm[pnt];

btm[pnt]=0;

searchStack02(pnt,tm,n);

else
{

btm[pnt]=btm[pnt]-qt;

tm=st+qt;

searchStack02(pnt,tm,n);

addqueue(pnt);

if(btm[pnt]==0)

ct[pnt]=tm;

flg++;

cout<<"]-p"<<pnt+1<<"-["<<tm;

} while(noe!=0);

cout<<"\n\nProcess\tAT\tBT\tCT\tTT\tWT\n";

for(int x=0;x<n;x++)

tt=ct[x]-at[x];

wt=tt-bt[x];
cout<<"p"<<x+1<<"\t"<<at[x]<<"\t"<<bt[x]<<"\t"<<ct[x]<<"\t"<<tt<<"\t"<<wt<
<"\n";

awt=awt+wt;

att=att+tt;

cout<<"\naverage tt:"<<att/n;

cout<<"\naverage wt:"<<awt/n;

return 0;

void searchStack01(int pnt,int tm,int n)

for(int x=pnt+1;x<n;x++)

if(at[x]<=tm)

rqi[noe]=x+1;

noe++;

}
void searchStack02(int pnt,int tm,int n)

for(int x=pnt+1;x<n;x++)

int fl=0;

for(int y=0;y<noe;y++)

if(rqi[y]==x+1)

fl++;

if(at[x]<=tm&&fl==0&&btm[x]!=0)

rqi[noe]=x+1;

noe++;

void addqueue(int pnt)


{

rqi[noe]=pnt+1;

noe++;

You might also like