Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OS Programs

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

Assignment 1 :

Set A
1)
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void child();
void parent();
main()
{
pid_t pid;
pid=fork();
if(pid==0)
child();
else
Parent();

}
void child()
{
int c_pid;
printf("\nIam child process");
c_pid=getpid();
printf("\nChild process id is %d",c_pid);
}
void Parent()
{
int p_pid;
printf("\nI am parent in process");
p_pid=getppid();
printf("\nParent Process id is %d",p_pid);
}

2)
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,retnice;
printf("Press DEL to stop process\n");
pid=fork();
for(;;)
{
if(pid==0)
{
retnice=nice(-5);
printf("Child get higher CPU priority %d\n,retnice");
sleep(1);
}break;
else
{
retnice=nice(4);
printf("Parent gets lower CPU priority %d\n,retnice");
sleep(1);
break;
}
}

Set B
1)
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

void bubblesort(int [],int);//bubble sort call


void insertionsort(int [],int);

int main()
{
int i,j,n;
int *status=NULL;
int a[30];

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


scanf("%d",&n);

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
pid_t pid;
pid=fork();

if(pid==0)
{
printf("\n\t This is child process. ");
printf("\n\t My process id is : %d", getpid());
printf("\n\t My Parent process id is : %d", getppid());
insertionsort(a,n);
printf("\nInsertionSort");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\n\n");
}
else
{

printf("\n\n\t Parent process resumed after the execution of child process with PID
%d", pid);
printf("\n\t My process id is : %d", getpid());
printf("\n\t My Parent process id is : %d", getppid());
bubblesort(a,n);
printf("\nBubbleSort:");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\n\n");
pid=wait(status);

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubblesort(int a[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place


for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1])
swap(&a[j], &a[j+1]);
}

void insertionsort(int a[],int n)


{
int i,j,key;
for(i=1;i<n;i++)
{
key=a[i];
for(j=i-1;j>=0;j--)
{
if(key<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=key;
}

2)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork() Create a child process

int pid = fork();


if (pid > 0) {
//getpid() returns process id
// while getppid() will return parent process id
printf("Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n", getpid());
// getppid() will return parent process id of child process
printf("Parent -ID: %d\n\n", getppid());

sleep(10);

// At this time parent process has finished.


// So if u will check parent process id
// it will show different process id
printf("\nChild process \n");
printf("ID: %d\n", getpid());
printf("Parent -ID: %d\n", getppid());
}
else {
printf("Failed to create child process");
}

return 0;
}

Assignment 2 :
Set A:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>

void count(char c, char *fn)


{
int lc=0,wc=0,cc=0,handle;
char ch;

handle = open(fn,O_RDONLY);

if(handle == -1)
{
printf("file %s not found.\n",fn);
return;
}

while(read(handle,&ch,1) !=0)
{
if(ch == '\n')
{
lc++;
cc++;
wc++;
}
else if(ch == ' ')
{
wc++;
cc++;
}
else
{
cc++;
}
}
close(handle);

switch(c)
{
case 'c':
printf("Total number of Character = %d\n\n",cc);
break;

case 'w':
printf("Total number of words = %d\n\n",wc);
break;
case 'l':
printf("Total number of lines = %d\n\n",lc );
break;
}
}

void main()
{
char command[80],t1[20],t2[20],t3[20],t4[20];
int n;
system("clear");
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(command,80,stdin);
n = sscanf(command,"%s %s %s %s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t1,t2,NULL);
perror(t1);
}
break;

case 3:
if(strcmp(t1,"count") == 0)
count(t2[0],t3);
else if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;

case 4:
if(!fork())
{
execlp(t1,t1,t2,t3,t4,NULL);
perror(t1);
}
}
}
}

SET B:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void make_toks(char *s, char *tok[])


{
int i=0;
char *p;

p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}

tok[i]=NULL;
}

void list(char *dn, char op)


{
DIR *dp;
struct dirent *entry;
int dc=0,fc=0;

dp = opendir(dn);
if(dp==NULL)
{
printf("Dir %s not found.\n",dn);
return;
}

switch(op)
{
case 'f':
while(entry=readdir(dp))
{
if(entry->d_type==DT_REG)
printf("%s\n",entry->d_name);
}
break;
case 'n':
while(entry=readdir(dp))
{
if(entry->d_type==DT_DIR) dc++;
if(entry->d_type==DT_REG) fc++;
}
printf("%d Dir(s)\t%d File(s)\n",dc,fc);
break;
case 'i':
while(entry=readdir(dp))
{
if(entry->d_type==DT_REG)
printf("%s\t%d\n",entry->d_name,entry->d_fileno);
}
}

closedir(dp);
}

int main()
{
char buff[80],*args[10];
int pid;
system("clear");
while(1)
{
printf("myshell$: ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"list")==0)
list(args[2],args[1][0]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}
SET C:
Q1:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void make_toks(char *s, char *tok[])


{
int i=0;
char *p;

p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}

tok[i]=NULL;
}

void typeline(char *fn, char *op)


{
int fh,i,j,n;
char c;

fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s not found.\n",fn);
return;
}

if(strcmp(op,"a")==0)
{
while(read(fh,&c,1)>0)
printf("%c",c);
close(fh);
return;
}

n = atoi(op);
if(n>0)
{
i=0;
while(read(fh,&c,1)>0)
{
printf("%c",c);
if(c=='\n') i++;
if(i==n) break;
}
}

if(n<0)
{
i=0;
while(read(fh,&c,1)>0)
{
if(c=='\n') i++;
}
lseek(fh,0,SEEK_SET);
j=0;
while(read(fh,&c,1)>0)
{
if(c=='\n') j++;
if(j==i+n) break;
}
while(read(fh,&c,1)>0)
{
printf("%c",c);
}
}
close(fh);
}

int main()
{
char buff[80],*args[10];
int pid;

while(1)
{
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"typeline")==0)
typeline(args[2],args[1]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}

Q2:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void make_toks(char *s, char *tok[])


{
int i=0;
char *p;

p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}

tok[i]=NULL;
}

void search(char *fn, char op, char *pattern)


{
int fh,count=0,i=0,j=0;
char buff[255],c,*p;

fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s Not Found\n",fn);
return;
}

switch(op)
{
case 'f':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
i++;
if(strstr(buff,pattern))
{
printf("%d: %s",i,buff);
break;
}
}
}
break;
case 'c':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
p = buff;
while(p=strstr(p,pattern))
{
count++;
p++;
}
}
}
printf("Total No.of Occurrences = %d\n",count);
break;
case 'a':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j = 0;
i++;
if(strstr(buff,pattern))
printf("%d: %s",i,buff);
}
}
}//switch
close(fh);
}//search

int main()
{
char buff[80],*args[10];
int pid;

while(1)
{
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"search")==0)
search(args[3],args[1][0],args[2]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}

Assignment 3
SET A:
1)
#include<stdio.h>
float getMax();
int getfirst();
void showRQ();
void setStartRQ();
void setAllT();
void ganttChart();
typedef struct
{
float at;
float bt;
float done;
float st;
float wt;
float ft;
float tat;
}JobProcess;

JobProcess p[100];
int n,rediQ[100];
int getfirst()
{
int i=0,j;
float least=getMax();
for(i=0;i<n;i++)
if(p[i].at<=least&&!p[i].done)
least=p[i].at;
for(i=0;i<n;i++)
if(least==p[i].at&&!p[i].done)
{
p[i].done=1;
return i;
}
}
float getMax()
{
int i;
float max=0;
for(i=0;i<n;i++)
if(p[i].at>max)
max=p[i].at;
return max;
}
void FCFS()
{
int i;
for(i=0;i<n;i++)
rediQ[i]=getfirst();
setStartT();
setAllT();
ganttChart();
showRQ();
}
void ganttChart()
{
int i;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
printf("-----------------------------------------------");
printf("\n|");
for(i=0;i<n;i++)
printf("P%d\t",rediQ[i]+1);
printf("\n|");
for(i=0;i<n;i++)
printf("------------------------------------------------");
printf("\n|");
for(i=0;i<n;i++)
printf("%.1f\t",p[rediQ[i]].st);
printf("%.1f",p[rediQ[n-1]].ft);
}
void setAllT()
{
int i;
for(i=0;i<n;i++)
{
p[i].ft=p[i].st+p[i].bt;
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].ft-p[i].at;
}
}
void setStartT()
{
int i;
p[rediQ[0]].st=p[rediQ[0]].at;
for(i=1;i<n;i++)
{
p[rediQ[i]].st=p[rediQ[i-1]].bt+p[rediQ[i-1]].st;
if(p[rediQ[i]].st<p[rediQ[i]].at)
p[rediQ[i]].st=p[rediQ[i]].at;
}
}
void showRQ()
{
int i;
printf("\n\n\t\t\t(time)\nProcess\tBurst\tArrival\tStart\tWait\tFinish\
tTurn_Arround\n");
for(i=0;i<n;i++)
printf("P%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\
n",i+1,p[i].bt,p[i].at,p[i].st,p[i].wt,p[i].ft,p[i].tat);
float sum;
for(i=0;i<n;i++)
sum+=p[i].tat;
printf("\nAverage TurnAroundTime:\t%.2f",sum/n);
sum=0.0;
for(i=0;i<n;i++)
sum+=p[i].wt;
printf("\nAverageWaiting Time:\t\t%.2f\n",sum/n);
}
void main()
{
int i,rq[n];
printf("Enter the number of process ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the BurstTime of Process%d ",i+1);
scanf("%f",&p[i].bt);
printf("Enter the ArrivalTime of process%d ",i+1);
scanf("%f",&p[i].at);
p[i].done=0;
printf("\n");
}
FCFS();
}
2)
#include<stdio.h>

struct time
{
int p,art,but,wtt,tat,st;
};
int process(struct time a[], int pro,int t)
{
int i,minpro, mintime=999;
for(i=0;i<pro;i++)
{
if(a[i].art <= t && a[i].st == 0)
{
if(mintime > a[i].but)
{
mintime = a[i].but;
minpro = i;
}
}
}
a[minpro].st = 1;
return minpro;
}

void ganttchart(struct time a[],int gc[],int pro)


{
int i,x=0;
printf("Gantt Chart\n");
printf("0");
for(i=0;i<pro;i++)
{
x = x + a[gc[i]].but;
printf(" | [P%d] | %d",a[gc[i]].p,x);
}
printf("\n");
return;
}

int main()
{
int i,pro,curpro,t=0,gc[100];
struct time a[100];
float avgwt=0,avgtt=0;
printf("SJF (Shortest Job First) - Non Preemptive\n");
printf("Note -\n1. Arrival Time of at least on process should be 0\n2. CPU
should never be idle\n");
printf("Enter Number of Processes\n");
scanf("%d",&pro);
for(i=0;i<pro;i++)
{
printf("Enter Arrival Time & Burst Time for Process P%d\n",i);
a[i].p = i;
scanf("%d%d",&a[i].art,&a[i].but);
a[i].st = 0;
}

for(i=0;i<pro;i++)
{
curpro = process(a,pro,t);
a[curpro].wtt = t - a[curpro].art;
a[curpro].tat = a[curpro].art + a[curpro].but;
t = t + a[curpro].but;
avgwt = avgwt + a[curpro].wtt;
avgtt = avgtt + a[curpro].tat;
gc[i] = curpro;
}
printf("\n-------------------------------------\n");
printf("Process\tArtime\tbrusttt\tturnTi\tWatTi\n");
printf("\n-------------------------------------\n");
for(i=0;i<pro;i++)
{
printf("%d\t %d\t %d\t %d\t %d\
n",a[i].p,a[i].art,a[i].but,a[i].tat,a[i].wtt);
}
printf("***************************************\n");
ganttchart(a,gc,pro);
printf("***************************************\n");
avgwt = avgwt/pro;
avgtt = avgtt/pro;
printf("Average Waiting Time : %.2f\n",avgwt);
printf("Average Turnaround Time : %.2f\n",avgtt);
return 0;
}

SET B:
1)
#include <stdio.h>

int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Details of %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest]
&& burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] -
temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}

average_waiting_time = wait_time / limit;


average_turnaround_time = turnaround_time / limit;
printf("\nAverage Waiting Time:t%lf\n", average_waiting_time);
printf("Average Turnaround Time:t%lf\n", average_turnaround_time);
return 0;
}

2)
#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT,PR;
};
struct process a[10];

void swap(int *b,int *c)


{
int tem;
tem=*c;
*c=*b;
*b=tem;
}

int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time , Burst time and priority of the process\
n");
printf("AT BT PR\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PR);
a[i].id=i+1;

if(i==0)
check_ar=a[i].AT;

if(check_ar!=a[i].AT )
check_ar=1;

if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
swap(&a[j].PR,&a[j+1].PR);
}
}
}
}

if(check_ar!=0)
{
a[0].WT=a[0].AT;
a[0].TAT=a[0].BT-a[0].AT;

Cmp_time=a[0].TAT;
Total_WT=Total_WT+a[0].WT;
Total_TAT=Total_TAT+a[0].TAT;
for(int i=1;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);

}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;

Cmp_time=Cmp_time+a[i].BT;

a[i].TAT=Cmp_time-a[i].AT;
Total_TAT=Total_TAT+a[i].TAT;

}
}

else
{
for(int i=0;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);
}

}
a[i].WT=Cmp_time-a[i].AT;

Cmp_time=Cmp_time+a[i].BT;
a[i].TAT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
Total_TAT=Total_TAT+a[i].TAT;

Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;

printf("The process are\n");


printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",a[i].id,a[i].WT,a[i].TAT);
}

printf("Avg waiting time is: %f\n",Avg_WT);


printf("Avg turn around time is: %f",Avg_TAT);
return 0;
}

SET C :
Q1:
#include<stdio.h>

int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}

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

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

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=total/n;
total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Q2:
#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]\t%d\t %d\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("\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
Assignment 4:
SET A:
#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,time[MAX];

void accept()
{
int i;

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


scanf("%d", &n);

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


scanf("%d", &m);

printf("Enter reference string:\n");


for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("\n\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}

printf("Total Page Faults: %d\n",faults);


}

int search(int pno)


{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}

int get_lru()
{
int i,min_i,min=9999;

for(i=0;i<n;i++)
{
if(time[i]<min)
{
min = time[i];
min_i = i;
}
}

return min_i;
}
void lru()
{
int i,j,k;

for(i=0;i<m && sp<n;i++)


{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
time[sp]=i;
faults++;
sp++;

for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
time[k]=i;

for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_lru();
frames[sp] = ref[i];
time[sp] = i;
faults++;

for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
time[k]=i;
}
}
void fifo()
{
int i,j;

for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];

}
}
}

int main()
{
accept();
lru();
fifo();
disp();

return 0;
}

SET B:
#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];

void accept()
{
int i;

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


scanf("%d", &n);

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


scanf("%d", &m);

printf("Enter reference string:\n");


for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}

void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("\n\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}

printf("Total Page Faults: %d\n",faults);


}

int search(int pno)


{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}
int get_mfu(int sp)
{
int i,max_i,max=-9999;

i=sp;
do
{
if(count[i]>max)
{
max = count[i];
max_i = i;
}
i=(i+1)%n;
}while(i!=sp);

return max_i;
}

void mfu()
{
int i,j,k;

for(i=0;i<m && sp<n;i++)


{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
count[sp]++;
faults++;
sp++;

for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
count[k]++;

sp=0;
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_mfu(sp);
frames[sp] = ref[i];
count[sp]=1;
faults++;
sp = (sp+1)%n;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
count[k]++;
}
}

int main()
{
accept();
mfu();
disp();

return 0;
}

You might also like