CPU Scheduling and Ram Management Part 1

CPU Scheduling and Ram Management 32

CPUScheduling and Ram Management

Part1

#include&ltiostream&gt

#include&ltstring&gt

usingnamespace std

classcpuschedule

{

intn,Bu[20]

floatTwt,Awt,A[10],Wt[10],w

public:

//obtainingthe No of processes &amp burst time

voidGetdata()

//Firstcome First served Algorithm

voidFcfs()

//Shortestjob First Algorithm

voidSjf()

//Shortestjob First Algorithm with Preemption

voidSjfP()

//Shortestjob First Algorithm with NonPreemption

voidSjfNp()

//RoundRobin Algorithm

voidRoundRobin()

//PriorityAlgorithm

voidPriority()

}

//Implementation file for Cpu scheduling

//Gettingno of processes and Burst time

voidcpuschedule::Getdata()

{

inti

cout&lt&lt&quotEnterthe no of processes:&quot

cin&gt&gtn

for(i=1i&lt=ni++)

{

cout&lt&lt&quotEnterThe BurstTime for Process p&quot&lt&lti&lt&lt&quot= &quot

cin&gt&gtBu[i]

}

}

//Firstcome First served Algorithm

voidcpuschedule::Fcfs()

{

inti,B[10]

Twt=0.0

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process p&quot&lt&lti&lt&lt&quot= &quot

cout&lt&ltB[i]

}

Wt[1]=0

for(i=2i&lt=ni++)

{

Wt[i]=B[i-1]+Wt[i-1]

}

//CalculatingAverage Weighting Time

for(i=1i&lt=ni++)

Twt=Twt+Wt[i]

Awt=Twt/n

cout&lt&lt&quotTotal Weighting Time=&quot&lt&ltTwt

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt&lt&lt&quot&quot

}

//Shortestjob First Algorithm

voidcpuschedule::Sjf()

{

inti,j,temp,B[10]

Twt=0.0

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process p&quot&lt&lti&lt&lt&quot= &quot

cout&lt&ltB[i]

}

for(i=ni&gt=1i–)

{

for(j=1j&lt=nj++)

{

if(B[j-1]&gtB[j])

{

temp=B[j-1]

B[j-1]=B[j]

B[j]=temp

}

}

}

Wt[1]=0

for(i=2i&lt=ni++)

{

Wt[i]=B[i-1]+Wt[i-1]

}

//calculatingAverage Weighting Time

for(i=1i&lt=ni++)

Twt=Twt+Wt[i]

Awt=Twt/n

cout&lt&lt&quotTotal Weighting Time=&quot&lt&ltTwt

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt&lt&lt&quot&quot

}

//Shortestjob First Algorithm with NonPreemption

voidcpuschedule::SjfNp()

{

inti,B[10],Tt=0,temp,j

charS[10]

floatA[10],temp1,t

Twt=0.0

w=0.0

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process p&quot&lt&lti&lt&lt&quot= &quot

cout&lt&ltB[i]

S[i]=`T`

Tt=Tt+B[i]

cout&lt&lt&quotEnterthe Arrival Time for&quot&lt&lti&lt&lt&quotth process= &quot

cin&gt&gtA[i]

}

for(i=ni&gt=1i–)

{

for(j=3j&lt=nj++)

{

if(B[j-1]&gtB[j])

{

temp=B[j-1]

temp1=A[j-1]

B[j-1]=B[j]

A[j-1]=A[j]

B[j]=temp

A[j]=temp1

}

}

}

for(i=1i&lt=ni++)

{

cout&lt&lt&quotp&quot&lt&lti&lt&lt&quot &quot&lt&ltB[i]&lt&lt&quot &quot&lt&ltA[i]

}

//Forthe 1st process

Wt[1]=0

w=w+B[1]

t=w

S[1]=`F`

while(w&ltTt)

{

i=2

while(i&lt=n)

{

if(S[i]==`T`&amp&ampA[i]&lt=t)

{

Wt[i]=w

cout&lt&lt&quotWT&quot&lt&lti&lt&lt&quot=&quot&lt&ltWt[i]

S[i]=`F`

w=w+B[i]

t=w

i=2

}

else

i++

}

}

for(i=1i&lt=ni++)

cout&lt&lt&quotWt&quot&lt&lti&lt&lt&quot==&quot&lt&ltWt[i]

//calculatingaverage weighting Time

for(i=1i&lt=ni++)

Twt=Twt+(Wt[i]-A[i])

Awt=Twt/n

cout&lt&lt&quotTotal Weighting Time=&quot&lt&ltTwt&lt&lt&quot&quot

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt&lt&lt&quot&quot

}

//PriorityAlgorithm

voidcpuschedule::Priority()

{

inti,B[10],P[10],j

w=0.0

intmax

Twt=0.0

max=1

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process p&quot&lt&lti&lt&lt&quot= &quot

cout&lt&ltB[i]

cout&lt&lt&quotEnterthe priority for process P&quot&lt&lti&lt&lt&quot= &quot

cin&gt&gtP[i]

if(max&ltP[i])

max=P[i]

}

j=1

while(j&lt=max)

{

i=1

while(i&lt=n)

{

if(P[i]==j)

{

Wt[i]=w

w=w+B[i]

}

i++

}

j++

}

//calculatingaverage weighting Time

for(i=1i&lt=ni++)

Twt=Twt+Wt[i]

Awt=Twt/n

cout&lt&lt&quotTotal Weighting Time=&quot&lt&ltTwt&lt&lt&quot&quot

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt&lt&lt&quot&quot

}

//Shortestjob First Algorithm with Preemption

voidcpuschedule::SjfP()

{

inti,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp

charS[20],start[20]

intmax=0,Time=0,min

floatTwt=0.0,Awt

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process P&quot&lt&lti&lt&lt&quot= &quot&lt&ltB[i]

if(B[i]&gtmax)

max=B[i]

Wt[i]=0

S[i]=`T`

start[i]=`F`

Tt=Tt+B[i]

cout&lt&lt&quotEnterthe Arrival Time for&quot&lt&lti&lt&lt&quotth process= &quot

cin&gt&gtA[i]

if(A[i]&gtTime)

Time=A[i]

}

cout&lt&lt&quotMax=&quot&lt&ltmax

intw=0,flag=0,t=0

i=1

while(t&ltTime)

{

if(A[i]&lt=t&amp&amp B[i]!=0)

{

if(flag==0)

{

Wt[i]=Wt[i]+w

cout&lt&lt&quotWt[&quot&lt&lti&lt&lt&quot]=&quot&lt&ltWt[i]

}

B[i]=B[i]-1

if(B[i]==0)

S[i]=`F`

start[i]=`T`

t++

w=w+1

if(S[i]!=`F`)

{

j=1flag=1

while(j&lt=n&amp&amp flag!=0)

{

if(S[j]!=`F`&amp&amp B[i]&gtB[j] &amp&amp A[j]&lt=t &amp&amp i!=j )

{

flag=0

Wt[i]=Wt[i]-w

i=j

}

else

{

flag=1

}

j++

}

}

else

{

i++

j=1

while(A[j]&lt=t&amp&ampj&lt=n)

{

if(B[i]&gtB[j]&amp&amp S[j]!=`F`)

{

flag=0

i=j

}

j++

}

}

}

else

if(flag==0)

i++

}

cout&lt&lt&quotPrintingremaining burst time&quot

for(i=1i&lt=ni++)

cout&lt&lt&quotB[&quot&lt&lti&lt&lt&quot]=&quot&lt&ltB[i]

cout&lt&lt&quot&quot

while(w&ltTt)

{

min=max+1

i=1

while(i&lt=n)

{

if(min&gtB[i]&amp&amp S[i]==`T`)

{

min=B[i]

j=i

}

i++

}

i=j

if(w==Time&amp&amp start[i]==`T`)

{

w=w+B[i]

S[i]=`F`

}

else

{

Wt[i]=Wt[i]+w

w=w+B[i]

S[i]=`F`

}

}

cout&lt&lt&quotWeightinfo&quot

for(i=1i&lt=ni++)

cout&lt&lt&quotWT[&quot&lt&lti&lt&lt&quot]=&quot&lt&ltWt[i]

cout&lt&lt&quotaftersubtracting arrival time&quot

for(i=1i&lt=ni++)

{

Wt[i]=Wt[i]-A[i]

cout&lt&lt&quotWT[&quot&lt&lti&lt&lt&quot]=&quot&lt&ltWt[i]

}

//CalculatingAverage Weighting time

for(i=1i&lt=ni++)

Twt=Twt+Wt[i]

Awt=Twt/n

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt

}

//RoundRobin Algorithm

voidcpuschedule::RoundRobin()

{

inti,j,tq,k,B[10],Rrobin[10][10],count[10]

intmax=0

intm

Twt=0.0

for(i=1i&lt=ni++)

{

B[i]=Bu[i]

cout&lt&lt&quotBursttime for process p&quot&lt&lti&lt&lt&quot= &quot

cout&lt&ltB[i]

if(max&ltB[i])

max=B[i]

Wt[i]=0

}

cout&lt&lt&quotEnterthe Time Quantum=&quot

cin&gt&gttq

//TOfind the dimension of the Rrobin array

m=max/tq+1

//initializingRrobin array

for(i=1i&lt=ni++)

{

for(j=1j&lt=mj++)

{

Rrobin[i][j]=0

}

}

//placingvalue in the Rrobin array

i=1

while(i&lt=n)

{

j=1

while(B[i]&gt0)

{

if(B[i]&gt=tq)

{

B[i]=B[i]-tq

Rrobin[i][j]=tq

j++

}

else

{

Rrobin[i][j]=B[i]

B[i]=0

j++

}

}

count[i]=j-1

i++

}

cout&lt&lt&quotDisplay&quot

for(i=1i&lt=ni++)

{

for(j=1j&lt=mj++)

{

cout&lt&lt&quotRr[&quot&lt&lti&lt&lt&quot,&quot&lt&ltj&lt&lt&quot]=&quot&lt&ltRrobin[i][j]

cout&lt&lt&quot &quot

}

cout&lt&lt&quot&quot

}

//calculatingweighting time

intx=1

i=1

while(x&lt=n)

{

for(inta=1a&ltxa++)

{

Wt[x]=Wt[x]+Rrobin[a][i]

}

i=1

intz=x

j=count[z]

k=1

while(k&lt=j-1)

{

if(i==n+1)

{

i=1

k++

}

else

{

if(i!=z)

{

Wt[z]=Wt[z]+Rrobin[i][k]

}

i++

}

}

x++

}

for(i=1i&lt=ni++)

cout&lt&lt&quotWeightingTime for process P&quot&lt&lti&lt&lt&quot=&quot&lt&ltWt[i]

//calculatingAverage Weighting Time

for(i=1i&lt=ni++)

Twt=Twt+Wt[i]

Awt=Twt/n

cout&lt&lt&quotTotal Weighting Time=&quot&lt&ltTwt

cout&lt&lt&quotAverageWeighting Time=&quot&lt&ltAwt&lt&lt&quot&quot

}

//Applicationfile for cpu Scheduling

intmain ()

{

intch,cho

cpuschedulec

cout&lt&lt&quotStudentname:&quot&lt&ltendl

cout&lt&lt&quot&quot&lt&ltendl

cout&lt&lt&quotSec:&quot&lt&ltendl

do

{

cout&lt&lt&quot MENU&quot&lt&ltendl

cout&lt&lt&quot1.GettingBurstTime&quot&lt&ltendl

cout&lt&lt&quot2.FirstComeFirstServed&quot&lt&ltendl

cout&lt&lt&quot3.ShortestJobFirst&quot&lt&ltendl

cout&lt&lt&quot4.RoundRobin&quot&lt&ltendl

cout&lt&lt&quot5.Priority&quot&lt&ltendl

cout&lt&lt&quot6.EXIT&quot&lt&ltendl

cout&lt&lt&quotEnteryour choice&quot&lt&ltendl

cin&gt&gtch

switch(ch)

{

case1:

c.Getdata()

break

case2:

cout&lt&lt&quotFIRSTCOME FIRST SERVED SCHEDULING&quot

c.Fcfs()

break

case3:

cout&lt&lt&quotSHORTESTJOB FIRST SCHEDULING&quot

do

{

cout&lt&lt&quot1.SJF-Normel&quot

cout&lt&lt&quot2.SJF-Preemptive&quot

cout&lt&lt&quot3.SJF-NonPreemptive&quot

cout&lt&lt&quotEnteryour choice&quot

cin&gt&gtcho

switch(cho)

{

case1:

c.Sjf()

break

case2:

c.SjfP()

break

case3:

c.SjfNp()

break

}

}while(cho&lt=3)

break

case4:

cout&lt&lt&quotROUNDROBIN SCHEDULING&quot

c.RoundRobin()

break

case5:

cout&lt&lt&quotPRIORITYSCHEDULING&quot

c.Priority()

break

case6:

break

}

}while(ch&lt=5)

}

Outputfile

Part2

Output

Reference

Garrido.(2000).&nbspPerformancemodeling of operating systems using object-oriented simulation apractical introduction.New York: Kluwer Academic /Plenum Publishers.