0% found this document useful (0 votes)
2 views23 pages

C Programs for CPU Scheduling Algorithms

The document contains C programs that simulate various CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority scheduling. It also includes examples of UNIX/Linux system calls such as open, read, write, lseek, stat, opendir, and readdir. Additionally, there is a C program that implements the Banker's Algorithm for deadlock avoidance and prevention.

Uploaded by

yashaswinivuta
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)
2 views23 pages

C Programs for CPU Scheduling Algorithms

The document contains C programs that simulate various CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority scheduling. It also includes examples of UNIX/Linux system calls such as open, read, write, lseek, stat, opendir, and readdir. Additionally, there is a C program that implements the Banker's Algorithm for deadlock avoidance and prevention.

Uploaded by

yashaswinivuta
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

1.

Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF
c) Round Robin d) priority
a)FCFS
#include<stdio.h>
int main(){
int n,i;
int wt[20],bt[20],ct[20], tat[20],Twt=0,Ttat=0,at[20],rt[20],ft[20],x=0;
float Avgwt=0,Avgtat=0;
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the arrival time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&at[i]);
}
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++){
x+=bt[i];
if(at[i+1]<bt[i]){
ct[i]=x;
}
else{
ct[i]=at[i];
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=tat[i]-bt[i];
Twt+=wt[i];
}

for(i=0;i<n;i++){
tat[i]=ct[i]-at[i];
Ttat+=tat[i];
}
int sum=0;
for(i=0;i<n;i++){
if (at[i]<sum){
ft[i]=at[i];
}
else{
ft[i]=sum;
}
sum+=bt[i];
}

Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\tF.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i],ft[i]);
}
printf("__\t__\t%d\t%d\n",Twt,Ttat);
printf("__\t__\t%.2f\t%.2f\n",Avgwt,Avgtat);

}
Output:
Enter the number of processes
4
Enter the burst time for each process
45
3
34
54
pno B.T W.T T.A.T
1 45 0 45
2 3 45 48
3 34 48 82
4 54 82 136
-- -- 175 311
-- -- 43.75 77.75

b)SJF
#include <stdio.h>

int main() {
int n, i, j, temp;
int wt[20], bt[20], tat[20], pno[20];
int Twt = 0, Ttat = 0; // Total waiting time and turnaround time
float Avgwt = 0, Avgtat = 0;

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


scanf("%d", &n);

printf("Enter the burst time for each process:\n");


for (i = 0; i < n; i++) {
printf("%d ", i + 1);
scanf("%d", &bt[i]);
pno[i] = i + 1;
}

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


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

temp = bt[j];
bt[j] = bt[i];
bt[i] = temp;

temp = pno[j];
pno[j] = pno[i];
pno[i] = temp;
}#include<stdio.h>
int main(){
int n,i;
int wt[20],bt[20],ct[20], tat[20],Twt=0,Ttat=0,at[20],rt[20],ft[20],x=0;
float Avgwt=0,Avgtat=0;
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the arrival time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&at[i]);
}
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++){
x+=bt[i];
if(at[i+1]<bt[i]){
ct[i]=x;
}
else{
ct[i]=at[i];
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=tat[i]-bt[i];
Twt+=wt[i];
}

for(i=0;i<n;i++){
tat[i]=ct[i]-at[i];
Ttat+=tat[i];
}
int sum=0;
for(i=0;i<n;i++){
if (at[i]<sum){
ft[i]=at[i];
}
else{
ft[i]=sum;
}
sum+=bt[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\tF.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i],ft[i]);
}
printf("__\t__\t%d\t%d\n",Twt,Ttat);
printf("__\t__\t%.2f\t%.2f\n",Avgwt,Avgtat);

}
}

wt[0] = 0;
for (i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
Twt += wt[i];
}

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


tat[i] = wt[i] + bt[i];
Ttat += tat[i];
}

Avgwt = (float)Twt / n;
Avgtat = (float)Ttat / n;

printf("\nPNo\tB.T\tW.T\tT.A.T\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", pno[i], bt[i], wt[i], tat[i]);
}

printf("\__\t__\t%d\t%d\n", Twt, Ttat);


printf("__\t__\t%.2f\t%.2f\n", Avgwt, Avgtat);

return 0;
}
Output:
Enter the number of processes:3
Enter the burst time for each process:
10
12
40
Pno B.T W.T T.A.T
1 10 0 10
2 12 10 22
3 40 22 62
-- -- 32 94
-- -- 10.67 31.33

c)RoundRobin
#include<stdio.h>
int main()
{
int p[20], bt[20], ct[20], tat[20], wt[20];
int n, ts, i, temp = 0, finished;
int Twt = 0, Ttat = 0;
float Awt, Atat;
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter the process IDs: ");
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 time slice: ");
scanf("%d",&ts);

do
{
finished = 1;
for(i=0;i<n;i++)
{
if(bt[i] > 0)
{
finished = 0;
if(bt[i] <= ts)
{
temp =temp+ bt[i];
tat[i] = temp;
bt[i] = 0;
}
else
{
bt[i] =bt[i]- ts;
temp = temp+ts;
}
}
}
} while(finished == 0);

for(i=0;i<n;i++)
{
wt[i] = tat[i]-ct[i];
Twt = Twt+wt[i];
Ttat = Ttat+tat[i];
}

Awt = (float)Twt / n;
Atat = (float)Ttat / n;

printf("\nProcess\tBT\tTAT\tWT");

for(i=0;i<n;i++)
{
printf("\nP%d\t%d\t%d\t%d", p[i], ct[i], tat[i], wt[i]);
}

printf("\nAverage TAT = %.2f", Atat);


printf("\nAverage WT = %.2f", Awt);

return 0;
}
output:
Enter the number of processes: 4
Enter the process IDs: 1 2 3 4
Enter the burst times: 5 15 4 3
Enter the time slice: 5
ProcessBTTATWT
P1550
P2153217
P3495
P43129

Average TAT = 14.50


Average WT = 7.75

d)priority
#include<stdio.h>
void main()
{
int wt[20], bt[20], tat[20],prty[20],i;
int Twt=0,Ttat=0,max,j,n,m,p[20],x;
float Avgwt,Avgtat;
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];
Twt+=wt[i];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
Ttat+=tat[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("Process\tBT\tPriority\tTAT\tWT");

for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d\t\t%d \t\t%d",p[i],bt[i],prty[i],tat[i],wt[i]);
}
printf("\n\t\t(ATAT)%f\t%f(AWT)\n",Avgtat,Avgwt);

}
Output:
Enter the no of processes: 4
Enter the burst times: 6 8 7 3
Enter the priorities: 2 1 3 4
Enter the processes: 1 2 3 4
ProcessBTPriorityTATWT
P2 818 0
P1 6214 8
P3 7321 14
P4 3424 21
(ATAT)16.75000010.750000(AWT)

2)Write programs using the I/O system calls of UNIX/LINUX operating system (open, read,
write, close,fcntl, seek, stat, opendir, readdir)
a)open,read,write
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
void main()

{
int n, fd, fd1;
char buf[20];
fd = open("[Link]", O_RDONLY);
n = read(fd, buf, 20);
fd1 = open("[Link]", O_CREAT | O_WRONLY, 0666);
write(fd1, buf, n);
}

output:
File copied successfully.
b)Lseek()
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
void main()
{
int fd;
char buf[50];
fd=open("[Link]",O_RDWR);
if(fd<0)
{
perror("Error with file");
exit(1);
}
read(fd,buf,10);
write(1,buf,10);
printf("\n");
lseek(fd,5,SEEK_CUR);
read(fd,buf,10);
write(1,buf,10);
printf("\n");
lseek(fd,5,SEEK_SET);
read(fd,buf,10);
write(1,buf,10);
printf("\n");
lseek(fd,-10,SEEK_END);
read(fd,buf,10);
write(1,buf,10);
printf("\n");
}

[Link]:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

output:
ABCDEFGHIJ
PQRSTUVWXY
FGHIJKLMNO
QRSTUVWXYZ

c)lstat()
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
int main( int argc,char *argv[]){
int fd;
struct stat buf;
fd=stat(argv[1],&buf);
if(fd<0)
{
perror("Error in opening file");
exit(1);
}
printf("user id:%d\n",buf.st_uid);
printf("groupo id:%d\n",buf.st_gid);
printf("number of links:%ld\n",buf.st_nlink);
printf("size:%ld\n",buf.st_size);
printf("permission:%o\n",buf.st_mode);
printf("inode:%lu\n",buf.st_ino);
printf("Time of last change:%s\n",ctime(&buf.st_ctime));
printf("Time of last access:%s\n",ctime(&buf.st_atime));
printf("Time lat modification:%s\n",ctime(&buf.st_mtime));
return 0;
}

Output:
user id:1000
groupo id:1000
number of links:1
size:26
permission:100644
inode:3456721
Time of last change:Sun Nov 10 14:23:40 2025
Time of last access:Sun Nov 10 14:24:02 2025
Time lat modification:Sun Nov 10 14:23:40 2025

d)Opendir,Closedir
#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
#include<sys/stat.h>
#include<unistd.h>
void main(){
char d[10];
int c,op;
DIR *e;
struct dirent *sd;
printf("***MENU***\[Link] dir\[Link] dir\[Link] dir\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("enter dir name\n");
scanf("%s",d);
c=mkdir(d,777);
if(c==1)
printf("dir is not created");
else
printf("dir is created");
break;
case 2:
printf("Enter dir name\n");
scanf("%s",d);
c=rmdir(d);
if(c==1)
printf("dir is not removed");
else
printf("dir is removed");
break;
case 3:
printf("dir name to open:");
scanf("%s",d);
e=opendir(d);
if(e==NULL)
printf("dir does not exist ");
else
{
printf("dir exist\n");
while((sd=readdir(e))!=NULL)
printf("%s",sd->d_name);
}
closedir(e);
break;
}
}

Output:
***MENU***
[Link] dir
[Link] dir
[Link] dir
Enter your choice:1
enter dir name
testdir
dir is not created
Enter your choice:2
Enter dir name
testdir
dir is not removed
Enter your choice:3
dir name to open: .
dir exist
. .. [Link] [Link] testdir

[Link] a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention.
#include <stdio.h>

int main() {
int totalres[3], available[3], work[3];
int max[5][3], allocation[5][3], need[5][3], safe[5];
char finish[5];
int i, j, k, totalloc, value = 0;

printf("Enter instances of each Resource (3 resources):\n");


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

printf("Enter maximum resources for each process (5 processes, 3 resources):\n");


for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("Max resource %d for process %d: ", j + 1, i);
scanf("%d", &max[i][j]);
}
}

printf("Enter allocated resources for each process:\n");


for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("Allocated resource %d to process %d: ", j + 1, i);
scanf("%d", &allocation[i][j]);
}
}

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


finish[i] = 'f';
}

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


totalloc = 0;
for (j = 0; j < 5; j++) {
totalloc += allocation[j][i];
}
available[i] = totalres[i] - totalloc;
work[i] = available[i];
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
printf("\nAllocated Resources:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}
printf("\nMaximum Resources:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
printf("\nNeeded Resources:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
printf("\nAvailable Resources:\n");
for (i = 0; i < 3; i++) {
printf("%d ", available[i]);
}
printf("\n");
int count = 0;
while (count < 5) {
int found = 0;
for (i = 0; i < 5; i++) {
if (finish[i] == 'f') {
int canAllocate = 1;
for (j = 0; j < 3; j++) {
if (need[i][j] > work[j]) {
canAllocate = 0;
break;
}
}

if (canAllocate) {
for (j = 0; j < 3; j++) {
work[j] += allocation[i][j];
}
safe[value++] = i;
finish[i] = 't';
found = 1;
count++;
}
}
}

if (!found) {
printf("\nSystem is not in a safe state.\n");
return 1;
}
}
printf("\nSafe Sequence is: ");
for (i = 0; i < 5; i++) {
printf("P%d ", safe[i]);
}
printf("\n");

return 0;
}

Output:
Enter instances of each Resource (3 resources):
10 5 7
Enter maximum resources for each process (5 processes, 3 resources):
Max resource 1 for process 0: 7
Max resource 2 for process 0: 5
Max resource 3 for process 0: 3
Max resource 1 for process 1: 3
Max resource 2 for process 1: 2
Max resource 3 for process 1: 2
Max resource 1 for process 2: 9
Max resource 2 for process 2: 0
Max resource 3 for process 2: 2
Max resource 1 for process 3: 2
Max resource 2 for process 3: 2
Max resource 3 for process 3: 2
Max resource 1 for process 4: 4
Max resource 2 for process 4: 3
Max resource 3 for process 4: 3
Enter allocated resources for each process:
Allocated resource 1 to process 0: 0
Allocated resource 2 to process 0: 1
Allocated resource 3 to process 0: 0
Allocated resource 1 to process 1: 2
Allocated resource 2 to process 1: 0
Allocated resource 3 to process 1: 0
Allocated resource 1 to process 2: 3
Allocated resource 2 to process 2: 0
Allocated resource 3 to process 2: 2
Allocated resource 1 to process 3: 2
Allocated resource 2 to process 3: 1
Allocated resource 3 to process 3: 1
Allocated resource 1 to process 4: 0
Allocated resource 2 to process 4: 0
Allocated resource 3 to process 4: 2

Allocated Resources:
010
200
302
211
002

Maximum Resources:
753
322
902
222
433

Needed Resources:
743
122
600
011
431
Available Resources:
332

Safe Sequence is: P1 P3 P4 P0 P2

[Link] a C program to implement the Producer – Consumer problem using semaphores


using UNIX/LINUX system calls
#include <stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice;
in = 0;
out = 0;
bufsize = 10;
while (choice != 3)
{
printf("\n1. Produce\t2. Consume\t3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
if ((in + 1) % bufsize == out)
printf("\nBuffer is full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in + 1) % bufsize;
}
break;
case 2:
if (in == out)
printf("\nBuffer is empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out + 1) % bufsize;
}

break;
case 3:
printf("\nExiting...");
break;
default:
printf("\nInvalid choice!");
break;
}
}
}
ouput:
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 10
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 20
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 10
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 20
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is empty
1. Produce 2. Consume 3. Exit
Enter your choice: 3
Exiting...

5. Write C programs to illustrate the following IPC mechanisms a) Pipes b) FIFOs c)


Message Queues d)Shared Memory
a)pipe
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
int pid, a[2], b[2];
char str[30], buff1[30], buff2[30];
pipe(a);
pipe(b);
pid = fork();

if (pid == 0)
{
strcpy(str, "Welcome to OS lab");
write(a[1], str, strlen(str) + 1);
sleep(2);
read(b[0], buff1, sizeof(buff1));
printf("response from parent is %s\n", buff1);
}
else
{
read(a[0], buff2, sizeof(buff2));
printf("request from child is %s\n", buff2);
strcpy(buff2, "parent says hello");
write(b[1], buff2, sizeof(buff2));
exit(1);
}
}

Output;
request from child is Welcome to OS lab
response from parent is parent says hello

b)fifo
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(){
int res;
res=mkfifo("fifo1",0777);
printf("named pipe created successfully\n");
}

Sender.c
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(){
int res;
res=mkfifo("fifo1",0777);
printf("named pipe created successfully\n");
}
Receiver.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
int res;
char buffer[100];

res = open("fifo1", O_RDONLY);


n=read(res,buffer,100);
printf("reader process having pid %d started\n",getpid());
printf("Receiver process having PID %d received data: %s\n", getpid(), buffer);

close(res);
return 0;
}

Output:
Sender process having pid 4506 sent the data
reader process having pid 4507 started
data received by receiver 4507 is message

c)Message queues
#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<unistd.h>

int main()
{
int pid, msqid;
char buff[20], buff1[20], a[20];
msqid = msgget((key_t)151, IPC_CREAT | 0600);
pid = fork();

if(pid == 0)
{
strcpy(a, "160422733151");
msgsnd(msqid, a, 12, 0);
sleep(2);
msgrcv(msqid, buff1, 18, 0, 0);
printf(" echoed message is %s\n", buff1);
}
else
{
sleep(1);
msgrcv(msqid, buff, 12, 0, 0);
printf("message received is %s\n", buff);
strcat(buff, "__pass");
msgsnd(msqid, buff, 18, 0);
}
}

output:
message received is 160422733151
echoed message is 160422733151__pass

d)sharedmemory
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/shm.h>

int main() {
int pid, shmid;
char *p;
shmid = shmget((key_t)224, 20, IPC_CREAT | 0600);
p = (char *)shmat(shmid, 0, 0);
pid = fork();

if (pid == 0) {
strcpy(p, "data in shared memory\n");
}
else if (pid > 0) {
sleep(1);
printf("%s\n", p);
}
else {
perror("error");
}
}

Output:
data in shared memory

6. Write C programs to simulate the following memory management techniques a) Paging


#include <stdio.h>
int main() {
int memsize = 15;
int pagesize, nofpage;
int p[100];

int frameno, offset;


int logadd, phyadd;
int i;
int choice = 0;
printf("\nYour memory size is %d", memsize);
printf("\nEnter page size: ");
scanf("%d", &pagesize);
nofpage = memsize / pagesize;
for(i = 0; i < nofpage; i++) {
printf("\nEnter the frame number for page %d: ", i);
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);
return 0;
}

output:
Your memory size is 15
Enter page size: 3
Enter the frame number for page 0: 5
Enter the frame number for page 1: 6
Enter the frame number for page 2: 4
Enter the frame number for page 3: 9
Enter the frame number for page 4: 7
Enter a logical address: 7
Physical address is 13
Do you want to continue (1/0)?: 0
[Link] C programs to simulate Page replacement policies a) Fifo b)LRU
a)Fifo
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,n,rs[50],f[10],nf,k=0,avail,pf=0;
float hr,mr;
system("clear");
printf("Enter number of pages\n");
scanf("%d",&n);
printf("Enter the reference string\n");
for(i=1;i<=n;i++){
scanf("%d",&rs[i]);
}
printf("Enter frame size\n");
scanf("%d",&nf);
for(i=0;i<nf;i++)
f[i]=-1;
printf("page frames\n");
for(i=1;i<=n;i++){
avail=0;
for(j=0;j<nf;j++){
if(f[j]==rs[i])
avail=1;
}
if(avail==0){
f[k]=rs[i];
k=(k+1)%nf;
pf++;
for(j=0;j<nf;j++)
printf("%d\t",f[j]);
printf("pf no is %d",pf);
}
printf("\n");
}
printf("Total pf is %d\n",pf);

mr=(float)pf/n;
hr=(float)(n-pf)/n;
printf("hr,mr %.2f %.2f\n",hr,mr);
}
output:
Enter number of pages
10
Enter the reference string
1234125123
Enter frame size
3
page frames
1 -1 -1 pf no is 1
1 2 -1 pf no is 2
1 2 3 pf no is 3
4 2 3 pf no is 4
4 1 3 pf no is 5
4 1 2 pf no is 6
5 1 2 pf no is 7
512
512
5 3 2 pf no is 8
Total pf is 8
hr,mr 0.20 0.80

b) LRU
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,n,rs[50],f[10],nf,k,min,count[10],flag[20],pf=0,next=1;
float hr,mr;
system("clear");
printf("Enter number of pages\n");
scanf("%d",&n);
printf("Enter the reference string\n");
for(i=0;i<n;i++){
scanf("%d",&rs[i]);
flag[i]=0;

}
printf("Enter frame size\n");
scanf("%d",&nf);
for(i=0;i<nf;i++){
count[i]=0;
f[i]=-1;
}
printf("page frame\n");
for(i=0;i<n;i++){
for(j=0;j<nf;j++){
if(f[j]==rs[i]){
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0){
if(i<nf){
f[i]=rs[i];
count[i]=next;
next++;
}
else{
}
pf++;
}
min=0;
for(j=1;j<nf;j++)
if(count[min]>count[j])
min=j;
f[min]=rs[i];
count[min]=next;
next++;
for(j=0;j<nf;j++)
printf("%d\t",f[j]);
if(flag[i]==0)
printf("pf no is %d",pf);

printf("\n");
}
printf("Total pf is %d\n",pf);
mr=(float)pf/n;
hr=(float)(n-pf)/n;
printf("hr,mr %.2f %.2f\n",hr,mr);
}
output:
Enter number of pages
12
Enter the reference string
123412512345
Enter frame size
3
page frame
1 -1 -1 pf no is 1
1 2 -1 pf no is 2
1 2 3 pf no is 3
4 2 3 pf no is 4
4 1 3 pf no is 5
4 1 2 pf no is 6
5 1 2 pf no is 7
512
512
5 3 2 pf no is 8
4 3 2 pf no is 9
4 3 5 pf no is 10
Total pf is 10
hr,mr 0.17 0.83

You might also like