0% found this document useful (0 votes)
4 views37 pages

OS Record Pages 2

The document contains a list of programming tasks related to CPU scheduling algorithms, memory management techniques, and UNIX/Linux system calls. Each task includes a brief description and a C program implementation for various scheduling algorithms such as FCFS, SJF, Round Robin, and Priority scheduling, along with examples of I/O system calls. The document serves as a guide for students to understand and implement these concepts in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

OS Record Pages 2

The document contains a list of programming tasks related to CPU scheduling algorithms, memory management techniques, and UNIX/Linux system calls. Each task includes a brief description and a C program implementation for various scheduling algorithms such as FCFS, SJF, Round Robin, and Priority scheduling, along with examples of I/O system calls. The document serves as a guide for students to understand and implement these concepts in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

2

LIST OF PROGRAMS

S.NO NAME OF THE PROGRAM PAGE NO


1 Write c program to simulate FCFS CPU scheduling
algorithm
2 Write c program to simulate SJF( non-preemptive)
scheduling algorithm
3 Write c program to simulate SJF(preemptive)
scheduling algorithm
4 Write c program to simulate Round Robin scheduling
algorithm
5 Write c program to simulate Priority scheduling
algorithm
6 Write programs using the I/O system calls of UNIX/
LINUX operating system : read system call
7 Write programs using the I/O system calls of UNIX/
LINUX operating system : read and write system calls

8 Write a c program to simulate Bankers Algorithm for


Deadlock Avoidance.
9 Write a c program to implement the Producer-
Consumer problem using semaphores using UNIX/
LINUX system calls
10 Write a program to simulate PAGING memory
management technique
11 Write a program to stimulate Segmentation memory
management technique
12 Write a program to illustrate the IPC mechanism –FIFO

13 Program to implement Page Replacement algorithms

I) FIFO
14 II) LRU-Least recently used

15 III) Optimal

JNTU college of engineering, Hyderabad


3

1. Write a c program to simulate FCFC CPU scheduling algorithm

#include<stdio.h>
int main()
{
int p[10],at[10],bt[10],ct[10],tat[10],wt[10],i,j,temp=0,n;
oat awt=0,atat=0;
prin ("enter no of proccess you want:");
scanf("%d",&n);
prin ("enter %d process:",n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
prin ("enter %d arrival me:",n);
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
}
prin ("enter %d burst me:",n);
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
// sor ng at,bt, and process according to at
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(at[j]>at[j+1])
{
temp=p[j+1];
p[j+1]=p[j];
p[j]=temp;
temp=at[j+1];
at[j+1]=at[j];
at[j]=temp;
temp=bt[j+1];
bt[j+1]=bt[j];

JNTU college of engineering, Hyderabad


fl
t
t
t
t
ti
ti
ti
4

bt[j]=temp;
}
}
}
/* calcula ng 1st ct */
ct[0]=at[0]+bt[0];
/* calcula ng 2 to n ct */
for(i=1;i<n;i++)
{
//when proess is ideal in between i and i+1
temp=0;
if(ct[i-1]<at[i])
{
temp=at[i]-ct[i-1];
}
ct[i]=ct[i-1]+bt[i]+temp;
}
/* calcula ng tat and wt */
prin ("\np\t A.T\t B.T\t C.T\t TAT\t WT");
for(i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
atat+=tat[i];
awt+=wt[i];
}
atat=atat/n;
awt=awt/n;
for(i=0;i<n;i++)
{
prin ("\nP%d\t %d\t %d\t %d \t %d \t %d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);
}
prin ("\naverage turnaround me is %f",atat);

prin ("\naverage wa ng mme is %f",awt);


return 0;
}

JNTU college of engineering, Hyderabad


t
t
t
t
ti
ti
ti
ti
ti
ti
5

Output:

JNTU college of engineering, Hyderabad


6

2.Write the c program to simulate SJF(non preemp ve)scheduling algorithm.

#include<stdio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
oat avg_wt,avg_tat;
prin (“Implementa on of SJF:\n”);
prin ("Enter number of process:");
scanf("%d",&n);

prin ("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
prin ("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sor ng burst me in ascending order using selec on sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //wai ng me for rst process will be zero

JNTU college of engineering, Hyderabad


fl
tf
tf
tf
ti
tf
ti
ti
ti
ti
fi
ti
ti
7

//calculate wai ng me
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=( oat)total/n; //average wai ng me


total=0;

prin ("\nProcess\t Burst Time \tWai ng Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround me
total+=tat[i];
prin ("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=( oat)total/n; //average turnaround me


prin ("\n\nAverage Wai ng Time=%f",avg_wt);
prin ("\nAverage Turnaround Time=%f\n",avg_tat);
}

Output:

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
fl
fl
ti
ti
ti
ti
ti
ti
ti
ti
8

3.Write a program to implement SJF(preemptive)

#include <stdio.h>
struct prg {
int ariv;
int prgno;
int bt;
int bt1;
int wt;
int tt;
int check;
};
float average(struct prg a[], int n) {
int i;
float sum = 0;
for (i = 0; i < n; i++) {
sum = sum +(float) a[i].wt;
}
return sum / n;
}
void main() {
struct prg p[10], temp;
int n,shot, count = 0, i = 0, j, flg = 0, sum = 0, time = 0, fg = 0, current
= 0;
printf("enter no of processes:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter process number:");
scanf("%d", &p[i].prgno);
printf("arrivel:");
scanf("%d", &p[i].ariv);
printf("burst time:");
scanf("%d", &p[i].bt);
p[i].bt1 = p[i].bt;

p[i].check = 0;
}

JNTU college of engineering, Hyderabad


9

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


flg = 0;
for (j = 0; j < n - i - 1; j++) {
if (p[j].bt > p[j + 1].bt) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
flg = 1;
}
}
if (flg == 0)
break;
}
shot=0;
while (count != n ) {

if(p[shot].ariv>time){
shot++;
}else{
p[shot].bt=p[shot].bt-1;
if(p[shot].bt==0){
p[shot].tt=time-p[shot].ariv;
p[shot].wt=p[shot].tt-p[shot].bt1;
count++;
}
for(i=0;i<n;i++){
flg=0;
for(j = 0; j < n - i - 1; j++) {
if (p[j].bt > p[j + 1].bt) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
flg = 1;
}
}
if (flg == 0)
break;

JNTU college of engineering, Hyderabad


10

}shot=0;
while(p[shot].bt==0){
shot++;
}

}time++;
}

printf("process:\ttotal\twait\n");
for (i = 0; i < n; i++) {
printf("process %d:\t\t%d\t%d\n", p[i].prgno, p[i].tt, p[i].wt);
}
printf("the average waiting time is : %f", average(p, n));
}

OUTPUT:

JNTU college of engineering, Hyderabad


11

4.WRITE A C PROGRAM TO SIMULATE ROUND ROBIN SCHEDULING ALGORITHM

#include<stdio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
oat awt,atat;
prin ("Implementa on of Round Robin algorithm\n");
prin ("enter the number of processes:");
scanf("%d",&n);
prin ("enter the burst me of each process \n");
for(i=0;i<n;i++)
{
prin ("p %d :",i+1);
scanf("%d",&bt[i]);
st[i]=bt[i];
}
prin ("enter the me quantum");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
con nue;
}
if(st[i]>tq)
st[i]=st[i]-tq;

else
if(st[i]>=0)

JNTU college of engineering, Hyderabad


fl
tf
ti
tf
tf
tf
tf
ti
ti
ti
12

{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=( oat)swt/n;
atat=( oat)stat/n;
prin ("process no\t burst me\t wai ng me\t turnaround me\n");
for(i=0;i<n;i++)
prin ("%d\t\t %d\t\t %d\t\t %d\n",i+1,bt[i],wt[i],tat[i]);
prin ("avg wt me=%f,avg turn around me=%f",awt,atat);
}

Output:

JNTU college of engineering, Hyderabad


tf
tf
tf
fl
fl
ti
ti
ti
ti
ti
ti
13

5.Write a c program to simulate the Priority scheduling algorithm

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pri[20],i,j,k,n,total=0,pos,temp;
oat avg_wt,avg_tat;
prin ("Implementa on of Priority algorithm\n");
prin ("Enter number of process:");
scanf("%d",&n);
prin ("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
prin ("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
prin (" enter priority of the process ");
for(i=0;i<n;i++)
{
p[i] = i;
//prin ("Priority of Process");
prin ("p%d ",i+1);
scanf("%d",&pri[i]);

JNTU college of engineering, Hyderabad


fl
tf
tf
tf
tf
tf
tf
tf
ti
14

}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wt[0]=0; //wai ng me for rst process will be zero
//calculate wai ng me
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=( oat)total/n; //average wai ng me
total=0;
prin ("\nProcess\t Burst Time \tPriority \tWai ng Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround me
total+=tat[i];
prin ("\np%d\t\t %d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],pri[i],wt[i],tat[i]);
}
avg_tat=( oat)total/n; //average turnaround me
prin ("\n\nAverage Wai ng Time=%f",avg_wt);
prin ("\nAverage Turnaround Time=%f\n",avg_tat);
}
Output:

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
fl
fl
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
15

JNTU college of engineering, Hyderabad


16

6.Write a c program to illustrate the system calls of UNIX/LINUX opera ng


system.

#reading from a le and displaying its content

#include<stdio.h>
#include<sys/types.h>
#include<sys/fcntl.h>
#include<sys/stat.h>
#include<sys/unistd.h>
#include<string.h>
int main(){
int sz;
char C[100];
int fd=open("foo1.txt",O_RDONLY);

if(fd>0){

sz=read(fd,C,10);
C[sz]='\0';
prin ("called read(%d,C,10) it reads 10 bytes into bu er \"%s\"",fd,C);
close(fd);
}

return 0;
}
Output:

JNTU college of engineering, Hyderabad


tf
fi
ff
ti
17

7.Write the c program using the I/O system calls of UNIX/LINUX opera ng
system

#reading and wri ng into le

#include<stdio.h>
#include<sys/types.h>
#include<sys/fcntl.h>
#include<sys/stat.h>
#include<sys/unistd.h>
#include<string.h>

int main(){
int sz;
char bu [100];
int fd=open("foo1.txt",O_RDWR);

if(fd>0){
int size=strlen("new lines");
sz=write(fd, "new lines", strlen("new lines"));
prin ("called write(%d, %s, %d)\n", fd, "new lines", size);
sz=read(fd,bu ,10);
bu [sz]=0;
prin ("called read(%d,bu ,10) it reads 10 bytes into bu er=\"%s\"",fd,bu );
close(fd);
}

return 0;
}

Output:
File before execu on:

JNTU college of engineering, Hyderabad


ff
tf
tf
ff
ff
ti
ti
ff
fi
ff
ff
ti
18

File a er execu on:

8.Write a c program to simulate Bankers Algorithm

#include<stdio.h>
#include<string.h>
#de ne max 100

JNTU college of engineering, Hyderabad


fi
ft
ti
19

int main(){
int alloc[max][max],maxi[max][max],need[max][max],avail[max],ans[max];
int p,r,index=0,i,j,k;

prin ("Enter no.of processes : ");


scanf("%d",&p);
prin ("Enter no.of resouruces : ");
scanf("%d",&r);
alloc[p][r],maxi[p][r],need[p][r],avail[p];
prin ("Enter Alloca on Matrix : ");
for ( i = 0; i < p; i++)
{
for (j = 0; j < r; j++)
{
scanf("%d",&alloc[i][j]);
}
}
prin ("Enter Max Matrix : ");
for ( i = 0; i < p; i++)
{
for (j = 0; j < r; j++)
{
scanf("%d",&maxi[i][j]);
}
}

prin ("Enter Available Matrix : ");


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]=maxi[i][j]-alloc[i][j];
}
}

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
tf
ti
20

int is_ nished[p];


memset(is_ nished,0,sizeof(is_ nished));
for ( k = 0; k < p; k++)
{
for ( i = 0; i < p; i++)
{
int ag=0;
if (is_ nished[i]==0)
{
for (j = 0; j < r; j++)
{
if(need[i][j]>avail[j])
{
ag=1;
break;
}
}
if ( ag==0)
{
for ( j = 0; j < r; j++)
{
avail[j]+=alloc[i][j];
}
ans[index++]=i;
is_ nished[i]=1;
}
}
}
}
int ag=0;
for (i = 0; i < p; i++)
{
if(is_ nished[i]== ag)
{
prin ("Unsafe State...");
ag=1;
}
}

JNTU college of engineering, Hyderabad


fl
fl
fl
fl
fi
fi
fi
fi
tf
fl
fi
fl
fi
21

if ( ag==0)
{
prin ("The Safe Sequence Is :");
for ( i = 0; i < p-1; i++)
{
prin ("P%d->",ans[i]);
}
prin ("P%d",ans[p-1]);
}
return 0;
}

Output:

9.Write a c program to implement the Producer-Consumer problem using


semaphores using UNIX/LINUX system calls

#include<stdio.h>

JNTU college of engineering, Hyderabad


fl
tf
tf
tf
22

#include<stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int main () {
int n;
void producer ();
void consumer ();
int wait (int);
int signal (int);
prin ("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
prin ("\nEnter your choice:");
scanf ("%d", &n);
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0))
producer ();
else
prin ("Bu er is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer ();
else
prin ("Bu er is empty!!");
break;
case 3:
exit (0);
break; }

}
return 0;
}
int wait (int s)
{
return (--s);
}
int signal (int s)

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
ff
ff
23

{
return (++s);
}
void producer ()
{
mutex = wait (mutex);
full = signal (full);
empty = wait (empty);
x++;
prin ("\nProducer produces the item %d", x);
mutex = signal (mutex);
}
void consumer ()
{
mutex = wait (mutex);
full = wait (full);
empty = signal (empty);
prin ("\nConsumer consumes item %d", x);
x--;
mutex = signal (mutex);
}

Output:

JNTU college of engineering, Hyderabad


tf
tf
24

JNTU college of engineering, Hyderabad


25

10.Write a c program to simulate Paging memory management technique.

#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, o set;
int s[10], fno[10][20];
prin ("\nEnter the memory size -- ");
scanf("%d",&ms);
prin ("\nEnter the page size -- ");
scanf("%d",&ps);
nop = ms/ps;
prin ("\nThe no. of pages available in memory are -- %d ",nop);
prin ("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
prin ("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
prin ("\nMemory is Full");
break;
}
rempages = rempages - s[i];

prin ("\nEnter pagetable for p[%d] --- ",i);


for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
prin ("\nEnter Logical Address to nd Physical Address ");
prin ("\nEnter process no. and pagenumber and o set -- ");
scanf("%d %d %d",&x,&y, &o set);
if(x>np || y>=s[i] || o set>=ps)
prin ("\nInvalid Process or Page Number or o set");

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
tf
tf
tf
tf
tf
tf
ff
ff
fi
ff
ff
ff
26

else
{ pa=fno[x][y]*ps+o set;
prin ("\nThe Physical Address is -- %d",pa);
}
return 0;
//getch();
}

Output:

JNTU college of engineering, Hyderabad


tf
ff
27

11. Write a program to s mulate segmenta on memory management


technique.
#include <stdio.h>
int main() {
int i, base = 0, o , np, ns, msize, ad = 0, ss[10],j;
prin ("Enter the memory size:");
scanf("%d", &msize);
prin ("Enter the number of segments:");
scanf("%d", &ns);
for (i = 0; i < ns; i++) {
prin ("Enter the segment %d size :", i + 1);
scanf("%d", &ss[i]);
ad = ad + ss[i];
if (ad > msize) {
prin ("segment %d can not be alloted due to insu cient memory", i + 1);
ad = ad - ss[i];
ss[i]=0;
}
}prin ("SEGMENT TABLE\n");
ad=0;
prin ("\t\t BASE\tOFFSET\n");
for(i=0;i<ns;i++){
prin ("sgment:%d %d \t %d\n",i+1,ad,ad+ss[i]);
ad=ad+ss[i];

OUTPUT:

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
tf
tf
tf
ff
ti
ti
ffi
28

12.Write the c program to illustrate FIFOS IPC mechanism.

#include <stdio.h>
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
prin (" Incoming \t\t Frame 1 \t\t Frame 2 \t\t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{

JNTU college of engineering, Hyderabad


tf
29

if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
prin ("\n");
prin ("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
prin (" %d\t\t\t", temp[n]);
else
prin (" - \t\t\t");
}
}
prin ("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}

Output:

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
tf
30

13.Program to implement Page Replacement algorithms


I) FIFO

#include <stdio.h>

int main() {

int n, top = -1, i, c = 0, count = 0, a, ag = 0, fault = 0;

prin ("Enter the numbeer of frames:");

scanf("%d", &n);

int p[n];

prin ("Enter number of elements in reference text:");

scanf("%d",&c);

while (c--) {

ag = 0;

prin ("Enter the page number:");

scanf("%d", &a);

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

if (p[i] == a)

ag = 1;

if ( ag == 0) {

if (count < n)

count++;

top = (top + 1) % n;

p[top] = a;

prin ("page fault\n");

fault++;

JNTU college of engineering, Hyderabad


fl
fl
fl
tf
tf
tf
tf
fl
31

} else

prin ("Page already exists no page fault\n");

prin ("Total number of page faults = %d\n", fault);

OUTPUT:

JNTU college of engineering, Hyderabad


tf
tf
32

II)LRU

#include <stdio.h>

typedef struct details {

int number;

int me;

} det;

typedef struct pros {

det prog;

} pros;

int main() {

int n, i, ag = 0, count = 0, c = 1, a, top = -1, mint, min, fault = 0;

prin ("Enter the number of frames:");

scanf("%d", &n);

pros p[n];

prin ("Enter number of elements in reference text:");

scanf("%d",&c);

while (c --) {

ag = 0;

prin ("Enter the page number:");

scanf("%d", &a);

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

JNTU college of engineering, Hyderabad


fl
ti
tf
tf
tf
fl
33

if (p[i].prog.number == a) {

p[i].prog. me++;

ag = 1;

if ( ag == 0) {

if (count < n) {

top = top + 1;

p[top].prog.number = a;

p[top].prog. me = 1;

count++;

} else {

min = 0;

mint = p[0].prog. me;

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

if (p[i].prog. me < mint) {

mint = p[i].prog. me;

min = i;

p[min].prog.number = a;

p[min].prog. me = 1;

fault++;

JNTU college of engineering, Hyderabad


fl
fl
ti
ti
ti
ti
ti
ti
34

prin ("Page fault\n");

} else

prin ("No page fault page already exists\n");

prin ("T0tal number of page faults = %d\n", fault);

OUTPUT:

JNTU college of engineering, Hyderabad


tf
tf
tf
35

III)OPTIMAL

#include <stdio.h>

int search(int a[], int size, int key) {

int i;

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

if (a[i] == key)

return 1;

return 0;

int largest(int ar[], int size, int top, int key) {

int i = 0, m = 0;

for (i = top + 1; i < size; i++) {

if (ar[i] == key) {

return i;

return 0;

int main() {

int a[100];

int nf, n, i, count = 0, hit = 0, fault = 0, max, j, ag = 0, maxidx;

prin ("Enter the frame size:");

JNTU college of engineering, Hyderabad


tf
fl
36

scanf("%d", &nf);

prin ("Enter length of string");

scanf("%d",&n);

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

scanf("%d",&a[i]);

int frame[nf];

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

if (count < nf) {

if (search(frame, count, a[i])) {

hit++;

prin ("hit-%d-\n", a[i]);

} else {

fault++;

prin ("fault-%d-\n", a[i]);

frame[count] = a[i];

count++;

} else {

if (search(frame, nf, a[i])) {

hit++;

prin ("hit-%d-\n", a[i]);

} else {

fault++;

JNTU college of engineering, Hyderabad


tf
tf
tf
tf
37

prin ("fault-%d-\n", a[i]);

ag = 0;

max = 0;

for (j = 0; j < nf; j++) {

if (largest(a, n, i, frame[j]) == 0) {

frame[j] = a[i];

ag = 1;

break;

} else if (max < largest(a, n, i, frame[j])) {

max = largest(a, n, i, frame[j]);

maxidx = j;

if (! ag) {

frame[maxidx] = a[i];

for (j = 0; j < count; j++) {

prin ("%d ", frame[j]);

prin ("\n");

prin ("no of page faults =%d\nno of hits =%d\n", fault, hit);

JNTU college of engineering, Hyderabad


fl
tf
tf
fl
tf
fl
tf
38

OUTPUT:

JNTU college of engineering, Hyderabad

You might also like