Opcodes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

Operating system codes

Assignment no.1
Operations on processes
Slot 1
1.Implement the c program to create a child process using fork(), display parent and child process id.child
process will display the message “I am child process: and the parent process should display “ I am aren’tt
process”.
//assignment1slot11
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int pid,x;
printf("\nparent process");
pid=fork();
printf("\nProcess id %d:",pid);
if(pid==0)
{
printf("\nchild process %d ,my parent id is %d\n ",getpid(),getppid());
}
else if(pid > 0)
{
printf("\nparent process\n");
}
x=fork();
printf("\n fork %d:\n",x);
}
2.write program that demonstrates the use of nice() system call .afteer a child process is started using fork(),
assign higher priority to the child using nice() system call.
//assignment1 slot1.2
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int pid,x;
printf("\nparent process");
pid=fork();
printf("\nProcess id %d:",pid);
if(pid==0)
{
printf("\n child Process id %d:",getpid());
printf("\npriority %d,id= %d\n ",nice(-7),getpid());
}
else
{
printf("\n parent Process id %d:",getppid());
printf("\npriority %d,id= %d\n ",nice(15),getppid());
}
}
/*
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int pid,n;
printf("accept a number");
scanf("%d",&n);
printf("\nparent process");
pid=fork();
if(pid==0)
if(n%2==0)
printf("number is even");
else
printf("number is odd");
else if(pid > 0)
printf("\n i am parent process\n");
}
*/
Slot2
1.implement c program to accept n integers to be sorted .main function creates child process using fork sytem
call .parent process sorts the integers using bubble sort and waits for child process using wait system
call .child process sorts the integers using insertion sort.
//assignment1slot2.1
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int pid;
int n,a[10],i,temp,j,p,key;
printf("Accept the number:");
scanf("%d",&n);
printf("the array elements are:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
pid=fork();
if(pid==0)
{

for(j=1;j<n;j++)
{
key=a[j];
for(i=j-1;i>=0;i++)
{
if(a[j]>key)
{
a[i+1]=a[i];
}
else
break;
}
a[j+1]=key;
}
}
else
{
printf("\nbubble sort");
for(p=0;p<n-1;p++)
{
for(i=0;i<n-1-p;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("bubble sorted elements are :\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("insertion sorted elements are :\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
}

2.write c program to illustrate concept of orphan process.parent process creates a child and terminates before
child has finished its task. So child process becomes orphan process.(use fork(),sleep(),getpid(),getppid()).
Assignment 2
Slot1
//Lwc
//line word char count l w c
#include<stdio.h>
#include<stdlib.h>
#include<wait.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
main()
{
FILE *fp;
int pid,c=0,w=0,l=0;
char func[50],op[50],fnm[50],cmd[50],ch,buff[1000];
while(1)
{
printf("\n[myshell] $");
gets(func);
sscanf(func,"%s %s %s",cmd,op,fnm);
pid=fork();
if(pid==0)
{
if(strcmp(cmd,"count")==0)
{
fp=open(fnm,O_RDONLY);
if(fp==-1)
{
printf("\nFILE DOES NOT EXIST");
}
else
{
c=0,w=0,l=0;
while(read(fp,buff,1)!=0)
{
ch=buff[0];
c++;
if(ch=='\n')
{w++;l++;}
else if(ch==' '||ch=='\t')
w++;
}
// printf("\nWord Count=%d Character Count=%d Line Count=%d",w,c,l);
close(fp);
if(strcmp(op,"c")==0)
{
printf("\nChar count=%d",c);
}
if(strcmp(op,"w")==0)
{
printf("\nWord Count=%d",w);
}
if(strcmp(op,"l")==0)
{
printf("\nLine Count=%d",l);
}
if(strcmp(op,"wc")==0)
{printf("\nWord Count=%d Character Count=%d",w,c);}
if(strcmp(op,"wl")==0)
{printf("\nWord Count=%d Line Count=%d",w,l);}
if(strcmp(op,"cl")==0)
{printf("\nCharacter Count=%d Line Count=%d",c,l);}
if(strcmp(op,"wcl")==0)
{printf("\nWord Count=%d Character Count=%d Line Count=%d",w,c,l);}
}
}
else if(strcmp(cmd,"exit")==0)
{
exit(0);
}
}
}
}
/*
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
main()
{
int l=0,w=0,c=0;
char str[10],cmd[10],opt[10],fnm[10], buff[10],abc[30],a[10];
//char ch[20];
FILE *fp;
while(1)
{
printf("[myprompt]");
gets(abc);
printf(abc);
sscanf(abc,"%s%s%s",cmd,opt,fnm);
printf("\tcommand is:%s",cmd);
printf("\toption is:%s",opt);
printf("\tFile name:%s",fnm);
fp=open(fnm,O_RDONLY);
if(fp)
{
l=0;
w=0;
while(read(fp,buff,1)!=0)
{
printf("%c",buff[0]);
c++;
if(buff[0]=='\n')
{
l++;
w++;;
}
if(buff[0]==' ')
w++;
}
if(opt[0]=='w')
printf("word count is %d:",w);

if(opt[0]=='l')
printf("line count is %d:",l);

if(opt[0]=='c')
printf("character count is %d:",c);
}
}
}
*/

Slot2
1.
//fac
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
main()
{
char str[10],cmd[10],opt[10],fnm[10], buff[10],abc[30],a[10],line[30],ch,PAT[30];
int pid,cnt=0,i=0,n;
FILE *fp;
while(1)
{
printf("[myprompt]");
gets(abc);
//printf(abc);
sscanf(abc,"%s%s%s%s",cmd,opt,fnm,PAT);
printf("\tcommand is:%s",cmd);
printf("\toption is:%s",opt);
printf("\tFile name:%s",fnm);
//printf("\tpattern:%s",PAT);
fp=open(fnm,O_RDONLY);
if(fp)
{
while(read(fp,buff,1)!=0)
{
ch=buff[0];
if(ch!='\n')
{
line[i]=ch;
i++;
}
if(ch=='\n')
{
line[i]='\0';
i=0;
if(strstr(line,PAT)!=NULL)
{
cnt++;
if(opt[0]=='f')
{
printf("first occurance is %s\n",line);
break;
}
if(opt[0]=='a')
printf("all occurance is %s\n",line);
if(opt[0]=='c')
printf("all count is %d\n",cnt);

}
}
}
}
}
}
2.
//ani
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>
DIR *dp;
struct dirent *dirfnm;
char fun[80],cmd[40],op[2],dnm[40];
int pid,cnt=0;
void main()
{
while(1)
{
printf("[myprompt]");
gets(fun);
sscanf(fun,"%s%s%s",cmd,op,dnm);
{
if(strcmp(cmd,"list")==0)
{
if(strcmp(op,"a")==0)
{
dp=opendir(dnm);
while((dirfnm=readdir(dp))!=NULL)
{
printf("\nname=%s",dirfnm->d_name);
}
closedir(dp);
}
if(strcmp(op,"n")==0)
{
dp=opendir(dnm);
while((dirfnm=readdir(dp))!=NULL)
{
cnt++;
}
printf("no of files are=%d",cnt);
closedir(dp);
}
if(strcmp(op,"i")==0)
{
dp=opendir(dnm);
while((dirfnm=readdir(dp))!=NULL)
{
printf("\nname=%s\tnode no=%d",dirfnm->d_name,dirfnm->d_ino);
}
closedir(dp);
}
}
if(strcmp(cmd,"exit")==0)
{
exit(0);
}
}
}
}

Slot3
//n-na

Assignment 3
//fcfs
Slot 1 fcfs
//inaara5603
//fcfs.c

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<wait.h>
#include<unistd.h>
#include<string.h>
struct process
{
char pnm[10];
int bt,at,wt,tat,ft,st;
}p[20];
int n,i,j;
char temp[10];
int t,t1;
float avg1=0,avg2=0;

void accept()
{
for(i=1;i<=n;i++)
{
printf("\n enter the process name:");
scanf("%s",p[i].pnm);
printf("\n enter the burst time:");
scanf("%d",&p[i].bt);
printf("\n enter the arrival time:");
scanf("%d",&p[i].at);
}
}
void display()
{
printf("\n process \tbt \tat \twt \ttat\n");
for(i=1;i<=n;i++)
{
printf("\n%s",p[i].pnm);
printf("\t\t%d",p[i].bt);
printf("\t%d",p[i].at);
printf("\t%d",p[i].wt);
printf("\t%d",p[i].tat);
}
printf("\n average wait time is:%f",avg1);
printf("\n average turn around time is:%f",avg2);
}
void sort()
{
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].at>p[j].at)
{
strcpy(temp,p[j].pnm);
strcpy(p[j].pnm,p[i].pnm);
strcpy(p[i].pnm,temp);

t=p[j].at;
p[j].at=p[i].at;
p[i].at=t;

t1=p[j].bt;
p[j].bt=p[i].bt;
p[i].bt=t1;
}
}
}
}
void fcfs()
{
int time =0;
for(i=1;i<=n;i++)
{
p[i].st=time;
while(p[i].bt!=0)
{
p[i].bt--;
time++;
}
p[i].ft=time;
}
}
void wt_tat()
{
int tot1=0,tot2=0;
for(i=1;i<=n;i++)
{
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].ft-p[i].at;
tot1=tot1+p[i].wt;
tot2=tot2+p[i].tat;
}
avg1=(float)tot1/n;
avg2=(float)tot2/n;
}
main()
{
printf("\n enter number of process:");
scanf("%d",&n);
accept();
sort();
fcfs();
wt_tat();
display();
}

//preemptive sjf
//premptive sjf
//asignment
//preemptive sjf
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
//#include<wait.h>
struct process
{
char pnm[20];
int bt,at,ft,wt,st,tat,tbt;
}p[20];
float avg1=0,avg2=0;
int n,totalbt=0;

void accept(int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("process name");
scanf("%s",p[i].pnm);
printf("bus time");
scanf("%d",&p[i].bt);
totalbt=totalbt+p[i].bt;
printf("arrival time");
scanf("%d",&p[i].at);
p[i].tbt=p[i].bt;
}
}

void display(int n)
{
int i;
printf("\npname \tbt \tat \tft \ttat \twt ");
for(i=1;i<=n;i++)
{
printf("\n %s",p[i].pnm);
printf("\t %d",p[i].bt);
printf("\t %d",p[i].at);
printf("\t %d",p[i].ft);
printf("\t %d",p[i].tat);
printf("\t %d",p[i].wt);
printf("\n");
}
printf("\n average of wait time:%f",avg1);
printf("\n average of tat is:%f",avg2);
printf("\ntotal time %d",totalbt);
}

void sort(int n)
{
char t[20];
int i,j,t2;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].at>p[j].at)
{
strcpy(t,p[j].pnm);
strcpy(p[j].pnm,p[i].pnm);
strcpy(p[i].pnm,t);
}
if(p[i].at>p[j].at)
{
t2=p[j].at;
p[j].at=p[i].at;
p[i].at=t2;

t2=p[j].bt;
p[j].bt=p[i].bt;
p[i].bt=t2;

t2=p[j].tbt;
p[j].tbt=p[i].tbt;
p[i].tbt=t2;

}
}
}
}

void sjfp()
{
int time=0;
int x,i ;
while(time<=totalbt)
for(i=1;i<=n;i++)
{
x=getprocess(time);
if(x==-1)
{
time++;
}
else
{
p[x].st=time;
if(p[x].bt!=0)
{
p[x].bt--;
display(n);
time++;
}
p[x].ft=time;
}
}
}

int getprocess(int tt)


{
int min=99,y=-1,j;
for(j=1;j<=n;j++)
{
if(p[j].bt<=min && p[j].at<=tt && p[j].bt!=0)
{
min=p[j].bt;
y=j;
}
}
return(y);
}

void wttat()
{
int i;
int tot=0,tot1=0;
for(i=1;i<=n;i++)
{
p[i].wt=p[i].ft-(p[i].at+p[i].tbt);
p[i].tat=p[i].ft-p[i].at;
tot=tot+p[i].wt;
tot1=tot1+p[i].tat;
}
avg1=(float)tot/n;
avg2=(float)tot1/n;
}

void main()
{
printf("\n Enter the process:");
scanf("%d",&n);
accept(n);
display(n);
sort(n);
display(n);
sjfp();
wttat();
display(n);
}

//nonpremptive sjf

//non premptive sjf


#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
//#include<wait.h>
struct process
{
char pnm[20];
int bt,at,ft,wt,st,tat;
}p[20];
float avg1=0,avg2=0;
int n;

void accept(int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("process name");
scanf("%s",p[i].pnm);
printf("bus time");
scanf("%d",&p[i].bt);
printf("arrival time");
scanf("%d",&p[i].at);
}
}

void display(int n)
{
int i;
printf("\npname \tbt \tat \tft \ttat \twt ");
for(i=1;i<=n;i++)
{
printf("\n %s",p[i].pnm);
printf("\t %d",p[i].bt);
printf("\t %d",p[i].at);
printf("\t %d",p[i].ft);
printf("\t %d",p[i].tat);
printf("\t %d",p[i].wt);
printf("\n");
}
printf("\n average of wait time:%f",avg1);
printf("\n average of tat is:%f",avg2);
}

void sort(int n)
{
char t[20];
int i,j,t2;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].at>p[j].at)
{
strcpy(t,p[j].pnm);
strcpy(p[j].pnm,p[i].pnm);
strcpy(p[i].pnm,t);
}
if(p[i].at>p[j].at)
{
t2=p[j].at;
p[j].at=p[i].at;
p[i].at=t2;

t2=p[j].bt;
p[j].bt=p[i].bt;
p[i].bt=t2;
}
}
}
}

void sjf()
{
int time=0;
int x,i ;
for(i=1;i<=n;i++)
{
x=getprocess(time);
p[x].st=time;
while(p[x].bt!=0)
{
p[x].bt--;
//display(n);
time++;
}
p[x].ft=time;
}
}
int getprocess(int tt)
{
int min=99,y,j;
for(j=1;j<=n;j++)
{
if(p[j].bt<=min && p[j].at<=tt && p[j].bt!=0)
{
min=p[j].bt;
y=j;
}
}
return(y);
}
void wttat()
{
int i;
int tot=0,tot1=0;
for(i=1;i<=n;i++)
{
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].ft-p[i].at;
tot=tot+p[i].wt;
tot1=tot1+p[i].tat;
}
avg1=(float)tot/n;
avg2=(float)tot1/n;
}

void main()
{
printf("\n Enter the process:");
scanf("%d",&n);
accept(n);
display(n);
sort(n);
display(n);
sjf();
wttat();
display(n);
}

Slot2
//non preemptive priority
//priority
//asignment
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<wait.h>
struct process
{
char pnm[20];
int bt,at,ft,wt,st,tat,pri;
}p[20];
float avg1=0,avg2=0;
int n;

void accept(int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("process name");
scanf("%s",p[i].pnm);
printf("bus time");
scanf("%d",&p[i].bt);
printf("arrival time");
scanf("%d",&p[i].at);
printf("priority time");
scanf("%d",&p[i].pri);
}
}

void display(int n)
{
int i;
printf("\npname \tbt \tat \tft \ttat \twt \tpri");
for(i=1;i<=n;i++)
{
printf("\n %s",p[i].pnm);
printf("\t %d",p[i].bt);
printf("\t %d",p[i].at);
printf("\t %d",p[i].ft);
printf("\t %d",p[i].tat);
printf("\t %d",p[i].wt);
printf("\t %d",p[i].pri);
printf("\n");
}
printf("\n average of wait time:%f",avg1);
printf("\n average of tat is:%f",avg2);
}

void sort(int n)
{
char t[20];
int i,j,t2;
for(i=1;i<=n;i++)
{
for(j=i;j<=n-i;j++)
{
if(p[j].at>p[j+1].at)
{
strcpy(t,p[j].pnm);
strcpy(p[j].pnm,p[j+1].pnm);
strcpy(p[j+1].pnm,t);
//}
//if(p[j+1].at>p[j].at)
//{
t2=p[j].at;
p[j].at=p[j+1].at;
p[j+1].at=t2;

t2=p[j].bt;
p[j].bt=p[j+1].bt;
p[j+1].bt=t2;

t2=p[j].pri;
p[j].pri=p[j+1].pri;
p[j+1].pri=t2;

}
}
}
}

void prior()
{
int time=0;
int x,i ;
for(i=1;i<=n;i++)
{
x=getprocess(time);
p[x].st=time;
while(p[x].bt!=0)
{
p[x].bt--;
//display(n);
time++;
}
p[x].ft=time;
}
}
int getprocess(int tt)
{
int min=99,y,j;
for(j=1;j<=n;j++)
{
if(p[j].pri<=min && p[j].at<=tt && p[j].bt!=0)
{
min=p[j].pri;
y=j;
}
}
return(y);
}
void wttat()
{
int i;
int tot=0,tot1=0;
for(i=1;i<=n;i++)
{
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].ft-p[i].at;
tot=tot+p[i].wt;
tot1=tot1+p[i].tat;
}
avg1=(float)tot/n;
avg2=(float)tot1/n;
}

void main()
{
printf("\n Enter the process:");
scanf("%d",&n);
accept(n);
display(n);
sort(n);
display(n);
prior();
wttat();
display(n);
}
//round robin
//inaara5603 round robin
//rr.c
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<wait.h>
#include<unistd.h>
#include<string.h>
struct process
{
char pnm[10];
int bt,at,wt,tat,ft,st,bt1;
}p[20];
int n,i,j;
char temp[10];
int t,tt,totbt=0,tq;
float avg1=0,avg2=0;

void accept()
{
for(i=1;i<=n;i++)
{
printf("\n enter the process name:");
scanf("%s",p[i].pnm);
printf("\n enter the burst time:");
scanf("%d",&p[i].bt);
p[i].bt1=p[i].bt;
totbt=totbt+p[i].bt;
printf("\n enter the arrival time:");
scanf("%d",&p[i].at);
}
printf("\n enter the time quantum:");
scanf("%d",&tq);
}
void display()
{
int i,bt;
printf("\n process \tbt \tat \twt \ttat \n");
for(i=1;i<=n;i++)
{
printf("\n%s",p[i].pnm);
printf("\t\t%d",p[i].bt);
printf("\t%d",p[i].at);
printf("\t%d",p[i].wt);
printf("\t%d",p[i].tat);
}
printf("\n average wait time is:%f",avg1);
printf("\n average turn around time is:%f",avg2);
}
void sort()
{
int i,j,t,t1;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].at>p[j].at)
{
strcpy(temp,p[j].pnm);
strcpy(p[j].pnm,p[i].pnm);
strcpy(p[i].pnm,temp);

t=p[j].at;
p[j].at=p[i].at;
p[i].at=t;

t1=p[j].bt;
p[j].bt=p[i].bt;
p[i].bt=t1;

t1=p[j].bt1;
p[j].bt1=p[i].bt1;
p[i].bt1=t1;
}
}
}
}

void rr()
{
int i,time=0,cnt=0;
while(time<=totbt)
{
if(cnt==n)
break;
else
{
for(i=1;i<=n;i++)
{
if(p[i].bt!=0)
{
if(p[i].at<=time)
{
if(p[i].bt<=tq)
{
time=time+p[i].bt;
p[i].bt=0;
p[i].ft=time;
cnt=cnt+1;
display();
}
else if(p[i].bt>tq)
{
p[i].bt=p[i].bt-tq;
time=time+tq;
display();
}
}
else
time++;
}
}
}

}
}
void wt_tat()
{
int i,tot1=0,tot2=0;
for(i=1;i<=n;i++)
{
p[i].wt=p[i].ft-(p[i].bt1+p[i].at);
p[i].tat=p[i].ft-p[i].at;
tot1=tot1+p[i].wt;
tot2=tot2+p[i].tat;
}
avg1=(float)tot1/n;
avg2=(float)tot2/n;
}
main()
{
printf("\n enter number of process:");
scanf("%d",&n);
accept();
display();
sort();
rr();
wt_tat();
display();
}

Assignment 4
Slot1
1.fifo

//ass4slot1.1
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<wait.h>
#include<unistd.h>
#include<string.h>

int nop,nof,prf[20],frame[5],pg;

void accept()
{
int i;
printf("\n enter the no. of pages:");
scanf("%d",&nop);
printf("\n enter the no of frames:");
scanf("%d",&nof);

printf("\n enter the pagereferrance (data):");


for(i=1;i<=nop;i++)
{
scanf("%d",&prf[i]);
}
}

void display()
{
int i,fault;
printf("\n your data is:");
/*for(i=1;i<=nop;i++)
{
printf("\t%d",prf[i]);
}*/
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}

void init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}

int search(int pg)


{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==pg)
return j;
}
return -1;
}

void fifo()
{
int i,j,x,fault=0,p=-1;
for(i=1;i<=nop;i++)
{
x=search(prf[i]);
if(x==-1)
{
fault++;
p=(p+1)%nof;
printf("\np=%d",p);
frame[p]=prf[i];
display();
printf("\n no of page fault:%d",fault);
}
}
}

main()
{
accept();
init();
display();
fifo();
display();
}

2.lru

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
//#include<wait.h>
#include<unistd.h>
#include<stdlib.h>
int nop,nof,pgref[20],frame[5],pg,pos[10];

void accept()
{
int i;
printf("Enter the number of pages: ");
scanf("%d",&nop);
printf("\nEnter the number of frames: ");
scanf("%d",&nof);
printf("\nEnter the data: ");
for(i=0;i<nop;i++)
{
scanf("%d",&pgref[i]);
}
}

void init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}

void display()
{
int i;
printf("\n\nEntered Data: ");
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}

int search(int pg)


{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==pg)
return j;
}
return -1;
}

int minpos()
{
int i,min=99,index;
for(i=0;i<nof;i++)
{
if(pos[i]<min)
{
min=pos[i];
index=i;
}
}
return index;
}
void LRU()
{
int i,j,x,pgflt=0,k,flag,z;
for(i=0;i<nop;i++)
{
flag=0;
x=search(pgref[i]);
if(x==-1)
{
pgflt++;
for(j=0;j<nof;j++)
{
if(frame[j]==-1)
{
frame[j]=pgref[i];
flag=1;
break;
}
}

if(flag==0)
{
for(j=0;j<nof;j++)
{
for(k=i-1;k>=0;k--)
{
if(frame[j]==pgref[k])
{
pos[j]=k;
printf("\nPosition=%d",pos[j]);
break;
}
}
}
z=minpos();
printf("\nMaxpos=%d",z);
frame[z]=pgref[i];
}
}
display();
}
printf("\nPage fault=%d",pgflt);
}

main()
{
accept();
init();
display();
LRU();
display();
}

3.opt
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<wait.h>
#include<unistd.h>
#include<string.h>
int nop,nof,prf[20],frame[5],pg;
int pos[20];
void accept()
{
int i;
printf("\n enter the no. of pages:");
scanf("%d",&nop);
printf("\n enter the no of frames:");
scanf("%d",&nof);

printf("\n enter the pagereferrance (data):");


for(i=0;i<nop;i++)
{
scanf("%d",&prf[i]);
}
}

void display()
{
int i,fault;
printf("\n your data is:");
/*for(i=1;i<=nop;i++)
{
printf("\t%d",prf[i]);
}*/
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}

void init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}
int search(int pg)
{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==pg)
return j;
}
return -1;
}

int minpos()
{
int i,z,index;
int max=-1;
for(i=0;i<nop;i++)
{
if(pos[i]>=max)
{
max=pos[i];
index=i;
}
}
return index;
}

void opti()
{
int i,j,x,k,z,fault=0,flag;
for(i=0;i<nop;i++)
{
flag=0;
x=search(prf[i]);
if(x==-1)
{
fault++;
for(j=0;j<nof;j++)
{
if(frame[j]==-1)
{
frame[j]=prf[i];
flag=1;
break;
}}

if(flag==0)
{
for(j=0;j<nof;j++)
{
for(k=i+1;k<nop;k++)
{
if(frame[j]==prf[k])
{
pos[j]=k;
printf("\n position:%d",pos[j]);
break;
}
else
pos[j]=99;
}}
z=minpos();
frame[z]=prf[i];
}
}
display();
printf("\n no of page fault:%d",fault);
}
}

main()
{
accept();
init();
display();
opti();
display();
}

Slot2
1.mfu

#include<stdio.h>
#include<stdlib.h>
int page[50],frame[5],pos[50];
int nop,nof;
void accept()
{
int i;
printf("\nEnter the no.of frames :");
scanf("%d",&nof);
printf("\nEnter the no.of pages :");
scanf("%d",&nop);
printf("\nEnter String :");
for(i=0;i<nop;i++)
{
scanf("%d",&page[i]);
}
}

int init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}

int search(int page)


{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==page)
{
return j;
}
}
return -1;
}

int maxpos()
{
int max=-1;
int index,i;
for(i=0;i<nof;i++)
{
if(pos[i]>=max)
{
max=pos[i];
index=i;
}
}
return index;
}

int mfu()
{
int insert=0;
int x,pgfault=0,i,j,k,z;
for(i=0;i<nop;i++)
{
insert=0;
x=search(page[i]);
if(x==-1)
{
pgfault++;
for(j=0;j<nof;j++)
{
if(frame[j]==-1)
{
frame[j]=page[i];
insert=1;
display();
break;
}
}
//}
if(insert==0)
{
for(j=0;j<nof;j++)
{
int cnt=0;
for(k=i-1;k>=0;k--)
{
if(frame[j]==page[k])
{
pos[j]=cnt++;
printf("\nCount are:\t%d",pos[j]);
}
}
}
z=maxpos();
printf("\nMIN:\t%d",z);
frame[z]=page[i];
}
}
display();

printf("\nPageFault:%d\n",pgfault);
}

int display()
{
int i;
printf("\nFrame");
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}

int main()
{
int i,j;
printf("\nEnter details:");
accept();
init();
display();
mfu();

2.lfu

//lfu
#include<stdio.h>
#include<stdlib.h>
int page[50],frame[5],pos[50];
int nop,nof;
void accept()
{
int i;
printf("\nEnter the no.of frames :");
scanf("%d",&nof);
printf("\nEnter the no.of pages :");
scanf("%d",&nop);
printf("\nEnter String :");
for(i=0;i<nop;i++)
{
scanf("%d",&page[i]);
}
}

int init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}

int search(int page)


{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==page)
{
return j;
}
}
return -1;
}

int minpos()
{
int min=99;
int index,i;
for(i=0;i<nof;i++)
{
if(pos[i]<=min)
{
min=pos[i];

index=i;
}
}
return index;
}

int lfu()
{
int insert=0;
int x,pgfault=0,i,j,k,z;
for(i=0;i<nop;i++)
{
insert=0;
x=search(page[i]);
if(x==-1)
{
pgfault++;
for(j=0;j<nof;j++)
{
if(frame[j]==-1)
{
frame[j]=page[i];
insert=1;
display();
break;
}
}
//}
if(insert==0)
{
for(j=0;j<nof;j++)
{
for(k=i-1;k>=0;k--)
{
int cnt=0;
if(frame[j]==page[k])
{
pos[j]=cnt++;
printf("\nCount are:\t%d",pos[j]);

}
}
}
z=minpos();
printf("\nMIN:\t%d",z);
frame[z]=page[i];
}
}
display();

printf("\nPageFault:%d\n",pgfault);
}

int display()
{
int i;
printf("\nFrame");
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}
int main()
{
int i,j;
printf("\nEnter details:");
accept();
init();
display();
lfu();

3.mru

//mru

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
//#include<wait.h>
#include<unistd.h>
#include<stdlib.h>
int nop,nof,pgref[20],frame[5],pg,pos[10];

void accept()
{
int i;
printf("Enter the number of pages: ");
scanf("%d",&nop);
printf("\nEnter the number of frames: ");
scanf("%d",&nof);
printf("\nEnter the data: ");
for(i=0;i<nop;i++)
{
scanf("%d",&pgref[i]);
}
}

void init()
{
int i;
for(i=0;i<nof;i++)
{
frame[i]=-1;
}
}

void display()
{
int i;
printf("\n\nEntered Data: ");
for(i=0;i<nof;i++)
{
printf("\n%d",frame[i]);
}
}

int search(int pg)


{
int j;
for(j=0;j<nof;j++)
{
if(frame[j]==pg)
return j;
}
return -1;
}

int maxpos()
{
int i,max=-1,index;
for(i=0;i<nof;i++)
{
if(pos[i]>max)
{
max=pos[i];
index=i;
}
}
return index;
}

void MRU()
{
int i,j,x,pgflt=0,k,flag,z;
for(i=0;i<nop;i++)
{
flag=0;
x=search(pgref[i]);
if(x==-1)
{
pgflt++;
for(j=0;j<nof;j++)
{
if(frame[j]==-1)
{
frame[j]=pgref[i];
flag=1;
break;
}
}

if(flag==0)
{
for(j=0;j<nof;j++)
{
for(k=i-1;k>=0;k--)
{
if(frame[j]==pgref[k])
{
pos[j]=k;
printf("\nPosition=%d",pos[j]);
break;
}
}
}
z=maxpos();
printf("\nMaxpos=%d",z);
frame[z]=pgref[i];
}
}
display();
}
printf("\nPage fault=%d",pgflt);
}

main()
{
accept();
init();
display();
MRU();
display();
}

You might also like