0% found this document useful (0 votes)
75 views

OS Lab Programs

The document discusses several algorithms related to memory management and concurrency control. It includes implementations of producer consumer problem using semaphores, Banker's algorithm, file locking, LRU and FIFO page replacement algorithms, and memory allocation with pages. Various data structures and functions are used to implement the different algorithms.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

OS Lab Programs

The document discusses several algorithms related to memory management and concurrency control. It includes implementations of producer consumer problem using semaphores, Banker's algorithm, file locking, LRU and FIFO page replacement algorithms, and memory allocation with pages. Various data structures and functions are used to implement the different algorithms.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Producer Consumer Problem using Semaphore

#include<stdio.h>

#include<sys/types.h>

#include<sys/sem.h>

#include<stdlib.h>

#include<sys/shm.h>

#include<errno.h>

#include<sys/ipc.h>

void sem_acq(int);

void sem_rel(int);

int main()

{ int mutex,empty,full,shmid, n;

pid_t ret;

int in=-1,out=-1;

char *buffer;

char c[2];

printf(“\nProducer Consumer Problem using Semaphore\n―);

printf(“\nEnter the size for buffer:―);

scanf(“%d―, &n);

if((mutex=semget(IPC_PRIVATE,1,0666|IPC_CREAT))==-1) {

perror(“\nFailed to create semaphore.―);

exit(0);

if((semctl(mutex,0,SETVAL,1))==-1) {

perror(“\nFailed to set value for the semaphore.―);


exit(0);

if((empty=semget(IPC_PRIVATE,1,0666|IPC_CREAT))==-1) {

perror(“\nFailed to create semaphore.―);

exit(0);

if((semctl(empty,0,SETVAL,n))==-1) {

perror(“\nFailed to set value for semaphore.―);

exit(0);

if((full=semget(IPC_PRIVATE,1,0666|IPC_CREAT))==-1) {

perror(“\nFailed to create semaphore.―);

exit(0);

if((semctl(full,0,SETVAL,0))==-1) {

perror(“\nFailed to set value for the semaphore.―);

exit(0);

if((shmid=shmget(IPC_PRIVATE,n*sizeof(char),0666|IPC_CREAT))==-1) {

perror(“\nFailed to allocate shared memory.―);

exit(0);

buffer=(char *)shmat(shmid,(const void *)0,0);

ret=fork();

while(1) {
if(ret==0) //Producer

sem_acq(empty);

printf(“\nItem produced: “);

scanf(“%s―,c);

sem_acq(mutex);

in = (in+1)%n;

buffer[in]=c[0];

sem_rel(mutex);

sem_rel(full);

else if(ret>0) //Consumer

sleep(5);

sem_acq(full);

sem_acq(mutex);

out = (out+1)%n;

c[0]=buffer[out];

printf(“\nItem consumed: %c―,c[0]);

sem_rel(mutex);

sem_rel(empty);

void sem_acq(int semid)


{

struct sembuf sb;

sb.sem_num=0;

sb.sem_op=1;

sb.sem_flg=0;

if((semop(semid,&sb,1))==-1) {

perror(“\nFailed to acquire semaphore.―);

exit(0);

void sem_rel(int semid)

struct sembuf sb;

sb.sem_num=0;

sb.sem_op=1;

sb.sem_flg=0;

if((semop(semid,&sb,1))==-1) {

perror(“\nFailed to release semaphore.―);

exit(0);

Implementation of Banker’s Algorithm using C++

// Banker’s algorithm
#include<iostream.h>

#include<conio.h>

#define MAX 20

class bankers

private:

int al[MAX][MAX],m[MAX][MAX],n[MAX][MAX],avail[MAX];

int nop,nor,k,result[MAX],pnum,work[MAX],finish[MAX];

public:

bankers();

void input();

void method();

int search(int);

void display();

};

bankers::bankers()

k=0;

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

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

al[i][j]=0;

m[i][j]=0;

n[i][j]=0;
}

avail[i]=0;

result[i]=0;

finish[i]=0;

void bankers::input()

int i,j;

cout<<“Enter the number of processes:―;

cin>>nop;

cout<<“Enter the number of resources:―;

cin>>nor;

cout<<“Enter the allocated resources for each process: “<<endl;

for(i=0;i<nop;i++)

cout<<“\nProcess “<<i;

for(j=0;j<nor;j++)

cout<<“\nResource “<<j<<“:―;

cin>>al[i][j];

cout<<“Enter the maximum resources that are needed for each process: “<<endl;

for(i=0;i<nop;i++)
{

cout<<“\nProcess “<<i;

for(j=0;j<nor;j++)

cout<<“\nResouce “<<j<<“:―;

cin>>m[i][j];

n[i][j]=m[i][j]-al[i][j];

cout<<“Enter the currently available resources in the system: “;

for(j=0;j<nor;j++)

cout<<“Resource “<<j<<“:―;

cin>>avail[j];

work[j]=-1;

for(i=0;i<nop;i++)

finish[i]=0;

void bankers::method()

int i=0,j,flag;

while(1)

if(finish[i]==0)
{

pnum =search(i);

if(pnum!=-1)

result[k++]=i;

finish[i]=1;

for(j=0;j<nor;j++)

avail[j]=avail[j]+al[i][j];

i++;

if(i==nop)

flag=0;

for(j=0;j<nor;j++)

if(avail[j]!=work[j])

flag=1;

for(j=0;j<nor;j++)

work[j]=avail[j];

if(flag==0)

break;

else

i=0;
}

int bankers::search(int i)

int j;

for(j=0;j<nor;j++)

if(n[i][j]>avail[j])

return -1;

return 0;

void bankers::display()

int i,j;

cout<<endl<<“OUTPUT:―;

cout<<endl<<“========―;

cout<<endl<<“PROCESS\t ALLOTED\t MAXIMUM\t NEED―;

for(i=0;i<nop;i++)

cout<<“\nP―<<i+1<<“\t “;

for(j=0;j<nor;j++)

cout<<al[i][j]<<― “;

cout<<“\t “;
for (j=0;j<nor;j++)

cout<<m[i][j]<<― “;

cout<<“\t “;

for(j=0;j<nor;j++ )

cout<<n[i][j]<<― “;

cout<<“\nThe sequence of the safe processes are: \n―;

for(i=0;i<k;i++)

int temp = result[i]+1 ;

cout<<“P―<<temp<<― “;

cout<<“\nThe sequence of unsafe processes are: \n―;

int flg=0;

for (i=0;i<nop;i++)

if(finish[i]==0)

flg=1;

cout<<“P―<<i<<― “;
}

cout<<endl<<“RESULT:―;

cout<<endl<<“=======―;

if(flg==1)

cout<<endl<<“The system is not in safe state and deadlock may occur!!―;

else

cout<<endl<<“The system is in safe state and deadlock will not occur!!―;

int main()

clrscr();

cout<<― DEADLOCK BANKER’S ALGORITHM “<<endl;

bankers B;

B.input ( );

B.method ( );

B.display ( );

getch ( );

Program for File Locking

/* File Locking */

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>
#include <fcntl.h>

#include <unistd.h>

int main(int argc, char *argv[])

/* l_type l_whence l_start l_len l_pid */

struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };

int fd;

fl.l_pid = getpid();

if (argc > 1)

fl.l_type = F_RDLCK;

if ((fd = open(“lockdemo.c―, O_RDWR)) == -1)

perror(“open―);

exit(1);

printf(“Press <RETURN> to try to get lock: “);

getchar();

printf(“Trying to get lock…―);

if (fcntl(fd, F_SETLKW, &fl) == -1)

perror(“fcntl―);

exit(1);

printf(“got lock\n―);

printf(“Press <RETURN> to release lock: “);


getchar();

fl.l_type = F_UNLCK; /* set to unlock same region */

if (fcntl(fd, F_SETLK, &fl) == -1)

perror(“fcntl―);

exit(1);

printf(“Unlocked.\n―);

close(fd);

Implementation of LRU Page Replacement Algorithm using C++

//LRU Page Replacement Algorithm

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

#include<stdlib.h>

#define max 100

class lrupage

private:

int frame[10], count[10], cstr[max];

int tot,nof,fault;

public:
lrupage(){fault=0;}

void getdata();

void push();

void dis();

};

void lrupage::getdata()

int pno,i=0;

cout<<“\n\t\tL R U – Page Replacement Algorithm\n―;

cout<<“\nEnter No. of Page Frames:―;

cin>>nof;

cout<<“\nEnter the Context String:(Press -1 to end)\n―;

do

cin>>pno;

cstr[i++]=pno;

}while(pno!=-1);

tot=i-1;

void lrupage::push()

int x,i,flag=0,nc=0,maximum,maxpos=-1,mark=0;

for(i=0;i<nof;i++)

frame[i]=-1;
count[i]=mark–;

for(i=0;i<tot;i++)

flag=0;

x=cstr[i];

nc++;

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

for(int k=0; k<nof;k++)

count[k]++;

if(frame[j]==x)

flag=1;

count[j]=1;

break;

if(flag==0)

maximum = 0;

for(int k=0;k<nof;k++)

if(count[k]>maximum && nc>nof)

{
maximum=count[k];

maxpos = k;

if(nc>nof)

frame[maxpos]=x;

count[maxpos]=1;

else

frame[nc-1]=x;

fault++;

cout<<“\nThe Page Fault No. “<<fault<<endl;

dis();

getch();

cout<<“\nTotal Page Faults :―<<fault;

void lrupage::dis()

int i=0;

cout<<“\n—–\n―;

while(i<nof)

{
cout<<“| “<<frame[i]<<― “;

i++;

cout<<“|―;

void main()

lrupage lru;

clrscr();

lru.getdata();

lru.push();

getch();

Implementation of FIFO Page Replacement Algorithm using C++

//FIFO Page Replacement Algorithm

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

#include<stdlib.h>

#define max 100

class fifo

private:
int frame[10], mrstr[max];

int f,r,tot,nof,fault;

public:

fifo()

f=r=-1;

fault=0;

void getdata();

void push();

void pop();

void dis();

};

void fifo::getdata()

int pno,i=0;

cout<<”\n\t\tFIFO – Page Replacement Algorithm\n”;

cout<<”\nEnter the No. of Page Frames:”;

cin>>nof;

cout<<“\nEnter the memory reference string:(Press -1 to end)\n―;

do

cin>>pno;

mrstr[i++]=pno;

}while(pno!=-1);
tot=i-1;

void fifo::push()

int x,i,flag=0;//,fault=0;

for(i=0;i<nof;i++)

frame[i]=-1;

cout<<“\nThe Page Frames are:\tNo.of Page Faults―;

for(i=0;i<tot;i++)

x=mrstr[i];

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

if(frame[j]==x)

flag=1;

break;

else

flag=0;

if(flag==0)

if(r==nof-1)

{
++f;

r=++r%nof;

frame[r]=x;

else

frame[++r]=x;

fault++;

dis();

getch();

cout<<”\nTotal Page Faults :”<<fault;

void fifo::dis()

int i=0;

cout<<”\n----------------------------------------\n”;

while(i<nof)

cout<<”| “<<frame[i]<<” “;

i++;

cout<<”| “<<fault;

void main()
{

fifo fp;

clrscr();

fp.getdata();

fp.push();

getch();

Memory Allocation with Pages

/*Program For Memory Allocation with Pages*/

#include<iostream.h>

#include<conio.h>

void main()

int npage,pgsz,nframe,phmsz,lgmsz,i,j,k;

int frmpos[10],ptable[10];

char lgmdata[20],phmdata[20];

clrscr();

cout<<“Memory allocation with pages\n―;

cout<<“Enter the physical memory size:\n―;

cin>>phmsz;

cout<<“How many pages you want:―;

cin>>npage;

cout<<“Enter your page size:―;


cin>>pgsz;

lgmsz=npage*pgsz;

cout<<“Enter the logical address―;

for(i=0;i<phmsz;i++)

cin>>lgmdata[i];

for(i=0;i<lgmsz;lgmdata[i++]);

nframe=phmsz/pgsz;

cout<<“Select the frames to store pages between 0 to―<<nframe-1<<endl;

for(i=0;i<npage;++i)

cout<<“Enter the frame no. to store page―<<i<<“:―;

cin>>ptable[i];

for(i=0;i<npage;++i)

frmpos[i]=ptable[i]*pgsz;

for(i=0;i<phmsz;phmdata[i++]=’ ‘);

for(k=i=0;i<npage;++i)

for(j=frmpos[i];j<frmpos[i]+pgsz;++j,++k)

phmdata[j]=lgmdata[k];

cout<<“Data stored in pysical memory:―;

for(i=0;i<phmsz;++i)

cout<<― “<<i<<“-“<<phmdata[i]<<endl;

getch();

}
Implementation of Best Fit & First Fit Memory Management Algorithms

// Implementation of Best Fit & First Fit Algorithm

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

unsigned int n,j,i,size[10],m,x=0,t;

int cho=1,ch;

clrscr();

cout<<“\t\t STORAGE PLACEMENT STRATEGIES\n―;

cout<<“\tEnter the number of holes:―;

cin>>n;

for(i=1;i<=n;i++)

cout<<“\n Enter The Size Of Hole “<<i<<“:―;

cin>>size[i];

while(cho==1)

cout<<“\nEnter the size of the incoming program:―;

cin>>m;

cout<<“\nMenu―;

cout<<“\n1.First Fit Strategy \n2.Best Fit Strategy―;


cout<<“\n Enter your choice:―;

cin>>ch;

x=0;

switch(ch)

case 1:

for(i=1;i<=n;i++)

if(size[i]>=m)

cout<<“\nYour program is placed in hole “<<i;

size[i]-=m;

x=i;

break;

if(x==0) cout<<“There is no room for your program.―;

break;

case 2:

unsigned int temp=0,pos=0,t1;

if(m<=size[1])

temp=size[1]-m;

pos=1;

}
else

temp=size[1];

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

if(m<=size[i])

t1=size[i]-m;

if(temp>=t1)

temp=t1;

pos=i;

else temp=size[i];

if(pos==0)

cout<<“There is no room for your page.―;

else

size[pos]=size[pos]-m;

cout<<“Your program is palced in hole “<<pos;

break;

case 4:

return;
}

cout<<“\nFree Storage List―;

for(i=1;i<=n;i++)

cout<<“\nHole “<<i<<“\t\t―<<size[i];

cout<<“\n\nPress 1 to continue:―;

cin>>cho;

Simulation of Producer Consumer Problem

/* Producer Consumer problem */

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define max 5

class que

int v[max],c[max];

int f,r,g,cc,pc,n;

public:

que()

f=r=-1;

n=0;
g=0;

pc=0;

cc=0;

void push()

int x,ch2;

while(1)

cout<<“Enter the Value to Produce:―;

cin>>x;

if(f==-1)

f=0;

cc++;

v[++r]=x;

n++;

else if((f==0)&&(r==max-1)||f-r==1)

cout<<“\n Buffer is Full\n―;

else if((r==max-1)&&(f!=0))

r=-1;v[++r]=x;

n++;

cc++;
}

else

cc++;

v[++r]=x;

n++;

cout<<“Do U Want to Continue(1/2):―;

cin>>ch2;

if(ch2==2)

break;

void pop()

int i=0;

int ch1=1;

while(1)

if(n==0)

cout<<“\n Buffer is Empty\n―;

break;

if((f==max)&&(r!=max-1))
f=-1;

else

cout<<“\n Consumer Is Consuming The Items\n―;

pc++;

c[g++]=v[f++];

n–;

cout<<“\n Do U Want to Continue(1/2):\n―;

cin>>ch1;

if(ch1==2)

break;

void dis()

int i=0;

if(n==0)

cout<<“\nConsumer is in Idle No item to consume.\n―;

else

if(f<=r)

cout<<“\n The Buffer has:\n―;

for(i=f;i<=r;i++)

cout<<v[i]<<“\t―;
}

else

for(i=0;i<=r;i++)

cout<<v[i]<<“\t―;

for(i=f;i<max;i++)

cout<<v[i]<<“\t―;

};

void main()

que d;

int ch1;

char o,ch;

clrscr();

cout<<“\t\t\tPRODUCER CONSUMER PRObLEM\n―;

do

cout<<“\nTHE OPERATIONS ARE\n―;

cout<<“\n1.PRODUCER WRITE\n2.CONSUMER GET\n3.BUFFER\n4.Exit\n―;

cout<<“\n Enter ur OPERATION:―;

cin>>ch1;

switch(ch1)

{
case 1:

d.push();

break;

case 2:

d.pop();

break;

case 3:

d.dis();

break;

default:

cout<<“Exit.―;

exit(0);

}while(ch1!=4);

getch();

Implementation of Dekker’s Algorithm

//Dekker’s Algorithm

# include <stdio.h>

# include<pthread.h>

# include<cursors.h>

int turn=0, flag[2]={0,0},bal=0;

void longdelay()

{
long int t1,t2;

for(t1=0;t1<64000;t1++);

for(t2=0;t2<64000;t2++);

void shortdelay()

int t3,t4;

for(t3=0;t3<32000;t3++)

for(t4=0;t4<32000;t4++)

void test(int i)

int j,k,m,p,q,r,c;

j=1-i;

for(k=0;k<3;k++)

flag[i]=1;

while(flag[j])

if (turn==j)

flag[i]=0;

printf(“Iam waiting:%d―,i);

while(turn==j);

flag[i]=1;
}

longdelay();

printf(“Iam entering %d―,i);

c=bal;

/*critical section*/

printf(“\n process: %d―,l);

printf(“flag[%d]=%d, flag[%d]=%d―,i,flag[i],j,flag[j]);

printf(“turn=%d\n―,turn);

bal=c+1000;

printf(“the balance is %d―,bal);

turn=j;

flag[i]=0;

printf(“Iam exiting: %d―,i);

shortdelay();

int main()

pthread_t t1,t2;

pthread_creat(&t,NULL,(void*)&test,(void*)0);

printf(“After creation of first thread…..―);

pthread_creat(&t2,NULL,(void*)&test,(void*)1);

pthread_join(t1,NULL);

pthread_join(t2,NULL);
sleep(s);

printf(“Parent terminated:%d―,bal);

return(0);

Program for Banker’s Algorithm

/*Implementation of Banker’s Algorithm*/

#include<stdio.h>

#include<conio.h>

struct process

int all[6],max[6],need[6],finished,request[6];

}p[10];

int avail[6],sseq[10],ss=0,check1=0,check2=0,n,pid,work[6];

int nor,nori;

void main()

int safeseq(void);

int ch,i=0,j=0,k,pid,ch1;

int violationcheck=0,waitcheck=0;

do

clrscr();

printf(“\n\n\t 1. Input―);
printf(“\n\n\t 2. New Request―);

printf(“\n\n\t 3. Safe State or Not―);

printf(“\n\n\t 4. print―);

printf(“\n\n\t 5. Exit―);

printf(“\n\n\t Enter ur choice : “);

scanf(“%d―,&ch);

switch(ch)

case 1:

printf(“\n\n\t Enter number of processes : “);

scanf(“%d―,&n);

printf(“\n\n\t Enter the Number of Resources : “);

scanf(“%d―,&nor);

printf(“\n\n\t Enter the Available Resouces : “);

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

for(k=0;k<nor;k++)

if(j==0)

printf(“\n\n\t For Resource type %d : “,k);

scanf(“%d―,&avail[k]);

p[j].max[k]=0;

p[j].all[k]=0;
p[j].need[k]=0;

p[j].finished=0;

p[j].request[k]=0;

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

printf(“\n\n\t Enter Max and Allocated resources for P%d : “,i); for(j=0;j<nor;j++)

printf(“\n\n\t Enter the Max of resource %d : “,j);

scanf(“%d―,&p[i].max[j]);

printf(“\n\n\t Allocation of resource %d : “,j);

scanf(“%d―,&p[i].all[j]);

if(p[i].all[j]>p[i].max[j])

printf(“\n\n\t Allocation should be less < or == max―);

j–;

else

p[i].need[j]=p[i].max[j]-p[i].all[j];

avail[j]=avail[j]-p[i].all[j];

break;

case 2:
violationcheck=0;

waitcheck=0;

printf(“\n\n\t Requesting process id : “);

scanf(“%d―,&pid);

for(j=0;j<nor;j++)

printf(“\n\n\t Number of Request for resource %d : “,j);


scanf(“%d―,&p[pid].request[j]);

if(p[pid].request[j]>p[pid].need[j])

violationcheck=1;

if(p[pid].request[j]>avail[j])

waitcheck=1;

if (violationcheck==1)

printf(“\n\n\t The Process Exceeds it’s Max Need: Terminated―);

else if(waitcheck==1)

printf(“\n\n\t Lack of Resourcess : Process State – Wait―);

else

for(j=0;j<nor;j++)

avail[j]=avail[j]-p[pid].request[j];

p[pid].all[j]=p[pid].all[j]+p[pid].request[j];

p[pid].need[j]=p[pid].need[j]-p[pid].request[j];

ch1=safeseq();
if(ch1==0)

printf(“\n\n\t Granting leads to Unsafe state : “);


printf(“\n\n\t Request Denied “);

for(j=0;j<nor;j++)

avail[j]=avail[j]+p[pid].request[j];

p[pid].all[j]=p[pid].all[j]-p[pid].request[j];

p[pid].need[j]=p[pid].need[j]+p[pid].request[j];

else if(ch1==1)

printf(“\n\n\t Request Committed “);

break;

case 3:

if(safeseq()==1)

printf(“\n\n\t The System is in safe state “);

else

printf(“\n\n\t The System is Not in safe state “);

break;

case 4:

printf(“\n\n\t Number of processes : %d―,n);

printf(“\n\n\t Number of Resources : %d―,nor);

printf(“\n\n\t Pid \t Max \t Allocated \t Need “);

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

printf(“\n\n\t P%d : “,i);

for(j=0;j<nor;j++)

printf(― %d “,p[i].max[j]);

printf(“\t―);

for(j=0;j<nor;j++)

printf(― %d “,p[i].all[j]);

printf(“\t―);

for(j=0;j<nor;j++)

printf(― %d “,p[i].need[j]);

printf(“\n\n\t Available : “);

for(i=0;i<nor;i++)

printf(― %d “,avail[i]);

break;

case 5:

break;

getch();

}while(ch!=5);

int safeseq()

int i,j,k;

ss=0;
for(j=0;j<nor;j++)

work[j]=avail[j];

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

p[j].finished=0;

for(int tk=0;tk<nor;tk++)

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

if(p[j].finished==0)

check1=0;

for(k=0;k<nor;k++)

if(p[j].need[k]<=work[k])

check1++;

if(check1==nor)

for(k=0;k<nor;k++)

work[k]=work[k]+p[j].all[k];

p[j].finished=1;

sseq[ss]=j;

ss++;

}
}

check2=0;

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

if(p[i].finished==1)

check2++;

printf(“\n\n\t―);

if(check2>=n)

printf(“\n\n\t The system is in safe state―);

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

printf(“P%d, “,sseq[tj]);

return 1;

else

printf(“\n\n\t The system is Not in safe state―);

return 0;

CPU Scheduling Algorithms – FCFS, SJF and Round Robin

//CPU Scheduling algorithms

//FCFS, Round Robin and Shortest Job First

#include<iostream.h>

#include<conio.h>

#include<stdio.h>
class cpuschedule

int n,bu[20];

float twt,awt,wt[20],tat[20];

public:

void Getdata();

void fcfs();

void sjf();

void roundrobin();

};

//Getting no of processes and Burst time

void cpuschedule::Getdata()

int i;

cout<<“Enter the no of processes:―;

cin>>n;

for(i=1;i<=n;i++)

cout<<“\nEnter The BurstTime for Process p―<<i<<“=―;

cin>>bu[i];

//First come First served Algorithm

void cpuschedule::fcfs()

{
int i,b[10];

float sum=0.0;

twt=0.0;

for(i=1;i<=n;i++)

b[i]=bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<b[i];

wt[1]=0;

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

wt[i]=b[i-1]+wt[i-1];

for(i=1;i<=n;i++)

twt=twt+wt[i];

tat[i]=b[i]+wt[i];

sum+=tat[i];

awt=twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<twt;

cout<<“\nAverage Waiting Time=―<<awt;

cout<<“\nAverage Turnaround time=―<<sum;


}

//Shortest job First Algorithm

void cpuschedule::sjf()

int i,j,temp,b[10];

float sum=0.0;

twt=0.0;

for(i=1;i<=n;i++)

b[i]=bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<b[i];

for(i=n;i>=1;i–)

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

if(b[j-1]>b[j])

temp=b[j-1];

b[j-1]=b[j];

b[j]=temp;

}
wt[1]=0;

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

wt[i]=b[i-1]+wt[i-1];

for(i=1;i<=n;i++)

twt=twt+wt[i];

tat[i]=b[i]+wt[i];

sum+=tat[i];

awt=twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<twt;

cout<<“\nAverage Waiting Time=―<<awt;

cout<<“\nAverage turnaround time=―<<sum;

//Round Robin Algorithm

void cpuschedule::roundrobin()

int i,j,tq,k,b[10],Rrobin[10][10],count[10];

int max=0;

int m;

float sum=0.0;

twt=0.0;
for(i=1;i<=n;i++)

b[i]=bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<b[i];

if(max<b[i])

max=b[i];

wt[i]=0;

cout<<“\nEnter the Time Quantum=―;

cin>>tq;

//TO find the dimension of the Round robin array

m=max/tq+1;

//initializing Round robin array

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

Rrobin[i][j]=0;

//placing value in the Rrobin array

i=1;

while(i<=n)

{
j=1;

while(b[i]>0)

if(b[i]>=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<<“Display―;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

cout<<“\nRr[“<<i<<“,―<<j<<“]=―<<Rrobin[i][j];
cout<<― “;

cout<<“\ncount=―<<count[i];

for(j=1;j<=n;j++)

for(i=1;i<=count[j];i++)

if(i==count[j])

for(k=1;k<j;k++)

if(k!=j)

wt[j]+=Rrobin[k][i];

else

for(k=1;k<=n;k++)

if(k!=j)

wt[j]+=Rrobin[k][i];

for(i=1;i<=n;i++)
cout<<“\nWaiting Time for process P―<<i<<“=―<<wt[i];

//calculating Average Weighting Time

for(i=1;i<=n;i++)

twt=twt+wt[i];

tat[i]=b[i]+wt[i];

sum+=tat[i];

awt=twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<twt;

cout<<“\nAverage Waiting Time=―<<awt;

cout<<“\nAverage turnaround time=―<<sum;

void main()

int ch=0,cho;

cpuschedule c;

clrscr();

do

switch(ch)

case 0:

cout<<“\n0.MENU―;
cout<<“\n1.Getting BurstTime―;

cout<<“\n2.FirstComeFirstServed―;

cout<<“\n3.ShortestJobFirst―;

cout<<“\n4.RoundRobin―;

cout<<“\n5.EXIT―;

break;

case 1:

c.Getdata();

break;

case 2:

cout<<“FIRST COME FIRST SERVED SCHEDULING―;

c.fcfs();

break;

case 3:

cout<<“SHORTEST JOB FIRST SCHEDULING―;

c.sjf();

break;

case 4:

cout<<“ROUND ROBIN SCHEDULING―;

c.roundrobin();

break;

case 5:

break;

cout<<“\nEnter your choice:―;


cin>>ch;

getch();

}while(ch<5);

//CPU Scheduling algorithms

//FCFS, Round Robin and Shortest Job First

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class cpuschedule

int n,Bu[20];

float Twt,Awt,Wt[20],Tat[20];

public:

void Getdata();

void Fcfs();

void Sjf();

void RoundRobin();

};

//Getting no of processes and Burst time

void cpuschedule::Getdata()

int i;

cout<<“Enter the no of processes:―;

cin>>n;
for(i=1;i<=n;i++)

cout<<“\nEnter The BurstTime for Process p―<<i<<“=―;

cin>>Bu[i];

//First come First served Algorithm

void cpuschedule::Fcfs()

int i,B[10];

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

B[i]=Bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<B[i];

Wt[1]=0;

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

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

for(i=1;i<=n;i++)

{
Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<Twt;

cout<<“\nAverage Waiting Time=―<<Awt;

cout<<“\nAverage Turnaround time=―<<sum;

//Shortest job First Algorithm

void cpuschedule::Sjf()

int i,j,temp,B[10];

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

B[i]=Bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<B[i];

for(i=n;i>=1;i–)

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

if(B[j-1]>B[j])

temp=B[j-1];

B[j-1]=B[j];

B[j]=temp;

Wt[1]=0;

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

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

for(i=1;i<=n;i++)

Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<Twt;

cout<<“\nAverage Waiting Time=―<<Awt;

cout<<“\nAverage turnaround time=―<<sum;


}

//Round Robin Algorithm

void cpuschedule::RoundRobin()

int i,j,tq,k,B[10],Rrobin[10][10],count[10];

int max=0;

int m;

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

B[i]=Bu[i];

cout<<“\nBurst time for process p―<<i<<“=―;

cout<<B[i];

if(max<B[i])

max=B[i];

Wt[i]=0;

cout<<“\nEnter the Time Quantum=―;

cin>>tq;

//TO find the dimension of the Round robin array

m=max/tq+1;

//initializing Round robin array

for(i=1;i<=n;i++)

{
for(j=1;j<=m;j++)

Rrobin[i][j]=0;

//placing value in the Rrobin array

i=1;

while(i<=n)

j=1;

while(B[i]>0)

if(B[i]>=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<<“Display―;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

cout<<“\nRr[“<<i<<“,―<<j<<“]=―<<Rrobin[i][j];

cout<<― “;

cout<<“\ncount=―<<count[i];

for(j=1;j<=n;j++)

for(i=1;i<=count[j];i++)

if(i==count[j])

for(k=1;k<j;k++)

if(k!=j)

Wt[j]+=Rrobin[k][i];

}
else

for(k=1;k<=n;k++)

if(k!=j)

Wt[j]+=Rrobin[k][i];

for(i=1;i<=n;i++)

cout<<“\nWaiting Time for process P―<<i<<“=―<<Wt[i];

//calculating Average Weighting Time

for(i=1;i<=n;i++)

Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=―<<Twt;

cout<<“\nAverage Waiting Time=―<<Awt;

cout<<“\nAverage turnaround time=―<<sum;

void main()

{
int ch=0,cho;

cpuschedule c;

clrscr();

do

switch(ch)

case 0:

cout<<“\n0.MENU―;

cout<<“\n1.Getting BurstTime―;

cout<<“\n2.FirstComeFirstServed―;

cout<<“\n3.ShortestJobFirst―;

cout<<“\n4.RoundRobin―;

cout<<“\n5.EXIT―;

break;

case 1:

c.Getdata();

break;

case 2:

cout<<“FIRST COME FIRST SERVED SCHEDULING―;

c.Fcfs();

break;

case 3:

cout<<“SHORTEST JOB FIRST SCHEDULING―;

c.Sjf();
break;

case 4:

cout<<“ROUND ROBIN SCHEDULING―;

c.RoundRobin();

break;

case 5:

break;

cout<<“\nEnter your choice:―;

cin>>ch;

getch();

}while(ch<5);

Input & Output:

0.MENU

1.Getting BurstTime

2.FirstComeFirstServed

3.ShortestJobFirst

4.RoundRobin

5.EXIT

Enter your choice:1

Enter the no of processes:2

Enter The BurstTime for Process p1=7

Enter The BurstTime for Process p2=4

Enter your choice:2


FIRST COME FIRST SERVED SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=7

Average Waiting Time=3.5

Average Turnaround time=9

Enter your choice:3

SHORTEST JOB FIRST SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=4

Average Waiting Time=2

Average turnaround time=7.5

Enter your choice:4

SHORTEST JOB FIRST SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=4

Average Waiting Time=2

Average turnaround time=7.5

Enter your choice:5

You might also like