cs6413-operating-systems-laboratory
cs6413-operating-systems-laboratory
LAB MANUAL
Regulation : 2013
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 1
lOMoARcPSD|4113605
REGULATION -2013
LIST OF EXPERIMENTS:
Total hours: 45
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 2
lOMoARcPSD|4113605
INDEX
S. SIGNATURE
NO DATE NAME OF THE EXPERIMENTS OF THE REMARKS
STAFF
2 IO System Calls
5 Priority Scheduling
10 File Manipulation-I
11 File Manipulation-II
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 3
lOMoARcPSD|4113605
Operating System
Introduction
An Operating system is software that creates a relation between the User, Software and
Hardware. It is an interface between the all. All the computers need basic software known as an
Operating System (OS) to function.
The OS acts as an interface between the User, Application Programs, Hardware and the
System Peripherals. The OS is the first software to be loaded when a computers starts up. The
entire application programs are loaded after the OS.
1. Single User: If the single user Operating System is loaded in computer€s memory; the
computer would be able to handle one user at a time.
1. Multi user: If the multi-user Operating System is loaded in computer€s memory; the
computer would be able to handle more than one user at a time.
Ex: UNIX, Linux, XENIX
2. Network: If the network Operating System is loaded in computer€s memory; the
computer would be able to handle more than one computer at time.
Ex: Novel Netware, Win-NT, Win-2000-2003
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 4
lOMoARcPSD|4113605
C:\>-
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 5
lOMoARcPSD|4113605
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 6
lOMoARcPSD|4113605
Process Scheduling
Processes are the Small Programs those are executed by the user according to
their Request. CPU Executes all the Process according to Some Rules or Some
Schedule. Scheduling ist hat in which each process have Some Amount of Time of
CPU. Scheduling Provides Time of CPU to the Each Process.
A process once allocated the CPU keeps it until releasing the CPU either by terminating or
requesting I/O. For example, interrupted process is allowed to continujre running after interrupt
handling is done with.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 7
lOMoARcPSD|4113605
EX NO: 1
DATE:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 8
lOMoARcPSD|4113605
#include<stdlib.h>
#include<unistd.h>
if(pid<0)
printf("fork failed");
exit(1);
else if(pid==0)
execlp("whoami","ls",NULL);
exit(0);
else
wait(NULL);
exit(0);
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 9
lOMoARcPSD|4113605
OUTPUT:
RESULT:
Thus the process system call program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 10
lOMoARcPSD|4113605
EX.NO:2
DATE:
IO SYSTEM CALLS
AIM:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 11
lOMoARcPSD|4113605
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
int fd[2];
char buf2[50];
fd[0]=open("file1",O_RDWR);
fd[1]=open("file2",O_RDWR);
scanf("\n %s",buf1);
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 12
lOMoARcPSD|4113605
OUTPUT:
RESULT:
Thus the I/O system call program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 13
lOMoARcPSD|4113605
EX.NO:3
DATE:
AIM:
To write the program to implement CPU & scheduling algorithm for first come first serve
scheduling.
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 14
lOMoARcPSD|4113605
struct process
int pid;
int bt;
int wt,tt;
}p[10];
int main()
p[i].pid=i;
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
p[i].wt=p[i-1].bt+p[i-1].wt; p[i].tt=p[i].bt+p[i].wt;
i ++;
i=1;
totwt=tottt=0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 15
lOMoARcPSD|4113605
tottt=p[i].tt+tottt;
i++;}
return 0;
OUTPUT:
Process sid bt wt tt
1 2 0 2
2 4 2 6
3 6 6 12
avg1=2 avg2=6
RESULT:
Thus the FIFO process scheduling program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 16
lOMoARcPSD|4113605
EX.NO:4
DATE:
AIM:
To write a program to implement cpu scheduling algorithm for shortest job first
scheduling.
ALGORITHM:
1. Start the program. Get the number of processes and their burst time.
2. Initialize the waiting time for process 1 as 0.
3. The processes are stored according to their burst time.
4. The waiting time for the processes are calculated a follows:
for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1].
5. The waiting time of all the processes summed and then the average time is calculate
6. The waiting time of each processes and average time are displayed.
7. Stop the program.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 17
lOMoARcPSD|4113605
#include<conio.h>
struct process
int pid;
int bt;
int wt;
int tt;
}p[10],temp;
int main()
int i,j,n,totwt,tottt;
float avg1,avg2;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i].pid=i;
scanf("%d",&p[i].bt);
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++)
if(p[i].bt>p[j].bt)
temp.pid=p[i].pid;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 18
lOMoARcPSD|4113605
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n){
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
i=1;
totwt=tottt=0;
while(i<=n){
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
} avg1=totwt/n;
avg2=tottt/n;
printf("\nAVG1=%f\t AVG2=%f",avg1,avg2);
getch();
return 0; }
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 19
lOMoARcPSD|4113605
OUTPUT:
processid bt wt tt
1 2 0 2
2 4 2 6
3 6 6 12
AVG1=2.000000 AVG2=6.000000
RESULT:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 20
lOMoARcPSD|4113605
EX.NO:5
DATE:
PRIORITY SCHEDULING
AIM:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 21
lOMoARcPSD|4113605
#include<conio.h>
struct process
int pid;
int bt;
int wt;
int tt;
int prior;
p[10],temp;
int main()
int i,j,n,totwt,tottt,arg1,arg2;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i].pid=i;
scanf("%d",&p[i].bt);
scanf("%d",&p[i].prior);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 22
lOMoARcPSD|4113605
if(p[i].prior>p[j].prior)
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
i=1;
totwt=tottt=0;
while(i<=n)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 23
lOMoARcPSD|4113605
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
arg1=totwt/n;
arg2=tottt/n;
getch();
return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 24
lOMoARcPSD|4113605
OUTPUT:
process to bt wt tt
1 4 0 4 4
2 6 4 10 14
3 2 10 12 22
avg1=4 avg2=8
RESULT:
Thus the priority scheduling program was executed and verified successfully
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 25
lOMoARcPSD|4113605
EX.NO:6
DATE:
AIM:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 26
lOMoARcPSD|4113605
#include<conio.h>
struct process
int pid,bt,tt,wt;
};
int main()
int i,j,k,tot=0,m,n;
float wttime=0.0,tottime=0.0,a1,a2;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++){
x[i].pid=i;
scanf("%d",&x[i].bt);
tot=tot+x[i].bt;
p[0].tt=0;
k=1;
scanf("%d",&m);
for(j=1;j<=tot;j++)
for(i=1;i<=n;i++)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 27
lOMoARcPSD|4113605
if(x[i].bt !=0)
p[k].pid=i;
if(x[i].bt-m<0)
p[k].wt=p[k-1].tt;
p[k].bt=x[i].bt;
p[k].tt=p[k].wt+x[i].bt;
x[i].bt=0;
k++;
else
p[k].wt=p[k-1].tt;
p[k].tt=p[k].wt+m;
x[i].bt=x[i].bt-m;
k++;
for(i=1;i<k;i++){
wttime=wttime+p[i].wt;
tottime=tottime+p[i].tt;
a1=wttime/n;
a2=tottime/n;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 28
lOMoARcPSD|4113605
getch();
return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 29
lOMoARcPSD|4113605
OUTPUT:
process id wt tt
1 0 2
2 2 4
3 4 6
1 6 7
process id wt tt
2 7 9
3 9 11
2 11 12
3 12 14
3 14 15
RESULT:
Thus the Round Robin scheduling program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 30
lOMoARcPSD|4113605
EX.NO:7
DATE:
PIPE PROCESSING
AIM :
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 31
lOMoARcPSD|4113605
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MSG_LEN 64
int main(){
int result;
int fd[2];
char message[MSG_LEN];
char recvd_msg[MSG_LEN];
if (result < 0)
perror("pipe ");
exit(1);
result=write(fd[1],message,strlen(message));
if (result < 0)
perror("write");
exit(2);
strncpy(message,"Understanding ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 32
lOMoARcPSD|4113605
perror("write");
exit(2);
strncpy(message,"Concepts of ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
perror("write");
exit(2);
result=write(fd[1],message,strlen(message));
if (result < 0)
perror("write");
exit(2);
result=read (fd[0],recvd_msg,MSG_LEN);
if (result < 0)
perror("read");
exit(3);
printf("%s\n",recvd_msg);
return 0;}
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 33
lOMoARcPSD|4113605
OUTPUT:
RESULT:
Thus the Piping process using IPC program was executed and verified successfully
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 34
lOMoARcPSD|4113605
EX.NO:8
DATE:
ALGORITHM:
1. Declare variable for producer & consumer as pthread-t-tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 35
lOMoARcPSD|4113605
void main()
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
scanf("%d", &choice);
switch(choice)
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
break;
printf("\nBuffer is Empty");
else
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 36
lOMoARcPSD|4113605
consume = buffer[out];
out = (out+1)%bufsize;
break;
} } }
OUTPUT:
Buffer is Empty
RESULT:
Thus the producer consumer program was executed and verified successfully
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 37
lOMoARcPSD|4113605
EX.NO:9
DATE:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 38
lOMoARcPSD|4113605
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 39
lOMoARcPSD|4113605
for(j=1;j<=nb;j++)
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
ff[i]=j;
highest=temp;
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",
i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 40
lOMoARcPSD|4113605
OUTPUT:
Block 1: 5
Block 2: 2
Block 3: 7
File 1: 1
File 2: 4
OUTPUT
1 1 3 7 6
2 4 1 5 1
RESULT:
Thus the First Bit and Best Fit program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 41
lOMoARcPSD|4113605
EX.NO:10
DATE:
FILE MANIPULATION-I
AIM:
To write a program for file manipulation for displays the file and directory in memory
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 42
lOMoARcPSD|4113605
#include <stdio.h>
int main(void)
DIR *d;
d = opendir(".");
if (d)
printf("%s\n", dir->d_name);
closedir(d);
return(0);
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 43
lOMoARcPSD|4113605
OUTPUT:
RESULT:
Thus the file management program was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 44
lOMoARcPSD|4113605
EX.NO:11
DATE:
FILE MANIPULATION-II
AIM:
To write a program performs file manipulation.
ALGORITHM:
1. Start the program.
2. Declare the arguments for file open and file create.
3. print the file in directory otherwise display the error message error in creation
4. if check the files in directory
5. close the files and directory
6. Stop the program.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 45
lOMoARcPSD|4113605
#include<sys/stat.h>
#include<time.h>
main(int ag,char*arg[])
char buf[100];
struct stat s;
int fd1,fd2,n;
fd1=open(arg[1],0);
fd2=creat(arg[2],0777);
stat(arg[2],&s);
if(fd2==-1)
printf("ERROR IN CREATION");
while((n=read(fd1,buf,sizeof(buf)))>0)
if(write(fd2,buf,n)!=n)
close(fd1);
close(fd2);
close(fd1);
close(fd2);
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 46
lOMoARcPSD|4113605
OUTPUT:
RESULT:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 47
lOMoARcPSD|4113605
EX NO: 12
DATE:
AIM:
ALGORITHM:
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 48
lOMoARcPSD|4113605
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
clrscr();
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
for(j=0;j<nof;j++)
if(frm[j]==ref[i])
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 49
lOMoARcPSD|4113605
flag=1;
break;
}}
if(flag==0)
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
} }
getch();
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 50
lOMoARcPSD|4113605
OUTPUT:
564123
...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1
RESULT:
Thus the program FIFO page replacement was successfully executed.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 51
lOMoARcPSD|4113605
EX NO:13
DATE :
SIMULATE PAGE REPLACEMENT ALGORITHMS: LRU
AIM:
To Simulate LRU page replacement algorithms
ALGORITHM:
1. Start
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames by selecting the page that has not been used for the longest
period of time.
7. Display the number of page faults.
8. stop
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 52
lOMoARcPSD|4113605
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
clrscr();
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n………………………………..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
lrucal[i]=0;
for(i=0;i<10;i++)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 53
lOMoARcPSD|4113605
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
for(j=0;j<nof;j++)
if(frm[j]==ref[i])
flag=1;
break;
if(flag==0)
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
recent[ref[i]]=i;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 54
lOMoARcPSD|4113605
getch();
int lruvictim()
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
temp1=frm[i];
lrucal[i]=recent[temp1];
temp2=lrucal[0];
for(j=1;j<nof;j++)
if(temp2>lrucal[j])
temp2=lrucal[j];
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 55
lOMoARcPSD|4113605
OUTPUT:
654231
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
RESULT:
Thus the process LRU page replacement was executed and verified successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 56
lOMoARcPSD|4113605
EX.NO:14
DATE:
ALGORITHM:
6. Allocate the pages in to frames by selecting the page that will not be used for the
longest period of time.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 57
lOMoARcPSD|4113605
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
clrscr();
printf("\n......................... ........");
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n................................");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
frm[i]=-1;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 58
lOMoARcPSD|4113605
optcal[i]=0;
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
if(frm[j]==ref[i])
flag=1;
break;
if(flag==0)
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 59
lOMoARcPSD|4113605
getch();
int i,j,temp,notfound;
for(i=0;i<nof;i++)
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
notfound=0;
optcal[i]=j;
break;
if(notfound==1)
return i;
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 60
lOMoARcPSD|4113605
OUTPUT:
654231
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
RESULT:
Thus the process optimal page replacement was executed and verified
successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 61
lOMoARcPSD|4113605
EX NO: 15
DATE:
ALGORITHM:
1. Start the program
2. Attacking Mutex condition: never grant exclusive access. But this may not be possible for
several resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait condition: make a process hold at the most 1 resource
5. At a time. Make all the requests at the beginning. Nothing policy. If you feel, retry.
6. Attacking circular wait: Order all the resources. Make sure that the requests are issued in
the
7. Correct order so that there are no cycles present in the resource graph. Resources
numbered 1 ... n.
8. Resources can be requested only in increasing
9. Order. i.e. you cannot request a resource whose no is less than any you may be holding.
10. Stop the program
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 62
lOMoARcPSD|4113605
#include<conio.h>
int avail[10],i,j,p,r,finish[10]={0},flag=0;
int main()
clrscr( );
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
if(flag==0)
if(finish[i]!=1)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 63
lOMoARcPSD|4113605
for(j=0;j<r;j++)
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}fun();
for(j=0;j<r;j++)
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
fun( );
for(j=0;j<r;j++)
if(avail[j]<need[i][j])
avail[j]=need[i][j];
fun( );
getch( ); return 0;
fun()
while(1)
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 64
lOMoARcPSD|4113605
for(flag=0,i=0;i<p;i++)
if(finish[i]==0)
for(j=0;j<r;j++)
if(need[i][j]<=avail[j])
continue;
else
break;
if(j==r)
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
if(flag==0)
break;
}return 0;
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 65
lOMoARcPSD|4113605
OUTPUT:
SIMULATION OF DEADLOCK PREVENTION
Enter no. of processes, resources 3, 2
Enter allocation matrix 2 4 5
345
Enter max matrix4 3 4
561
Enter available matrix2
Failing: Mutual Exclusion
By allocating required resources to process dead is prevented
Lack of no preemption deadlock is prevented by allocating needed resources
Failing: Hold and Wait condition
RESULT:
Thus the program deadlock was executed successfully.
VVIT DEPARTMENT
Downloaded OF COMPUTER
by Usman Ahmad SCIENCE AND ENGINEERING
([email protected]) 66