0% found this document useful (0 votes)
5 views65 pages

BSC OS Programs Manual

The document contains multiple C programs that implement various CPU scheduling algorithms including Round Robin, Shortest Job First (SJF), First-Come-First-Serve (FCFS), and Priority scheduling. Additionally, it covers file allocation strategies such as Sequential, Indexed, and Linked file allocation, as well as memory management techniques like MVT and MFT. Each section includes code snippets and example outputs demonstrating how the algorithms and strategies function.

Uploaded by

B happy
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)
5 views65 pages

BSC OS Programs Manual

The document contains multiple C programs that implement various CPU scheduling algorithms including Round Robin, Shortest Job First (SJF), First-Come-First-Serve (FCFS), and Priority scheduling. Additionally, it covers file allocation strategies such as Sequential, Indexed, and Linked file allocation, as well as memory management techniques like MVT and MFT. Each section includes code snippets and example outputs demonstrating how the algorithms and strategies function.

Uploaded by

B happy
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/ 65

1.

Write a program to implement ROUND ROBIN CPU Scheduling Algorithm:

#include<stdio.h>

Int main() {

Int i, limit, total = 0, x, counter = 0, time_quantum;

Int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];

Float average_wait_time, average_turnaround_time;

Printf(“nEnter Total Number of Processes:t”);

Scanf(“%d”, &limit);

X = limit;

For(i = 0; i < limit; i++)

Printf(“nEnter Details of Process*%d+n”, i + 1);

Printf(“Arrival Time:t”);

Scanf(“%d”, &arrival_time*i+);

Printf(“Burst Time:t”);

Scanf(“%d”, &burst_time*i+);

Temp[i] = burst_time[i];

Printf(“nEnter Time Quantum:t”);

Scanf(“%d”, &time_quantum);

Printf(“nProcess IDttBurst Timet Turnaround Timet Waiting Timen”);

For(total = 0, i = 0; x != 0;)

If(temp[i] <= time_quantum && temp[i] > 0)

Total = total + temp[i];

Temp[i] = 0;

Counter = 1;

} else if(temp[i] > 0)

{
Temp[i] = temp[i] – time_quantum;

Total = total + time_quantum;

} if(temp[i] == 0 && counter == 1)

{ x--;

Printf(“nProcess*%d+tt%dtt %dttt %d”, i + 1, burst_time*i+, total – arrival_time[i], total –


arrival_time[i] – burst_time[i]);

Wait_time = wait_time + total – arrival_time[i] – burst_time[i];

Turnaround_time = turnaround_time + total – arrival_time[i];

Counter = 0;

If(i == limit – 1)

I = 0; }

Else if(arrival_time[i + 1] <= total)

I++;

} else

{ i = 0;

} }

Average_wait_time = wait_time * 1.0 / limit;

Average_turnaround_time = turnaround_time * 1.0 / limit;

Printf(“nnAverage Waiting Time:t%f”, average_wait_time);

Printf(“nAvg Turnaround Time:t%fn”, average_turnaround_time);

Return 0;

}
Output:

Total number of process in the system: 3

Enter the Arrival and Burst time of the Process[1]

Arrival time is: 1

Burst time is: 2

Enter the Arrival and Burst time of the Process[2]

Arrival time is: 1

Burst time is: 2

Enter the Arrival and Burst time of the Process[3]

Arrival time is: 3

Burst time is: 4

Enter the Time Quantum for the process: 2

Process No Burst Time TAT Waiting Time

Process No[1] 2 1 -1

Process No[2] 2 3 1

Process No[3] 4 5 1

Average Turn Around Time: 0.333333

Average Waiting Time: 3.000000


2. Simulate SJF CPU Scheduling Algorithm:

#include<stdio.h>

Int main() {

Int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;

Float avg_wt,avg_tat;

Printf(“Enter number of process:”);

Scanf(“%d”,&n);

Printf(“nEnter Burst Time:n”);

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

Printf(“p%d:”,i+1);

Scanf(“%d”,&bt*i+); p*i+=i+1;

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;

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

{ wt[i]=0;

For(j=0;j<i;j++)

Wt[i]+=bt[j];
Total+=wt[i]; }

Avg_wt=(float)total/n;

Total=0;

Printf(“nProcesst Burst Time tWaiting TimetTurnaround Time”);

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

{ tat[i]=bt[i]+wt[i];

Total+=tat[i];

Printf(“np%dtt %dtt %dttt%d”,p*i+,bt*i+,wt*i+,tat*i+);

Avg_tat=(float)total/n;

Printf(“nnAverage Waiting Time=%f”,avg_wt);

Printf(“nAverage Turnaround Time=%fn”,avg_tat);

}
Output:

Enter number of process: 2

Enter Burst Time:

p1:32

p2:43

Process Burst Time Waiting Time Turnaround Time

p1 32 0 32

p2 43 32 75

Average Waiting Time=16.000000

Average Turnaround Time=53.500000


3. Write a program the FCFS CPU Scheduling Algorithm:

#include<stdio.h>

int main()

{ int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

Printf(“Enter total number of processes:”);

Scanf(“%d”,&n);

Printf(“nEnter Process Burst Timen”);

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

{ Printf(“P*%d+:”,i+1);

Scanf(“%d”,&bt*i+);

Wt[0]=0;

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

Wt[i]=0;

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

Wt[i]+=bt[j];

Printf(“nProcessttBurst TimetWaiting TimetTurnaround Time”);

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

Tat[i]=bt[i]+wt[i];

Avwt+=wt[i];

Avtat+=tat[i];

Printf(“nP*%d+tt%dtt%dtt%d”,i+1,bt*i+,wt*i+,tat*i+);

Avwt/=i; Avtat/=i;

Printf(“nnAverage Waiting Time:%d”,avwt);

Printf(“nAverage Turnaround Time:%d”,avtat);

return 0; }
Output:

Enter total number of processes:3

Enter process Burst Time p[1]:5

p[2]:7

p[3]:2

process Burst Time waiting Time Turnaround Time

p[1] 5 0 5

p[2] 7 5 12

p[3] 2 12 14

Average waiting Time: 5 average Turnaround Time:10


4. Write a program to Priority CPU Scheduling Algorithm:

#include<stdio.h>

#include<conio.h>

void main()

{ int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;

Printf(“Enter the number of process : “);

Scanf(“%d”,&n);

Printf(“\n Enter process : time priorities \n”);

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

Printf(“\nProcess no %d : “,i+1);

Scanf(“%d %d”,&pt*i+,&pp*i+);

P[i]=i+1; }

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

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

if(pp[i]<pp[j])

X=pp[i];

Pp[i]=pp[j];

Pp[j]=x;

X=pt[i];

Pt[i]=pt[j];

Pt[j]=x;

X=p[i];

P[i]=p[j];

P[j]=x; } } }

W[0]=0;

Awt=0;
T[0]=pt[0];

atat=t[0];

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

{ w[i]=t[i-1];

awt+=w[i];

T[i]=w[i]+pt[i];

atat+=t[i];

- printf(“\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n”);

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

Printf(“\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n”,p*i+,pt*i+,w*i+,t*i+,pp*i+);

Awt/=n;

Atat/=n;

Printf(“\n Average Wait Time : %d \n”,awt);

Printf(“\n Average Turn Around Time : %d \n”,atat);

getch();

}
Output:

Enter no. of process: 3

Enter process: time priorities

Process no. 1:68

Job Burst Time Wait Time Turn around Time Priority

1 68 0 68 3

0 0 68 68 0

0 0 68 68 -720880432

Average Wait Time: 45

Average Turn around Time: 68


5. Simulate Sequential File Allocation Strategies:

#include<stdio.h>

#include<conio.h>

void main()

int f[50], i, st, len, j, c, k, count = 0;

clrscr();

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

{f[i]=0;}

printf(“Files Allocated are : \n”);

X: count=0;

Printf(“Enter starting block and length of files: “);

scanf(“%d%d”, &st,&len);

for(k=st;k<(st+len);k++)

{ if(f[k]==0)

count++;}

if(len==count)

for(j=st;j<(st+len);j++)

if(f[j]==0)

F[j]=1;

printf(“%d\t%d\n”,j,f*j+);

if(j!=(st+len-1))

printf(“The file is allocated to disk\n”);

else
printf(“ The file is not allocated \n”);

printf(“Do you want to enter more file(Yes – 1/No – 0)”);

scanf(“%d”, &c);

if(c==1){

goto x;}

getch();

}
Output:

Files Allocated are:

Enter Starting Blocks and length of files: 3 10

3 1

4 1

5 1

6 1

7 1

8 1

9 1

10 1

11 1

12 1

The file is allocated to disk

Do you want to enter more file (yes-1/ no-0)


6. Simulate Indexed File Allocation Strategies:

#include<stdio.h>

#include<conio.h>

#include<stdio.h>

void main()

int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;

clrscr();

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

F[i]=0;

x:printf(“Enter the index block: “);

scanf(“%d”,&ind);

if(f[ind]!=1)

Printf(“Enter no of blocks needed and no of files for the index %d on the disk : \n”, ind);

Scanf(“%d”,&n);

else

Printf(“%d index is already allocated \n”,ind);

goto x;

y: count=0;

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

scanf(“%d”, &index*i+);

if(f[index[i]]==0)

count++;

if(count==n)
{

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

f[index[j]]=1;

Printf(“Allocated\n”);

Printf(“File Indexed\n”);

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

Printf(“%d------%d : %d\n”,ind,index*k+,f*index*k++);

else

Printf(“File in the index is already allocated \n”);

Printf(“Enter another file indexed”);

goto y;

Printf(“Do you want to enter more file(Yes – 1/No – 0)”);

Scanf(“%d”, &c);

if(c==1)

goto x;

else

exit(0);

getch();

}
Output :

Enter the index block: 3

Enter no. of blocks needed and no. of files for the index 3 on the disk: 2

Trri

Allocated

File indexed

3-----0:1

3-----0:1

Do you want to enter more file (yes -1/ No-0)


7. Simulate Linked File Allocation Strategies:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int f[50], p,i, st, len, j, c, k, a;

clrscr();

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

F[i]=0;

Printf(“Enter how many blocks already allocated: “);

Scanf(“%d”,&p);

Printf(“Enter blocks already allocated: “);

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

scanf(“%d”,&a);

F[a]=1;

}
X: printf(“Enter index starting block and length: “);

scanf(“%d%d”, &st,&len);

K=len;

if(f[st]==0)

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

if(f[j]==0)

F[j]=1;

printf(“%d------%d\n”,j,f*j+);

else

printf(“%d Block is already allocated \n”,j);

k++;
}

else

printf(“%d starting block is already allocated \n”,st);

printf(“Do you want to enter more file(Yes – 1/No – 0)”);

scanf(“%d”, &c);

if(c==1)

goto x;

else

exit(0);

getch();

}
Output:

Enter how many blocks already allocated: 3

Enter blocks already allocated: 5

Enter index starting block and length:

4 7 9

4------1

5 Block is already allocated

6------1

7------1

8------1

9------1

10------1

11------1
8. Simulate MVT and MFT Memory Management Techniques:

#include<stdio.h>

#include<conio.h>

Void main()

{ int i,os_m,nPage,total,pg[25];

clrscr();

printf("\nEnter total memory size:");

scanf("%d",&total);

printf("\nEnter memory for OS:");

scanf("%d",&os_m);

printf("\nEnter no. Of pages:");

scanf("%d",&nPage);

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

{ printf("Enter size of page[%d]:",i+1);

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

total=total-os_m;

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

{ if(total>=pg[i])

{ printf("\n Allocate page %d",i+1);

total=total-pg[i]; }

else{

printf("\n page %d is not allocated due to insufficient memory.",i+1);

} }printf("\n External Fragmentation is:%d",total);

getch();

}
Output:

Enter total memory size: 32

Enter memory for OS: 4

Enter no. Of pages: 3

Enter size of page[1]:5

Enter size of page[2]:5

Enter size of page[3]:5

Allocate page 1

Allocate page 2

Allocate page 3

External Fragmentation is: 13


9. Simulate Single Level Directory File Organization Techniques:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int nf=0,i=0,j=0,ch;

char mdname[10],fname[10][10],name[10];

clrscr();

printf("Enter the directory name:");

scanf("%s",mdname);

printf("Enter the number of files:");

scanf("%d",&nf);

do

printf("Enter file name to be created:");


scanf("%s",name);

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

if(!strcmp(name,fname[i]))

break;

if(i==nf)

strcpy(fname[j++],name);

nf++;

else

printf("There is already %s\n",name);

printf("Do you want to enter another file(yes – 1 or no – 0):");

scanf("%d",&ch);
}

while(ch==1);

printf("Directory name is:%s\n",mdname);

printf("Files names are:");

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

printf("\n%s",fname[i]);

getch();

}
Output:

Enter the directory name: Computers

Enter the number of files: 4

Enter file name to be created: System

Do you want to enter another file (yes – 1 or no – 0):1

Enter file name to be created: Operating

Do you want to enter another file (yes – 1 or no – 0):0

Directory name is: Computers

Files names are:

System

Operating
10. Simulate Two Level File Organization Techniques:

#include<string.h>

#include<stdlib.h>

#include<stdio.h>

struct

char dname[10],fname[10][10];

int fcnt;

}dir[10];

void main()

int i,ch,dcnt,k;

char f[30], d[30];

dcnt=0;

while(1)

printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");

printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice – ");

scanf("%d",&ch);

switch(ch)

case 1: printf("\nEnter name of directory – ");

scanf("%s", dir[dcnt].dname);

dir[dcnt].fcnt=0;

dcnt++;

printf("Directory created");

break;

case 2: printf("\nEnter name of the directory –");

scanf("%s",d);

for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file – ");

scanf("%s",dir[i].fname[dir[i].fcnt]);

printf("File created");

break;

if(i==dcnt)

printf("Directory %s not found",d);

break;

case 3: printf("\nEnter name of the directory – ");

scanf("%s",d);

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

if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file – ");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is deleted ",f);

dir[i].fcnt--;

strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);

goto jmp;

printf("File %s not found",f);

goto jmp;

}
}

printf("Directory %s not found",d);

jmp : break;

case 4: printf("\nEnter name of the directory – ");

scanf("%s",d);

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

if(strcmp(d,dir[i].dname)==0)

printf("Enter the name of the file –");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is found ",f);

goto jmp1;

printf("File %s not found",f);

goto jmp1;

printf("Directory %s not found",d);

jmp1: break;

case 5: if(dcnt==0)

printf("\nNo Directory’s ");

else

printf("\nDirectory\tFiles");

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

printf("\n%s\t\t",dir[i].dname);

for(k=0;k<dir[i].fcnt;k++)

printf("\t%s",dir[i].fname[k]);

break;

default:exit(0);

}
Output :

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice – 1

Enter name of directory – computers

Directory created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice – 2

Enter name of the directory –computers

Enter name of the file – OS

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice – 5

Directory Files

computers

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice – 6


11. Simulate Hierarchical File Organization Techniques :

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int nf=0,i=0,j=0,ch;

char mdname[10],fname[10][10],name[10];

clrscr();

printf("Enter the directory name:");

scanf("%s",mdname);

printf("Enter the number of files:");

scanf("%d",&nf);

do

{
printf("Enter file name to be created:");

scanf("%s",name);

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

if(!strcmp(name,fname[i]))

break;

if(i==nf)

strcpy(fname[j++],name);

nf++;

else

printf("There is already %s\n",name);

printf("Do you want to enter another file(yes – 1 or no – 0):");


scanf("%d",&ch);

while(ch==1);

printf("Directory name is:%s\n",mdname);

printf("Files names are:");

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

printf("\n%s",fname[i]);

getch();

}
Output:

Enter the directory name: computers

Enter the number of files:3

Enter file name to be created: operating

Do you want to enter another file(yes – 1 or no – 0):1

Enter file name to be created: system

Do you want to enter another file(yes – 1 or no – 0):0

Directory name is: computers

Files names are:

operating

system
12. Write a Program for Bankers Algorithm for Dead lock Avoidance:

#include <stdio.h>

int current[5][5], maximum_claim[5][5], available[5];

int allocation[5] = {0, 0, 0, 0, 0};

int maxres[5], running[5], safe = 0;

int counter = 0, i, j, exec, resources, processes, k = 1;

int main()

printf("\nEnter number of processes: ");

scanf("%d", &processes);

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

running[i] = 1;

counter++;

printf("\nEnter number of resources: ");

scanf("%d", &resources);

printf("\nEnter Claim Vector:");

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

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

printf("\nEnter Allocated Resource Table:\n");

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


{

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

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

printf("\nEnter Maximum Claim Table:\n");

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

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

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

printf("\nThe Claim Vector is: ");

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

printf("\t%d", maxres[i]);

printf("\nThe Allocated Resource Table:\n");

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

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

printf("\t%d", current[i][j]);

printf("\n");

}
printf("\nThe Maximum Claim Table:\n");

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

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

printf("\t%d", maximum_claim[i][j]);

printf("\n");

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

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

allocation[j] += current[i][j];

printf("\nAllocated resources:");

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

printf("\t%d", allocation[i]);

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

available[i] = maxres[i] - allocation[i];

printf("\nAvailable resources:");
for (i = 0; i < resources; i++)

printf("\t%d", available[i]);

printf("\n");

while (counter != 0)

safe = 0;

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

if (running[i])

exec = 1;

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

if (maximum_claim[i][j] - current[i][j] > available[j])

exec = 0;

break;

if (exec)

printf("\nProcess%d is executing\n", i + 1);

running[i] = 0;

counter--;

safe = 1;

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


{

available[j] += current[i][j];

break;

if (!safe)

printf("\nThe processes are in unsafe state.\n");

break;

else

printf("\nThe process is in safe state");

printf("\nAvailable vector:");

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

printf("\t%d", available[i]);

printf("\n");

return 0;

}
Output:

Enter number of processes: 2

Enter number of resources: 2

Enter Claim Vector: 3

Enter Allocated Resource Table:

Enter Maximum Claim Table:

The Claim Vector is: 3 2

The Allocated Resource Table:

3 4

6 4

The Maximum Claim Table:

3 2

4 5

Allocated resources: 9 8

Available resources: -6 -6

The processes are in unsafe state.


13. Implement Bankers Algorithm Dead Lock Prevention:

#include <stdio.h>

int main()

// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;

n = 5; // Number of processes

m = 3; // Number of resources

int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix

{ 2, 0, 0 }, // P1

{ 3, 0, 2 }, // P2

{ 2, 1, 1 }, // P3

{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix


{ 3, 2, 2 }, // P1

{ 9, 0, 2 }, // P2

{ 2, 2, 2 }, // P3

{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;

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

f[k] = 0;

int need[n][m];

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

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

need[i][j] = max[i][j] - alloc[i][j];


}

int y = 0;

for (k = 0; k < 5; k++) {

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

if (f[i] == 0) {

int flag = 0;

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

if (need[i][j] > avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;
for (y = 0; y < m; y++)

avail[y] += alloc[i][y];

f[i] = 1;

int flag = 1;

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

if(f[i]==0)

flag=0;

printf("The following system is not safe");


break;

if(flag==1)

printf("Following is the SAFE Sequence\n");

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

printf(" P%d ->", ans[i]);

printf(" P%d", ans[n - 1]);

return (0);

}
Output:

Following is the SAFE Sequence

P1 -> P3 -> P4 -> P0 -> P2


14. Simulate all Page replacement algorithms:
a)FIFO
b)LRU
c)LFU

a)FIFO

#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]);

Printf(“Incoming \t Frame 1 \t Frame 2 \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++)

{ 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];

}
Printf(“\n”);

Printf(“%d\t\t\t”,incomingstream[m]);

For(n = 0; n < frames; n++)

if(temp[n] != -1)

Printf(“ %d\t\t\t”, temp*n+);

Else printf(“ - \t\t\t”);

Printf(“\nTotal Page Faults:\t%d\n”, pagefaults);

return 0;

}
Output:

Incoming Frame 1 Frame 2 Frame 3

4 4 - -

1 4 1 -

2 4 1 2

4 4 1 2

5 5 1 2

Total Page Faults: 4


b) LRU:

#include<stdio.h>

int findLRU(int time[], int n)

int i, minimum = time[0], pos = 0;

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

if (time[i] < minimum)

minimum = time[i];

pos = i;

return pos;
}

//main function

int main()

int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos,
faults = 0;

printf(“Enter number of frames: “);

scanf(“%d”, &no_of_frames);

printf(“Enter number of pages: “);

scanf(“%d”, &no_of_pages);

printf(“Enter reference string: “);

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


{

scanf(“%d”, &pages*i+);

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

frames[i] = -1;

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

flag1 = flag2 = 0;

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

{
if (frames[j] == pages[i])

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

if (flag1 == 0)

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

if (frames[j] == -1)

counter++;
faults++;

frames[j] = pages[i];

time[j] = counter;

flag2 = 1;

break;

if (flag2 == 0)

pos = findLRU(time, no_of_frames);

counter++;

faults++;

frames[pos] = pages[i];
time[pos] = counter;

printf(“\n”);

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

printf(“%d\t”, frames*j+);

printf(“\nTotal Page Faults = %d”, faults);

return 0;

}
Output:

Enter number of frames: 2

Enter number of pages: 3

Enter references string: ten

0 -1

0 -1

0 -1

Total Page faults =1


c)LFU

#include<stdio.h>

int main() {

int f,p;

int pages[50],frame[10],hit=0,count[50],time[50];

int i,j,page,flag,least,mintime,temp;

printf(“Enter no of frames : “);

scanf(“%d”,&f);

printf(“Enter no of pages : “);

scanf(“%d”,&p);

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

{ frame[i]=-1; }

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

{ count[i]=0; }

printf(“Enter page no : \n”);

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

, scanf(“%d”,&pages*i+); -

printf(“\n”);

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

{ count[pages[i]]++;

time[pages[i]]=i;

flag=1;

least=frame[0];

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

{ if(frame[j]==-1 || frame[j]==pages[i])

{ if(frame[j]!=-1) { hit++; }

flag=0;

frame[j]=pages[i];

break; }

if(count[least]>count[frame[j]]) { least=frame[j]; } }
if(flag) { mintime=50;

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

{ if(count[frame[j]]==count[least] && time[frame[j]]<mintime)

{ temp=j;

mintime=time[frame[j]]; } }

count[frame[temp]]=0;

frame[temp]=pages[i]; }

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

, printf(“%d “,frame*j+); -

printf(“\n”); -

printf(“Page hit = %d”,hit);

return 0;

}
Output:

Enter no of frames: 4

Enter no of pages: 3

Enter page no:

2 -1 -1 -1

2 1 -1 -1

2 1 0 -1

Page hit = 0
15. Simulate Paging Techniques of Memory Management:

#include<stdio.h>

#include<conio.h>

main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

clrscr();

printf(“\nEnter the memory size – “);

scanf(“%d”,&ms);

printf(“\nEnter the page size – “);

scanf(“%d”,&ps);

nop = ms/ps;

printf(“\nThe no. Of pages available in memory are -- %d “,nop);


printf(“\nEnter number of processes – “);

scanf(“%d”,&np);

rempages = nop;

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

printf(“\nEnter no. Of pages required for p[%d]—“,i);

scanf(“%d”,&s*i+);

if(s[i] >rempages)

printf(“\nMemory is Full”);

break;

rempages = rempages – s[i];

printf(“\nEnter pagetable for p[%d] --- “,i);

for(j=0;j<s[i];j++)
scanf(“%d”,&fno*i+*j+);

printf(“\nEnter Logical Address to find Physical Address “);

printf(“\nEnter process no. And pagenumber and offset – “);

scanf(“%d %d %d”,&x,&y, &offset);

if(x>np || y>=s[i] || offset>=ps)

printf(“\nInvalid Process or Page Number or offset”);

else

pa=fno[x][y]*ps+offset;

printf(“\nThe Physical Address is -- %d”,pa);

getch();

}
Output:

Enter the memory size - 4

Enter the page size – 2

The no. of pages available in memory is – 2

Enter number of processes – 2

Enter no. of pages required for p[1] – 2

Enter pagetable for p[1] - - - 2 1

Enter no, of pages required for p[2] – 2

Memory is Full

Enter process no. and pagenumber and offset – 2

Invalid Process or Page Number or offset

You might also like