OS Lab Manual R22 CSM
OS Lab Manual R22 CSM
Regulation: R22
Class: II B Tech I Semester CSE(AI & ML)
Academic Year: 2022-23
To make learning process exciting and interesting through curricular and co-
curricular program with staff molding them into leaders and entrepreneurs.
To nurture the students with the required skills to solve technical problems of
modern society with courage and confidence.
At the end of the program, the women engineers will be able to:
PSO1: Identify suitable data structures and algorithms to design and develop computing
solutions for real-life problems.
INDEX
Prerequisites
● A course on “Programming for Problem Solving”, A course on “Computer
Organization and Architecture”
Co-requisite
● A course on “Operating Systems”
Course Objectives:
● To provide an understanding of the design aspects of operating system concepts
through simulation
Introduce basic Unix commands, system call interface for process management,
interprocess communication and I/O in Unix.
Course Outcomes:
● Simulate and implement operating system concepts such as scheduling, deadlock
management, file management and memory management
List of Experiments:
Program 1
Aim: Write C programs to simulate the following CPU Scheduling algorithms a) FCFS
b) SJF c) Round Robin d) priority
a) FCFS
#include<stdio.h>
void main()
{
int wt[20]={0}, bt[20]={0}, tat[20]={0}, n, i, swt=0, stat=0;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times: ");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
}
awt=swt/n;
for(i=0;i<n;i++)
{
stat=stat+tat[i];
}
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
printf("\n-----------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",i+1,bt[i],tat[i],wt[i]);
}
printf("\n-----------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("-----------------------------------");
}
INPUT:
Process BT TAT WT
--------------------------------------------------
P1 24 24 0
P2 3 27 24
P3 4 31 27
P4 12 43 31
----------------------------------------------------
(ATAT)31.000000 20.000000(AWT)
b) SJF
#include<stdio.h>
void main()
{
Int wt[20] ={0}, bt[20]={0}, tat[20]={0}, i, swt=0, stat=0, max, j,
n, p[20],m;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
printf("Enter the processes");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
max=bt[i];
m=p[i];
bt[i]=bt[j];
p[i]=p[j];
bt[j]=max;
p[j]=m;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=swt/n;
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
printf("\n-----------------------------------");
for(i=0;i<n;i++)
{
printf("\n %d \t%d\t%d \t%d",p[i],bt[i],tat[i],wt[i]);
}
printf("\n-----------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("-----------------------------------");
}
INPUT:
Enter the no of processes:4
Enter the burst times6
7
8
3
Enter the processes1
2
3
4
OUTPUT:
Process BT TAT WT
-----------------------------------
4 3 3 0
1 6 9 3
2 7 16 9
3 8 24 16
-----------------------------------
(ATAT)13.000000 7.000000(AWT)
-----------------------------------
c) Round Robin
#include<stdio.h>
void main()
{
int p[20],bt[20]={0},tat[20]={0},wt[20]={0},i,temp=0,ct[20]={0},max,stat=0,swt=0,n,
tmslc,j;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("Enter the burst times:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
ct[i]=bt[i];
}
printf("Enter the tmslc:");
scanf("%d",&tmslc);
max=bt[0];
for(i=1;i<n;i++)
{
if(max<bt[i])
max=bt[i];
}
for(j=0;j<(max/tmslc)+1;j++)
{
for(i=0;i<n;i++)
{
if(bt[i]!=0)
{
if(bt[i]<=tmslc)
{
tat[i]=temp+bt[i];
temp=temp+bt[i];
bt[i]=0;
}
else
{
bt[i]=bt[i]-tmslc;
temp=temp+tmslc;
}
}
}
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-ct[i];
stat+=tat[i];
swt+=wt[i];
}
atat=(float)stat/n;
awt=(float)swt/n;
printf("Process\tBT\tTAT\tWT");
printf("\n---------------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",p[i],ct[i],tat[i],wt[i]);
}
printf("\n---------------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("\n---------------------------------------");
}
INPUT:
OUTPUT:
Process BT TAT WT
---------------------------------------
P1 10 19 9
P2 1 5 4
P3 2 7 5
P4 1 8 7
P5 5 17 12
---------------------------------------
(ATAT)11.200000 7.400000(AWT)
---------------------------------------
d) Priority
#include<stdio.h>
void main()
{
int wt[20]={0},bt[20]={0},tat[20]={0},prty[20]={0},i,swt=0,stat=0,max,j,n,m,p[20],x;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
printf("\nEnter the priorities:");
for(i=0;i<n;i++)
{
scanf("%d",&prty[i]);
}
printf("Enter the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(prty[i]>prty[j])
{
max=prty[i];
prty[i]=prty[j];
prty[j]=max;
m=p[i];
p[i]=p[j];
p[j]=m;
x=bt[i];
bt[i]=bt[j];
bt[j]=x;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=swt/n;
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
printf("\n---------------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",p[i],bt[i],tat[i],wt[i]);
}
printf("\n---------------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("\n---------------------------------------");
}
INPUT:
Enter the no of processes:5
Enter the burst times:10
1
2
1
5
Process BT TAT WT
---------------------------------------
P2 1 1 0
P5 5 6 1
P1 10 16 6
P3 2 18 16
P4 1 19 18
---------------------------------------
(ATAT)12.000000 8.000000(AWT)
---------------------------------------
Program 2
Aim: Write programs using the I/O system calls of UNIX/LINUX operating system
(open, read, write, close, fcntl, seek, stat, opendir, readdir)
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd;
fd=creat("n1",0666);
if(fd!=-1)
{
fd=open("n1",O_WRONLY);
if(fd==-1)
{
printf("Cannot open file");
}
write(fd,"This is OS Program",20);
close(fd);
}
}
OUTPUT:
This is OS Program
b) Open and read
include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd,n=1;
char buf;
fd=open("opening.c",O_RDONLY);
if(fd==-1)
{
printf("File doesnot exist");
}
while(n>0)
{
n=read(fd,&buf,1);
printf("%c",buf);
}
}
OUTPUT:
I like programming.
c) Opendir and readdir
#include<stdio.h>
#include<dirent.h>
void main(int argc,char **argv)
{
DIR *dp;
struct dirent *link;
dp=opendir(argv[1]);
printf("Contents of the directory %s are \n",argv[1]);
while((link=readdir(dp))!=0)
{
printf("%s\n",link->d_name);
}
closedir(dp);
}
OUTPUT:
d) stat
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
struct stat fs;
if(stat("p2.c",&fs)==-1)
{
printf("Error in stat system call");
}
printf("No of links: %ld\n",fs.st_nlink);
printf("Filesize: %ld bytes\n",fs.st_size);
printf("File in ode: %ld\n",fs.st_ino);
}
OUTPUT:
No of links: 1
Filesize: 279 bytes
File in ode: 8128784
e) fcntl
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main(int argc,char *argv[])
{
struct flock f1={F_UNLCK,SEEK_SET,0,0,0};
int fd;
char buf;
if((fd=open(argv[1],O_RDWR))==-1)
{
printf("Error in opening\n");
}
printf("Press<RETURN> to try to get lock\n");
getchar();
printf("Trying to get lock\n");
f1.l_type=F_WRLCK;
f1.l_pid=getpid();
if(fcntl(fd,F_SETLK,&f1)==-1)
{
printf("fcnl error\n");
}
else if(f1.l_type!=F_UNLCK)
{
printf("File has been exclusively locked by %d\n",f1.l_pid);
}
else
{
printf("File is not locked\n");
}
printf("Press enter to return lock\n");
getchar();
f1.l_type=F_UNLCK;
printf("File has been unlocked\n");
close(fd);
}
OUTPUT
In Terminal 1
In Terminal 2
f) i) SEEK_SET
#includesys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
void main()
{
char buf;
int fd=open("my.c",O_RDONLY);
lseek(fd,5,SEEK_SET);
read(fd,&buf,1);
printf("The char at offset 5=%c\n",buf);
close(fd);
}
OUTPUT:
ii) SEEK_CUR
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
void main()
{
char buf;
int fd=open("my.c",O_RDONLY);
lseek(fd,5,SEEK_SET);
read(fd,&buf,1);
printf("The char at offset 5=%c\n",buf);
close(fd);
}
OUTPUT:
ii) SEEK_END
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd;
fd=open("my.c",O_WRONLY);
lseek(fd,10,SEEK_END);
write(fd,"I am in BRECW",13);
close(fd);
}
OUTPUT:
Aim: Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and
Prevention
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i,j,k,p,b,n,r,g,cnt=0,id,newr;
int avail[10],seq[10];
printf("Enter the no of processes--");
scanf("%d",&n);
printf("Enter the no of resources");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter the details for P%d\n",i);
printf("Enter the allocation : ");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter the max: ");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available resources--\t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter new Request details--");
printf("\nEnter the pid\t--");
scanf("%d",&id);
printf("\nEnter the request for resources--\t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i]+=newr;
avail[i]=avail[i]-newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\nREQUEST NOT GRANTED__DEADLOCK OCCURRED");
printf("\nSYTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe safe sequence is __(");
for(i=0;i<fl;i++)
printf("P%d\t",seq[i]);
printf(")");
y:printf("\nProcess\t\tAllocation\t Max\t\t Need\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}
INPUT:
OUTPUT:
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
SYSTEM IS IN SAFE STATE
Process Allocation Max Need
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Program 4
#include<stdio.h>
void main()
{
int buffer[10],bufsize,in,out,produce,consume,choice;
in=0;
out=0;
bufsize=10;
while(choice!=3)
{
printf("\n 1.produce \t 2. consumer \t 3.Exit");
printf("\n Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\n buffer is full");
else
{
printf("\n enter the value:");
scanf("%d", &produce);
buffer[in]=produce;
in=(in+1)%bufsize;
}
break;
case 2: if(in==out)
printf("\n buffer is empty");
else
{
consume=buffer[out];
printf("\n the consumered value is %d", consume);
out=(out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
buffer is empty
1.produce 2. consumer 3.Exit
Enter your choice:1
a) pipes
#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1)
{
printf("Unable to create pipe\n");
return 1;
}
pid = fork();
// Child process
if (pid == 0)
{
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n", readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n", readmessage);
}
else
{
//Parent process
printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
}
return 0;
}
OUTPUT:
Parent Process - Writing to pipe - Message 1 is Hi
Parent Process - Writing to pipe - Message 2 is Hello
Child Process - Reading from pipe – Message 1 is Hi
Child Process - Reading from pipe – Message 2 is Hello
b) FIFO
fifoheader.h
#include<stdio.h>
#define BUFFMAX 80
#define PERMS 0666
#include<sys/stat.h>
#include<string.h>
#define F1 "F1"
#define F2 "F2"
client(int readfd, int writefd)
{
int n,n1;
char buff[100];
if((n=read(0,buff,80))<0)
printf("error in reading from keyboard");
if((n1=write(writefd,buff,n))==n)
{
n=read(readfd,buff,100);
write(1,buff,n);
}
else
printf("error in writing into pipe");
}
ser1.c
#include"fifoheader.h"
main()
{
int readfd,writefd;
if(mknod(FIFO1,PERMS|S_IFIFO,0)<0)
printf("error in creating FIFO1");
if(mknod(FIFO2,PERMS|S_IFIFO,0)<0);
printf("error in creating FIFO2");
readfd=open(FIFO1,0);
writefd=open(FIFO2,1);
server(readfd,writefd);
close(FIFO1);
close(FIFO2);
}
cli1.c
#include"fifoheader.h"
main()
{
int readfd,writefd;
writefd=open(FIFO1,1);
readfd=open(FIFO2,0);
client(readfd,writefd);
}
OUTPUT:
$./cli1
hi
hi
this is the message from server[3]-exit 255 ./ser1
c) Message Passing(server)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct msg_buffer {
long msg_type;
char msg[100];
} message;
void main() {
key_t my_key;
int msg_id;
my_key = ftok("progfile", 65); //create unique key
msg_id = msgget(my_key, 0666 | IPC_CREAT); //create message queue and return id
message.msg_type = 1;
printf("Write Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_id, &message, sizeof(message), 0); //send message
printf("Sent message is : %s \n", message.msg);
}
OUTPUT:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
shmhdr.h
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<sys/sem.h>
#define SHMKEY 21250
#define PERMS 0666
#define MAXMESGDATA 1000
#define MESGHDRSIZE (sizeof(mesg)-MAXMESGDATA)
typedef struct{
int mesg_len;
int mesg_type;
char mesg_data[MAXMESGDATA];
}mesg;
shmclient.c
#include"shmhdr.h"
int shmid;
mesg *mesgptr;
main()
{
if((shmid=shmget(SHMKEY,sizeof(mesg),0))<0)
perror("can't get shared memory segment");
if((mesgptr=(mesg *)shmat(shmid,0,0))<0)
perror("can't attach shared memory");
client();
if(shmdt(mesgptr)<0)
perror("can't deattach shared memory");
if(shmctl(shmid,IPC_RMID,0)<0)
perror("can't remove shared memory");
exit(0);
}
client()
{
int n;
if(read(0,mesgptr->mesg_data,80)<0)
perror("error in reading from keyboard");
n=strlen(mesgptr->mesg_data);
if(write(1,mesgptr->mesg_data,strlen(mesgptr->mesg_data))!=strlen(mesgptr-
>mesg_data))
perror("error in writing on to screen");
}
shmserver.c
#include"shmhdr.h"
int shmid;
mesg *mesgptr;
main()
{
if((shmid=shmget(SHMKEY,sizeof(mesg),PERMS|IPC_CREAT))<0)
perror("error in shared memory creation");
if((mesgptr=(mesg *)shmat(shmid,0,0))<0)
perror("can't attach shared memory");
server();
if(shmdt(mesgptr)<0)
perror("can't deattach shared memory");
exit(0);
}
server()
{
strcat(mesgptr->mesg_data," from the server");
mesgptr->mesg_len=strlen(mesgptr->mesg_data);
}
Output:
cc shmclient.c -o shmclient
cc shmclient.c -o shmclient
$cc shmserver.c
$cc shmclient.c
hello
hello
from the server
Program 6
Aim: Write C programs to simulate the following memory management techniques
a) Paging b) Segmentation
a) Paging
#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
OUTPUT:
Your memsize is 15
Enter page size:100
a) FCFS
#include<stdio.h>
#include<conio.h>
int fsize;
int frm[15];
void display();
void main()
{
int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0;
//clrscr();
printf("\n Enter frame size:");
scanf("%d",&fsize);
printf("\n Enter number of pages:");
scanf("%d",&nPage);
for(i=0;i< nPage;i++)
{
printf("\n Enter page[%d]:",i+1);
scanf("%d",&pg[i]);
}
for(i=0;i< fsize;i++)
frm[i]=-1;
printf("\n page | \t Frame content ");
printf("\n--------------------------------------");
for(j=0;j< nPage;j++)
{
flag=0;
for(i=0;i< fsize;i++)
{
if(frm[i]==pg[j])
{
flag=1;
break;
}
}
if(flag==0)
{
if(top==fsize-1)
{
top=-1;
}
pf++;
top++;
frm[top]=pg[j];
}
printf("\n %d |",pg[j]);
display();
}
printf("\n--------------------------------------");
printf("\n total page fault:%d",pf);
getch();
}
void display()
{
int i;
for(i=0;i< fsize;i++)
printf("\t %d",frm[i]);
}
Output:
Enter frame size:3
Enter number of pages:12
Enter page[1]:1
Enter page[2]:2
Enter page[3]:3
Enter page[4]:4
Enter page[5]:1
Enter page[6]:2
Enter page[7]:5
Enter page[8]:1
Enter page[9]:2
Enter page[10]:3
Enter page[11]:4
Enter page[12]:5
b) LRU
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
Output:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
c) Optima
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i,
j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
if(flag3 ==0){
max = temp[0];
pos = 0;
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
return 0;
}
Output:
1 -1 -1
1 2 -1
1 2 3
1 2 4
1 2 5
1 2 6
1 2 6
1 2 6
1 2 3
1 2 4
1 2 5
1 2 5
1 2 5
1 2 5
3 2 5
4 2 5
4 2 5
6 2 5