0% found this document useful (0 votes)
128 views26 pages

OS Lab File

The document describes implementations of different page replacement algorithms in C including FIFO, LRU, and optimal page replacement. It includes code snippets showing how to track page frames and determine when a page fault occurs. Sample outputs are also provided showing the page frames and number of page faults for sample reference strings using each algorithm.

Uploaded by

Sudeep Shukla
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)
128 views26 pages

OS Lab File

The document describes implementations of different page replacement algorithms in C including FIFO, LRU, and optimal page replacement. It includes code snippets showing how to track page frames and determine when a page fault occurs. Sample outputs are also provided showing the page frames and number of page faults for sample reference strings using each algorithm.

Uploaded by

Sudeep Shukla
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/ 26

Lab 1:- Implementation Of First Come First Serve Algorithm

Using C.

#include<stdio.h>

int main()

int at[10],at2[10],bt[100],ex[100],seq[100],re[100],wt[100],tat[100];

int n,i,j,start,pos,max=0,min,idle=0,k=0;

float av1=0,av2=0;

printf("*****INPUT*****\n");

printf("Enter number of process\n");

scanf("%d",&n);

printf("Enter arrival time for processess\n");

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

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

at2[i]=at[i];

printf("Enter burst time for processess\n");

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

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

start=at[0];

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

if(start>at[i])

start=at[i];
}

printf("*****OUTPUT*****\n");

printf("Sequence of execution is\n");

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

if(max<at[i])

max=at[i];

max=max+1;

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

{ min=max;

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

if(at[j]!=-1)

if(at[j]<min)

min=at[j];

pos=j;

printf("[P%d] ",pos);

seq[k]=pos;

if(start<at[pos]){

re[pos]=start;

idle+=at[pos]-start;

start=at[pos];

start+=bt[pos];
at[pos]=-1;

ex[pos]=start;

else{

re[pos]=start;

start+=bt[pos];

at[pos]=-1;

ex[pos]=start;

printf("\n");

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

tat[i]=ex[i]-at2[i];

wt[i]=tat[i]-bt[i];

printf("Process Arrival-time(s) Burst-time(s) Waiting-time(s) Turnaround-time(s)\n");

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

printf("P%d %d %d %d %d\n",i,at2[i],bt[i],wt[i],tat[i]);

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

av1+=tat[i];

av2+=wt[i];

printf("Average waiting time(s) %f\nAverage turnaroundtime(s) %f\nCPU idle


time(s)%d\n",av2/n,av1/n,idle);

Output:-
*****INPUT*****

Enter number of process

Enter arrival time for processess

35054

Enter burst time for processess

43213

*****OUTPUT*****

Sequence of execution is

[P2] [P0] [P4] [P1] [P3]

Process Arrival-time(s) Burst-time(s) Waiting-time(s) Turnaround-time(s)

P0 3 4 0 4

P1 5 3 5 8

P2 0 2 0 2

P3 5 1 8 9

P4 4 3 3 6

Average waiting time(s) 3.200000

Average turn around time(s) 5.800000

CPU idle time(s)1

Process returned 0 (0x0) execution time : 35.057 s

Press any key to continue.


Lab 2:- Implementation of Round Robin algorithm using C

#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 ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");

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]\t\t%d\t\t %d\t\t\t %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("\n\nAverage Waiting Time:\t%f", average_wait_time);


printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);

return 0;

Output:-
Lab 3a:- FIFO Page Replacement Algorithm

#include<stdio.h>

int main()

int i,j,n,a[50],frame[10],no,k,avail,count=0;

printf("\nenter the length of the Reference string:\n");

scanf("%d",&n);

printf("\n enter the reference string:\n");

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

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

printf("\n enter the number of Frames:");

scanf("%d",&no);

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

frame[i]= -1;

j=0;

printf("\tref string\t page frames\n");

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

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

avail=0;

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

if(frame[k]==a[i])

avail=1;

if (avail==0)

frame[j]=a[i];

j=(j+1)%no;

count++;

for(k=0;k<no;k++)
printf("%d\t",frame[k]);

printf("\n\n");

printf("Page Fault Is %d",count);

return 0;

Output:-

enter the length of the Reference string:

20

enter the reference string:

enter the number of Frames:3


ref string page frames

7 7 -1 -1

0 7 0 -1

1 7 0 1

2 2 0 1

3 2 3 1

4 2 3 4

0 0 3 4

2 0 2 4

1 0 2 1

7 7 2 1

0 7 0 1
1

Page Fault Is 11

--------------------------------

Process exited after 66.89 seconds with return value 0

Press any key to continue . . .


Lab 3b:- Implementation of LRU Page Replacement
Algorithm using C
#include<stdio.h>

#include<conio.h>

#define high 37

void main()

int fframe[10],used[10],index;

int count,n1,k,nf,np=0,page[high],tmp;

int flag=0,pf=0;

clrscr();

printf("Enter no. of frames:");

scanf("%d",&nf);

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

fframe[count]=-1;

printf(" lru page replacement algorithm in c ");

printf("Enter pages (press -999 to exit):\n");

for(count=0;count<high;count++)

scanf("%d",&tmp);

if(tmp==-999) break;

page[count]=tmp;

np++;

for(count=0;count<np;count++)

flag=0;

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

if(fframe[n1]==page[count])

{
printf("\n\t");

flag=1;break;

/* program for lru page replacement algorithm in c */

if(flag==0)

for(n1=0;n1<nf;n1++) used[n1]=0;

for(n1=0,tmp=count-1;n1<nf-1;n1++,tmp--)

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

if(fframe[k]==page[tmp])

used[k]=1;

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

if(used[n1]==0)

index=n1;

fframe[index]=page[count];

printf("\nFault: ");

pf++;

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

printf("%d\t",fframe[k]);

} // lru algorithm in c

printf("\nnumber of total page faults is: %d ",pf);

getch();

Output:-
lru page replacement algorithm in c
Enter no. of frames Enter pages :

2321

5245

3 2 5 -999

-1 -1 2 Fault:

-1 -1 2 Fault:

-1 3 2 Fault:

1 3 2 Fault:

132

1 5 2 Fault:

4 5 2 Fault:

452

4 5 3 Fault:

2 5 3 Fault:

253

253
Lab 3c:- Implementation of Optimal Page Replacement
algorithm
#include<stdio.h>

#include<conio.h>

main()

int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];

clrscr();

printf("enter the number of frames:");

scanf("%d",&nf);

printf("\n enter the page size");

scanf("%d",&psz);

printf("\nenter the page sequence:");

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

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

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

fr[i]=-1;

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

if(full(fr,nf)==1)

break;

else

flag=0;

for(j=1; j<=nf; j++)

if(page[i]==fr[j])

flag=1;
printf(" \t%d:\t",page[i]);

break;

if(flag==0)

fr[p]=page[i];

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

p++;

for(j=1; j<=nf; j++)

printf(" %d ",fr[j]);

printf("\n");

p=0;

for(; i<=psz; i++)

flag=0;

for(j=1; j<=nf; j++)

if(page[i]==fr[j])

flag=1;

break;

if(flag==0)

p++;
for(j=1; j<=nf; j++)

for(k=i+1; k<=psz; k++)

if(fr[j]==page[k])

u[j]=k;

break;

else

u[j]=21;

for(j=1; j<=nf; j++)

t[j]=u[j];

for(j=1; j<=nf; j++)

for(k=j+1; k<=nf; k++)

if(t[j]<t[k])

t1=t[j];

t[j]=t[k];

t[k]=t1;

for(j=1; j<=nf; j++)

if(t[1]==u[j])

{
fr[j]=page[i];

u[j]=i;

printf("page fault\t");

else

printf(" \t");

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

for(j=1; j<=nf; j++)

printf(" %d ",fr[j]);

printf("\n");

printf("\ntotal page faults: %d",p+3);

// getch();

int full(int a[],int n)

int k;

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

if(a[k]==-1)

return 0;

return 1;

}
Output:-
enter the number of frames:5

enter the page size2

enter the page sequence:1

1: 1 -1 -1 -1 -1

2: 1 2 -1 -1 -1

total page faults: 3


Lab 4:- Implementation of BANKER’S Algorithm using C

#include <stdio.h>

int curr[5][5], maxclaim[5][5], avl[5];

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

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

int count = 0, i, j, exec, r, p, k = 1;

int main()

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

scanf("%d", &p);

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

running[i] = 1;

count++;

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

scanf("%d", &r);

printf("\nEnter Claim Vector:");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

printf("\n");

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

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

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

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

printf("\n");
}

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

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

alloc[j] += curr[i][j];

printf("\nAllocated resources:");

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

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

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

avl[i] = maxres[i] - alloc[i];

printf("\nAvailable resources:");

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

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

printf("\n");

//Main procedure goes below to check for unsafe state.

while (count != 0) {

safe = 0;

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

if (running[i]) {

exec = 1;

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

if (maxclaim[i][j] - curr[i][j] > avl[j]) {


exec = 0;

break;

if (exec) {

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

running[i] = 0;

count--;

safe = 1;

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

avl[j] += curr[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 < r; i++) {

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

printf("\n");

}
}

Output:-

Enter the number of processes:5

Enter the number of resources:4

Enter Claim Vector:8 5 9 7

Enter Allocated Resource Table:

2011

0121

4003

0210

1030

Enter Maximum Claim Table:

3214

0252

5105

1530

3033

The Claim Vector is: 8 5 9 7

The Allocated Resource Table:

2 0 1 1

0 1 2 1

4 0 0 3

0 2 1 0

1 0 3 0

The Maximum Claim Table:

3 2 1 4

0 2 5 2

5 1 0 5

1 5 3 0
3 0 3 3

Allocated resources: 7 3 7 5

Available resources: 1 2 2 2

Process3 is executing

The process is in safe state

Available vector: 5 2 2 5

Process1 is executing

The process is in safe state

Available vector: 7 2 3 6

Process2 is executing

The process is in safe state

Available vector: 7 3 5 7

Process4 is executing

The process is in safe state

Available vector: 7 5 6 7

Process5 is executing

The process is in safe state

Available vector: 8 5 9 7

You might also like