Operating System Programs:
Assignment 1:
Set A(1)
/*
Implement the C Program to create a child process using fork(), display parent and child
process id. Child process will display the message “I am Child Process” and the parent
process should display “I am Parent Process".
*/
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int p;
p = fork();
if(p<0)
printf("\n program cannot executed..");
else if(p==0)
{
printf("\n I'm child Process..");
printf("\n the child process id is:%d",getpid());
}
else
{
printf("\n I'm the Parent Process..");
printf("\n the parent process id is:%d",getppid());
}
}
/* OUTPUT:
I'm the Parent Process..
the parent process id is:31547
I'm child Process..
the child process id is:31555
*/
(2)
/*
Write a program that demonstrates the use of nice() system call. After a child process is
started using fork(), assign higher priority to the child using nice() system call
*/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main() {
int pid, retnice;
printf("press DEL to stop process \n");
pid = fork();
for(;;)
{
if(pid == (0))
{
retnice = nice (-5);
printf("child gets higher CPU priority %d\n", retnice);
sleep(1);
}
else{
retnice = nice (4);
printf("Parent gets lower CPU priority %d\n", retnice);
sleep(1);
}
}
}
/* OUTPUT:
press DEL to stop process
Parent gets lower CPU priority 4
child gets higher CPU priority -1
Parent gets lower CPU priority 8
child gets higher CPU priority -1
child gets higher CPU priority -1
Parent gets lower CPU priority 12
child gets higher CPU priority -1
Parent gets lower CPU priority 16
child gets higher CPU priority -1
Parent gets lower CPU priority 19
child gets higher CPU priority -1
Parent gets lower CPU priority 19
child gets higher CPU priority -1
Parent gets lower CPU priority 19
child gets higher CPU priority -1
Parent gets lower CPU priority 19
child gets higher CPU priority -1
Parent gets lower CPU priority 19
*/
Set B
(1)
/*
Implement the C program to accept n integers to be sorted. Main function creates child
process using fork system call. Parent process sorts the integers using bubble sort and
waits for child process using wait system call. Child process sorts the integers using
insertion sort.
*/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
void bubblesort(int arr[30],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
}
void insertionsort(int arr[30], int n)
{
int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while(j>=0 && key <= arr[j])
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void fork1()
{
int arr[25],arr1[25],n,i,status;
printf("\nEnter the no of values in array :");
scanf("%d",&n);
printf("\nEnter the array elements :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
int pid=fork();
if(pid==0)
{
sleep(10);
printf("\nchild process\n");
printf("child process id=%d\n",getpid());
insertionsort(arr,n);
printf("\nElements Sorted Using insertionsort:");
printf("\n");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("\b");
printf("\nparent process id=%d\n",getppid());
system("ps -x");
}
else
{
printf("\nparent process\n");
printf("\nparent process id=%d\n",getppid());
bubblesort(arr,n);
printf("Elements Sorted Using bubblesort:");
printf("\n");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("\n\n\n");
}
}
int main()
{
fork1();
return 0;
}
/* OUTPUT:
Enter the no of values in array :6
Enter the array elements :9
8
7
5
6
4
parent process
parent process id=32032
Elements Sorted Using bubblesort:
4,5,6,7,8,9
child process
child process id=32315
Elements Sorted Using insertionsort:
4,5,6,7,8,9
*/
Set B
(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;
}
/* OUTPUT:
Parent process
ID : 2415
Child process
ID: 2423
Parent -ID: 2422
Child process
ID: 2423
Parent -ID: 1
*/
Assignment 2:
Set A
Write a C program that behaves like a shell which displays the command prompt‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘count’ as
myshell$ count c filename: It will display the number of characters in given file
myshell$ count w filename: It will display the number of words in given file
myshell$ count l filename: It will display the number of lines in given file
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <string.h>
void count(char *tok2 ,char *tok3)
{
int l=0,w=0,c=0;
FILE *fp;
fp = fopen(tok3,"r");
if(fp==NULL)
printf("\n file not exist");
else
{
while(!feof(fp))
{
char ch;
ch = fgetc(fp);
if(ch == ' ' )
w++;
else if(ch == '\n')
{
w++;
l++;
}
else
c++;
}
if(strcmp(tok2,"w") == 0)
printf("\n word count :%d",w);
if(strcmp(tok2,"c") == 0)
printf("\n character count :%d",c);
if(strcmp(tok2,"l") == 0)
printf("\n line count :%d",l);
}
}
int main(int argc , char *argv[])
{
char command[30],tok1[30],tok2[30],tok3[30];
while(1)
{
printf("\n MyShell:");
gets(command);
int ch = sscanf(command,"%s%s%s",tok1,tok2,tok3);
if(ch == 3)
{
if(strcmp(tok1,"count")==0)
count(tok2,tok3);
continue;
}
/*
if(fork() == 0)
{
switch(ch)
{
case 1: execlp(tok1, NULL);
break;
case 2: execlp(tok1,tok2, NULL);
break;
case 3: execlp(tok1,tok2,tok3, NULL);
break;
}
}
*/
}
}
/*
OUTPUT:
MyShell:count l Search_File.txt
line count :5
MyShell:count w Search_File.txt
word count :10
MyShell:count c Search_File.txt
character count :53
*/
Set B
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<dirent.h>
void list(char *tok2, char *tok3)
{
DIR *dp;
int c =0;
struct dirent *dir;
dp = opendir(tok3);
if(dp == 0)
printf("%s directory not exit",tok3);
else
{
if(strcmp(tok2,"n")==0)
{
while((dir=readdir(dp))!= NULL)
c++;
printf("no of files directory : %d ",c);
}
if(strcmp(tok2,"f")==0)
{
while((dir=readdir(dp))!= NULL)
printf("%s \n ",dir->d_name);
}
if(strcmp(tok2,"i")==0)
{
while((dir=readdir(dp))!= NULL)
printf("%s\t\t%d\n",dir->d_name,dir->d_ino);
}
}
}
int main(int argc , char * argv[])
{
char tok1[20],tok2[20],tok3[20];
int choice,f;
char cmd[40];
while(1)
{
printf("\n Myshell$");
gets(cmd);
if(strcmp(cmd, "exit")==0)
exit(0);
int choice = sscanf(cmd,"%s%s%s",&tok1,&tok2,&tok3);
if(choice==3)
{
if(strcmp(tok1,"list")==0)
list(tok2,tok3);
continue;
}
/*
if(fork()==0)
{
switch(choice)
{
case 1:
execlp(tok1,NULL);
break;
case 2:
execlp(tok1,tok2,NULL);
break;
case 3:
execlp(tok1,tok2,tok3,NULL);
break;
}
}
*/
}
}
Set C
(1)
/*Write C program that behaves like a shell which display the command prompt 'myshell$'.
iy accept the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command 'typeline' as:
myshell$ typeline n filename : it will display first n lines of file.
myshell$ typeline -n filename : it will display last n lines of file.
myshell$ typeline a filename : it will display all the lines of file.
- (n is positive integer value, e.g n=3 which print first 3 lines)
- (-n is negative integer value, e.g n=-3 which print last 3 lines)
- (a means print all lines in textfile)
*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
void typeline(char *tok2,char *tok3){
char ch,str[30];
int lc=0,s,count=0;
FILE *fp;
fp = fopen(tok3,"r");
if(fp == NULL){
printf("File does not exist.\n");
}else{
printf("File exist\n");
}
if(strcmp(tok2,"a") == 0){
while(!feof(fp)){
ch = fgetc(fp);
printf("%c",ch);
}
}
else{
int n = atoi(tok2);
if(n>0){
while(lc<n){
ch = fgetc(fp);
if(ch == '\n')
lc++;
printf("%c",ch);
}
}
if(n<0){
while(!feof(fp)){
ch = fgetc(fp);
if(ch == '\n')
lc++;
}
s = lc + n;
s++;
fseek(fp,0,SEEK_SET);
while(!feof(fp)){
ch = fgetc(fp);
while (count!=s)
{
ch = fgetc(fp);
if(ch == '\n')
count++;
}
printf("%c",ch);
}
}
}
}
int main(){
char tok1[10],tok2[10],tok3[10],tok4[10],cmd[30];
int choice;
while(1){
printf("\n$MYShell$:");
gets(cmd);
if(strcmp(cmd,"exit") == 0){
exit(0);
}
int choice = sscanf(cmd,"%s%s%s%s",&tok1,&tok2,&tok3,&tok4);
if(strcmp(tok1,"typeline") == 0){
typeline(tok2,tok3);
continue;
}
}
}
/*
OUTPUP:
$MYShell$:typeline a type.txt
File exist
hey Good Evening......
I,m swapnil jamble
Learn with nil
practise makes improvement
Improvement makes u better
$MYShell$:typeline 3 type.txt
File exist
hey Good Evening......
I'm swapnil...
Learn with nil
$MYShell$:typeline -3 type.txt
File exist
Learn with nil
practise makes improvement
Improvement makes u better
*/
type.txt :
hey Good Evening......
I'm swapnil...
Learn with nil
practise makes improvement
Improvement makes u better
2)
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’.
It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘search’ as
myshell$ search f filename pattern : It will search the first occurrence of pattern in the given
file
myshell$ search a filename pattern : It will search all the occurrence of pattern in the given file
myshell$ search c filename pattern : It will count the number of occurrence of pattern in the
given file
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
void search(char *tok2,char *tok3,char *tok4)//tok2:command tok3:pattern tok4:Textfilename
{
FILE *fp;
int lineno=0,count=0;
char str[50];
fp=fopen(tok4,"r");
if(fp==NULL)
{
printf("File does not exist");
exit(0);
}
else
{
if(strcmp(tok2,"f")==0)//First occurance of pattern
{
while(!feof(fp))
{
fgets(str,50,fp);
lineno++;
if(strstr(str,tok3))
{
printf("First occurance of '%s' is on line : %d\n",tok3,lineno);
break;
}
}
}
if(strcmp(tok2,"c")==0)//Total number of occurances of pattern
{
while(!feof(fp))
{
fgets(str,50,fp);
if(strstr(str,tok3))
{
count++;
}
}
printf("Total number occurance of pattern(%s) is : %d\n",tok3,count);
}
if(strcmp(tok2,"a")==0)//Total occurances of pattern
{
while(!feof(fp))
{
fgets(str,50,fp);
lineno++;
if(strstr(str,tok3))
{
printf("\n Total Occurance of pattern:\n %d %s\n",lineno,str);
}
}
}
}
}
void main(int argc,char *argv[])
{
char cmd[20],tok1[20],tok2[20],tok3[20],tok4[20];
while(1)
{
printf("\nMyShell$:");
gets(cmd);
if(strcmp(tok1,"exit")==0)
exit(0);
int ch=sscanf(cmd,"%s%s%s%s",tok1,tok2,tok3,tok4);
if(ch==4)
{
if(strcmp(tok1,"search")==0)
{
search(tok2,tok3,tok4);
continue;
}
}
/*if(fork()==0)
{
switch(ch)
{
case 1:
execlp(tok1,NULL);
break;
case 2:
execlp(tok1,tok2,NULL);
break;
case 3:
execlp(tok1,tok2,tok3,NULL);
break;
case 4:
execlp(tok1,tok2,tok3,tok4,NULL);
break;
}
}*/
}
}
/*Output:-
MyShell$:search f learn Search_File.txt
First occurance of 'learn' is on line : 1
MyShell$:search c learn Search_File.txt
Total number occurance of pattern(learn) is : 3
MyShell$:search a learn Search_File.txt
Total Occurance of pattern:
1 learn world
Total Occurance of pattern:
2 learnwithnil
Total Occurance of pattern:
5 learn with nil
MyShell$:exit
*/
SearchFile.txt :
learn world
learnwithnil
Music world
learn with nil
Learn
Assignment 3:
Set A:
i.
Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPUburst for different n number of processes should be input to the algorithm. Assume that
the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly.
The output should give Gantt chart, turnaround time and waiting time for each process.
Also find the average waiting time and turnaround time.
// FCFS
#include<stdio.h>
#include<stdlib.h>
struct job{
int atime; //arraival time.
int btime; //brust time.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
}p[10];
int arr[10],burst[10],n,rq[10],no_rq=0,time=0;
void main()
{
int i,j;
printf("\nEnter the no. of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nEnter the arrival time of p%d:",i);
scanf("%d",&p[i].atime); //Assigning the arrival time.
arr[i] = p[i].atime;
}
for(i=0; i<n; i++)
{
printf("\nThe arrival time of p%d:",i);
printf("%d\n",p[i].atime); //Printing the arrival time.
}
for(i=0; i<n; i++)
{
printf("\nEnter the burst time of p%d:",i);
scanf("%d",&p[i].btime); //Assigning the burst time.
burst[i] = p[i].btime;
}
for(i=0; i<n; i++)
{
printf("\nThe burst time of p%d:",i);
printf("%d\n",p[i].btime); //Printing the burst time.
}
printf("\n");
addrq(); //Adding the process.
// Process start now.
printf("\nGantt Chart is:");
while(1)
{
j = selectionjob(); //sercah the process now.
if(j == -1)
{
printf("CPU is ideal");
time++;
addrq();
}
else
{
while(burst[j]!=0)
{
printf("\t P %d",j);
burst[j]--;
time++;
addrq();
}
p[j].ft = time;
}
if(fshall() == 1)
break;
}
int Ttat = 0,Twt =0;
printf("\n");
printf("\nProcess \t FT \t TAT \t WT");
for(i=0;i<n;i++)
{
p[i].tat = p[i].ft-p[i].atime;
p[i].wt = p[i].tat-p[i].btime;
printf("\n P %d \t\t %d \t %d \t %d",i,p[i].ft,p[i].tat,p[i].wt);
Ttat += p[i].tat;
Twt += p[i].wt;
}
float avgtat = Ttat /n;
float avgwt = Twt /n;
printf("\n\nAverage of turn around time is :%f",avgtat);
printf("\nAverage of waiting time is :%f",avgwt);
}
int addrq()
{
int i;
for(i=0;i<n;i++)
{
if(arr[i] == time)
{
rq[no_rq] = i;
no_rq++;
}
}
}
int selectionjob()
{
int i,j;
if(no_rq == 0)
return 1;
j = rq[0];
deleteq(j);
return j;
}
int deleteq(int j)
{
int i;
for(i=0;i<no_rq;i++)
if(rq[i] == j)
break;
for(i=i+1; i<no_rq; i++)
rq[i-1] = rq[i];
no_rq--;
}
int fshall()
{
int i;
for(i=0;i<n;i++)
if(burst[i]!=0)
return -1;
return 1;
}
/* OUTPUT:
Enter the no. of process:3
Enter the arrival time of p0:0
Enter the arrival time of p1:3
Enter the arrival time of p2:1
The arrival time of p0:0
The arrival time of p1:3
The arrival time of p2:1
Enter the burst time of p0:3
Enter the burst time of p1:1
Enter the burst time of p2:2
The burst time of p0:3
The burst time of p1:1
The burst time of p2:2
Gantt Chart is: P 0 P 0 P 0 P 2 P 2 P 1
Process FT TAT WT
P 0 3 3 0
P 1 6 3 2
P 2 5 4 2
Average of turn around time is :3.000000
Average of waiting time is :1.000000
*/
ii.
Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The
arrival time and first CPU-burst for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
// NON-Premptive SJF
#include<stdio.h>
#include<stdlib.h>
struct job{
int atime; //arraival time.
int btime; //brust time.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
}p[10];
int arr[10],burst[10],n,rq[10],no_rq=0,time=0;
void main()
{
int i,j;
printf("\nEnter the no. of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nEnter the arrival time of p%d:",i);
scanf("%d",&p[i].atime); //Assigning the arrival time.
arr[i] = p[i].atime;
}
for(i=0; i<n; i++)
{
printf("\nThe arrival time of p%d:",i);
printf("%d\n",p[i].atime); //Printing the arrival time.
}
for(i=0; i<n; i++)
{
printf("\nEnter the burst time of p%d:",i);
scanf("%d",&p[i].btime); //Assigning the burst time.
burst[i] = p[i].btime;
}
for(i = 0;i<n;i++)
{
printf("\nThe burst time of p%d:",i);
printf("%d\n",p[i].btime); //Printing the burst time.
}
printf("\n");
addrq(); //Adding the process.
// Process start now.
printf("Gantt Chart is:");
while(1)
{
j = selectionjob(); //search the process now.
if(j == -1)
{
printf("CPU is ideal");
time++;
addrq();
}
else
{
while(burst[j]!=0)
{
printf("\t P %d",j);
burst[j]--;
time++;
addrq();
}
p[j].ft = time;
}
if(fshall() == 1)
break;
}
int Ttat = 0,Twt =0;
printf("\n");
printf("\nProcess \t FT \t TAT \t WT");
for(i=0;i<n;i++)
{
p[i].tat = p[i].ft-p[i].atime;
p[i].wt = p[i].tat-p[i].btime;
printf("\n P %d \t\t %d \t %d \t %d",i,p[i].ft,p[i].tat,p[i].wt);
Ttat += p[i].tat;
Twt += p[i].wt;
}
float avgtat = Ttat /n;
float avgwt = Twt /n;
printf("\n\nAverage of turn around time is:%f",avgtat);
printf("\nAverage of waiting time is:%f",avgwt);
}
int addrq()
{
int i;
for(i=0;i<n;i++)
{
if(arr[i] == time)
{
rq[no_rq] = i;
no_rq++;
}
}
}
int selectionjob()
{
int i,j;
if(no_rq == 0)
return 1;
j = rq[0];
for(i=1;i<no_rq;i++)
if(burst[j]>burst[rq[i]])
j = rq[i];
deleteq(j);
return j;
}
int deleteq(int j)
{
int i;
for(i=0;i<no_rq;i++)
if(rq[i] == j)
break;
for(i= i+1;i<no_rq;i++)
rq[i-1] = rq[i];
no_rq--;
}
int fshall()
{
int i;
for(i=0;i<n;i++)
if(burst[i]!=0)
return -1;
return 1;
}
/*
Output:-
*/
Set B:
i.
Write the program to simulate Preemptive Shortest Job First (SJF) -scheduling. The
arrival time and first CPU-burst for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
// preemptive sjf
#include<stdio.h>
struct process
{
int WT,AT,BT,TAT;
};
struct process a[10];
int main()
{
int n,temp[10];
int count=0,t=0,short_P;
float total_WT=0, total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time and burst time of the process\n");
printf("AT WT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
// copying the burst time in
// a temp array for the further use
// in calculation of WT
temp[i]=a[i].BT;
}
// we initialize the burst time
// of a process with the maximum
a[9].BT=10000;
// loop will be execute until all the process
// complete so we use count!= number of
// the process
for(t=0;count!=n;t++)
{
// for finding min burst
// it is useful
short_P=9;
for(int i=0;i<n;i++)
{
if(a[i].BT<a[short_P].BT && (a[i].AT<=t && a[i].BT>0))
{
short_P=i;
}
}
a[short_P].BT=a[short_P].BT-1;
// if any process is completed
if(a[short_P].BT==0)
{
// one process complete
count++;
a[short_P].WT=t+1-a[short_P].AT-temp[short_P];
a[short_P].TAT=t+1-a[short_P].AT;
// total calculation
total_WT=total_WT+a[short_P].WT;
total_TAT=total_TAT+a[short_P].TAT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
// printing of the answer
printf("Id WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,a[i].WT,a[i].TAT);
}
printf("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process %f\n",Avg_TAT);
}
/*
Enter the number of the process
3
Enter the arrival time and burst time of the process
AT WT
0 6
1 3
2 9
Id WT TAT
1 3 9
2 0 3
3 7 16
Avg waiting time of the process is 3.333333
Avg turn around time of the process 9.333333
*/
ii.
Write the program to simulate Non-preemptive Priority scheduling. The arrival time and
first CPU-burst and priority for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
// non preemptive priority scheduling algorithm
#include<stdio.h>
#include<stdlib.h>
struct job{
int atime; // arraival time.
int btime; //brust time.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
int pri; //Priority varilable is add.
}p[10];
int arr[10],burst[10],n,rq[10],no_rq=0,time=0,j=-1;
void main(){
int i,j;
printf("\nEnter the no. of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nEnter the arrival time of p%d:",i);
scanf("%d",&p[i].atime); //Assigning the arrival time.
arr[i] = p[i].atime;
}
for(i=0; i<n; i++)
{
printf("\nThe arrival time of p%d:",i);
printf("%d\n",p[i].atime); //Printing the arrival time.
}
for(i=0; i<n; i++)
{
printf("\nEnter the burst time of p%d:",i);
scanf("%d",&p[i].btime); //Assigning the burst time.
burst[i] = p[i].btime;
}
for(i=0; i<n; i++)
{
printf("\nThe burst time of p%d:",i);
printf("%d\n",p[i].btime); //Printing the burst time.
}
printf("\n");
for(i=0; i<n; i++)
{
printf("Enter the Priority p%d:",i);
scanf("%d",&p[i].pri); //Assigning the Priority.
}
for(i = 0;i<n;i++)
{
printf("\nThe Priority of p%d:",i);
printf("%d",p[i].pri); //Printing the Priority.
}
printf("\n");
addrq(); //Adding the process.
// Process start now.
printf("\nGantt Chart is:");
while(1)
{
j = selectionjob(); //sercah the process now.
if(j == -1)
{
printf("CPU is ideal");
time++;
addrq();
}
else
{
while(burst[j]!=0)
{
printf("\t P %d",j);
burst[j]--;
time++;
addrq();
}
p[j].ft = time;
}
if(fshall() == 1)
break;
}
int Ttat = 0,Twt =0;
printf("\n");
printf("\nProcess \t FT \t TAT \t WT");
for(i=0;i<n;i++)
{
p[i].tat = p[i].ft-p[i].atime;
p[i].wt = p[i].tat-p[i].btime;
printf("\n P %d \t\t %d \t %d \t %d",i,p[i].ft,p[i].tat,p[i].wt);
Ttat += p[i].tat;
Twt += p[i].wt;
}
float avgtat = Ttat /n;
float avgwt = Twt /n;
printf("\n\nAverage of turn around time is :%f",avgtat);
printf("\nAverage of waiting time is :%f",avgwt);
}
int addrq()
{
int i;
for(i=0;i<n;i++)
{
if(arr[i] == time)
{
if(j!=-1 && p[i].pri>p[j].pri)
{
rq[no_rq] = i;
j = i;
}
else
rq[no_rq++] = i;
}
}
}
int selectionjob()
{
int i,k;
if(no_rq == 0)
return -1;
k = rq[0];
for(i=1;i<no_rq;i++)
if(p[k].pri<p[rq[i]].pri)
{
k = rq[i];
}
deleteq(k);
return k;
}
int deleteq(int k)
{
int i;
for(i=0;i<no_rq;i++)
if(rq[i] == k)
break;
for(i= i+1;i<no_rq;i++)
rq[i-1] = rq[i];
no_rq--;
}
int fshall()
{
int i;
for(i=0;i<n;i++)
if(burst[i]!=0)
return -1;
return 1;
}
/*
Enter the no. of process:7
Enter the arrival time of p0:0
Enter the arrival time of p1:2
Enter the arrival time of p2:1
Enter the arrival time of p3:4
Enter the arrival time of p4:6
Enter the arrival time of p5:5
Enter the arrival time of p6:7
The arrival time of p0:0
The arrival time of p1:2
The arrival time of p2:1
The arrival time of p3:4
The arrival time of p4:6
The arrival time of p5:5
The arrival time of p6:7
Enter the burst time of p0:3
Enter the burst time of p1:5
Enter the burst time of p2:4
Enter the burst time of p3:2
Enter the burst time of p4:9
Enter the burst time of p5:4
Enter the burst time of p6:10
The burst time of p0:3
The burst time of p1:5
The burst time of p2:4
The burst time of p3:2
The burst time of p4:9
The burst time of p5:4
The burst time of p6:10
Enter the Priority p0:2
Enter the Priority p1:6
Enter the Priority p2:3
Enter the Priority p3:5
Enter the Priority p4:7
Enter the Priority p5:4
Enter the Priority p6:10
The Priority of p0:2
The Priority of p1:6
The Priority of p2:3
The Priority of p3:5
The Priority of p4:7
The Priority of p5:4
The Priority of p6:10
Gantt Chart is: P 0 P 0 P 0 P 1 P 1 P 1 P 1 P 1 P 6 P 6 P 6 P 6 P 6 P 6 P 6 P 6 P 6 P 6 P 4 P 4 P 4 P 4 P 4 P 4 P 4 P 4 P 4 P 3 P 3 P 5 P 5 P 5 P 5 P 2 P 2 P 2 P 2
Process FT TAT WT
P 0 3 3 0
P 1 8 6 1
P 2 37 36 32
P 3 29 25 23
P 4 27 21 12
P 5 33 28 24
P 6 18 11 1
Average of turn around time is :18.000000
Average of waiting time is :13.000000
*/
Set C:
i.
Write the program to simulate Preemptive Priority scheduling. The arrival time and first
CPU-burst and priority for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
// Premptive-Priority
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
struct job{
bool isc;
int at; //arraival time.
int bt; //brust time.
int st; //Start time.
int nst; //new start time.
int oft; //old finish time;
int pcount; // preocess to be count.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
int pri; //Priority varilable is add.
}p[100];
int n,arr[100],tm=0,arrv=0,count=0,j=-1;
float Tat,Wt;
void minbrust(int a[],int k){
int min=p[a[1]].bt,i,m=a[1];
for(i=1;i<=k;i++)
{
if(p[a[i]].bt<min && j!=-1 && p[i].pri>p[j].pri)
{
min=p[a[i]].bt;
m=a[i];
}
}
process(m);
}
process(int s)
{
int k;
p[s].pcount++;
if(p[s].pcount==1)
{
p[s].wt=p[s].st-p[s].at;
p[s].st=tm;
}
p[s].nst=tm;
tm++;
k=p[s].nst-p[s].oft;
p[s].oft=tm;
if(k>0)
p[s].wt+=k;
if(p[s].pcount==p[s].bt)
{
p[s].isc=true;
p[s].ft=tm;
p[s].tat=p[s].ft-p[s].at;
Tat+=p[s].tat;
Wt+=p[s].wt;
}
printf("p%d ",s);
}
selecta(){
int i;
for(i=1;i<=n;i++)
{
if(p[i].at<=tm && p[i].isc==false)
arr[++arrv]=i;
}
}
void main(){
int i,k=0,a[100];
printf("How many Process: ");
scanf("%d",&n);
printf("Enter :-\nProcess BT AT Priority\n");
for(i=1;i<=n;i++){
printf("p%d\t",i);
scanf("%d%d%d",&p[i].bt,&p[i].at,&p[i].pri);
p[i].isc=false;
p[i].pcount=0;
count += p[i].bt;
p[i].wt=0;
}
printf("Process BT AT Priority\n");
for(i=1;i<=n;i++)
printf("p%d \t%d \t%d \t%d\n",i,p[i].bt,p[i].at,p[i].pri);
printf("\nGantt chart\n");
for(i=1;i<=n;i++)
{
if(p[i].at==0)
{
a[++k]=i;
}
}
minbrust(a,k);
while(tm!=count)
{
selecta();
if(arrv==0)
{
printf("|idl");
tm+=1;
count+=1;
}
else
{
minbrust(arr,arrv);
arrv=0;
}
}
printf("\n _________________________#######################______________________\n");
printf("\nProcess\tBT\tAT\tPri\tST\tWT\tFT\tTAT\n");
for(i=1;i<=n;i++)
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i,p[i].bt,p[i].at,p[i].pri,p[i].st,p[i].wt,p[i].ft,p[i].tat);
printf("\nAvg wait time=%f",Wt/n);
printf("\nAvg TAT=%f\n",Tat/n);
}
/*
Output:-
How many Process: 3
Enter :-
Process BT AT Priority
p1 3 1 3
p2 2 2 2
p3 5 0 1
Process BT AT Priority
p1 3 1 3
p2 2 2 2
p3 5 0 1
Gantt chart
p3 p1 p1 p1 p2 p2 p3 p3 p3 p3
_________________________#######################______________________
Process BT AT Pri ST WT FT TAT
P1 3 1 3 1 0 4 3
P2 2 2 2 4 2 6 4
P3 5 0 1 0 5 10 10
Avg wait time=2.333333
Avg TAT=5.666667
--------------------------------
Process exited after 24.57 seconds with return value 18
Press any key to continue . . .
*/
ii.
Write the program to simulate Round Robin (RR) scheduling. The arrival time and first
CPU-burst for different n number of processes should be input to the algorithm. Also
give the time quantum as input. Assume the fixed IO waiting time (2 units). The next
CPU-burst should be generated randomly. The output should give Gantt chart, turnaround
time and waiting time for each process. Also find the average waiting time and
turnaround time.
/*RR Scheduling*/
#include<stdio.h>
#include<stdlib.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;
}
/*
Enter Total Number of Processes: 3
Enter Details of Process[1]
Arrival Time: 0
Burst Time: 3
Enter Details of Process[2]
Arrival Time: 2
Burst Time: 2
Enter Details of Process[3]
Arrival Time: 1
Burst Time: 5
Enter Time Quantum: 2
Process ID Burst Time Turnaround Time Waiting Time
Process[2] 2 2
0
Process[1] 3 7
4
Process[3] 5 9
4
Average Waiting Time: 2.666667
Avg Turnaround Time: 6.000000
*/
OR
//rr
#include<stdio.h>
void 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; //contains process number
}
//sorting burst time in ascending order using selection 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; //waiting time for first process will be zero
//calculate waiting time
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; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
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=(float)total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
/*
Enter number of process:3
Enter Burst Time:
p1:3
p2:5
p3:7
Process Burst Time Waiting Time Turnaround Time
p1 3 0 3p2 5 3 8p3 7 8 15
Average Waiting Time=3.666667
Average Turnaround Time=8.666667
*/
ASSIGNMENT 4:
Demand Paging:
Set A
/*
write the simulation program to implement demand paging and show the page sheduling
and total number of page faults for the following given page reference string.
Give input n as the number of memory frames.
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
1. Implementing FIFO
*/
#include<stdio.h>
int main()
{
int i,j,n,f,k,avail,fcount=0; // Declaration of variable required
int ref_str[50],frame[10];
printf("\nENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n); // total no. of pages in reference string
printf("\nENTER THE PAGE NUMBER :\n");
for(i=1; i<=n; i++) // accept entire reference string
scanf("%d",&ref_str[i]);
printf("\nENTER THE NUMBER OF FRAMES :");
scanf("%d",&f); // accept no of frames
for(i=0; i<f; i++)
frame[i] = -1; // initialize all page frame to -1
j = 0; // initialize page frame pointer
printf("\nref string \t page frames \t\tHit/Fault\n");
for(i=1; i<=n; i++)
{
printf("%d\t\t",ref_str[i]);
avail = 0; // Default value of available flag is 0
for(k=0; k<f; k++)
if(frame[k] == ref_str[i]) // input of page requested is compared with existing content of FRAME
{
avail = 1; // as page found available is turned 1
for(k=0; k<f; k++)
printf("%d\t",frame[k]); // Print Current state of FRAME
printf("H"); // Indication of Page Hit
}
if (avail == 0) // input page requested NOT existing in FRAME
{
frame[j] = ref_str[i]; // place page requested at j th location in FRAME
j = (j+1)%f; // Update J for next Cycle
fcount++; // Increment Counter for Page Fault
for(k=0; k<f; k++)
printf("%d\t",frame[k]); // Print Current state of FRAME
printf("F"); // Indication of Page Fault
}
printf("\n");
}
printf("\nPage Fault Is: %d",fcount);
return 0;
}
/* OUTPUT:
ENTER THE NUMBER OF PAGES:
16
ENTER THE PAGE NUMBER :
12
15
12
18
6
8
11
12
19
12
6
8
12
15
19
8
ENTER THE NUMBER OF FRAMES :3
ref string page frames Hit/Fault
12 12 -1 -1 F
15 12 15 -1 F
12 12 15 -1 H
18 12 15 18 F
6 6 15 18 F
8 6 8 18 F
11 6 8 11 F
12 12 8 11 F
19 12 19 11 F
12 12 19 11 H
6 12 19 6 F
8 8 19 6 F
12 8 12 6 F
15 8 12 15 F
19 19 12 15 F
8 19 8 15 F
Page Fault Is: 14
*/
2) Implement LRU
/*
write the simulation program to implement demand paging and show the page sheduling
and total number of page faults for the following given page reference string.
Give input n as the number of memory frames.
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
1. Implementing LRU
*/
#include<stdio.h>
int pf=0,pg;
int f,frame[5],ref_str[50],victim=0,time[50];
int searchpage(int p)
{
int i;
for(i=0; i<f; i++)
if(frame[i] == p )
return i;
return -1;
}
int selectvictim()
{
int min=0,i;
for(i=0; i<f; i++)
{
if(time[i] < time[min])
min = i;
}
return min;
}
int main()
{
int i;
printf("\nEnter the no of pages:");
scanf("%d",&pg);
printf("\nEnter the reference string:");
for(i=0; i<pg; i++)
scanf("%d",&ref_str[i]);
printf("\nEnter the no of frames:");
scanf("%d",&f);
printf("\nref string \t page frames \n");
for(i=0; i<f; i++)
frame[i] = -1;
for(i=0; i<f; i++)
time[i] = -1;
for(i=0; i<pg; i++)
{
int k,j;
k = searchpage(ref_str[i]);
if(k == -1) // page fault
{
pf ++;
k = selectvictim();
frame[k] = ref_str[i];
}
time[k] = i;
printf("\n pg = %d |",ref_str[i]);
for(j=0; j<f; j++)
printf("\t%d",frame[j]);
}
printf("\n page fault: %d",pf);
}
/* OUTPUT:
Enter the no of pages:16
Enter the reference string:12
15
12
18
6
8
11
12
19
12
6
8
12
15
19
8
Enter the no of frames:3
ref string page frames
pg = 12 | 12 -1 -1
pg = 15 | 12 15 -1
pg = 12 | 12 15 -1
pg = 18 | 12 15 18
pg = 6 | 12 6 18
pg = 8 | 8 6 18
pg = 11 | 8 6 11
pg = 12 | 8 12 11
pg = 19 | 19 12 11
pg = 12 | 19 12 11
pg = 6 | 19 12 6
pg = 8 | 8 12 6
pg = 12 | 8 12 6
pg = 15 | 8 12 15
pg = 19 | 19 12 15
pg = 8 | 19 8 15
page fault: 13
*/
Write the simulation program for demand panging and show the page scheduling and total number of faults according to the MRU (Most Recently Used ) page replacement algorithm.
Assume the memory of n frames.
Reference String: 8,5,7,8,5,7,2,3,7,3,5,9,4,6,2
#include<stdio.h>
int main()
{
int ref_str[20],frame[10];
int f,n,i,k,fcount=0,avail;
printf("\nPls. Enter the no. of pages:");
scanf("%d",&n);
printf("\nEnter the Reference String:");
for(i=1;i<=n;i++)
scanf("%d",&ref_str[i]);
printf("\nEnter the no. of frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
frame[i] = -1;
printf("\nRef String \t Frames \t page falut or hit\n");
for(i=1;i<=n;i++)
{
printf("%d",ref_str[i]);
avail = 0;
for(k=0;k<f;k++)
if(frame[k] == ref_str[i]) // check ref_string already in frame
{
avail =1;
for(k=0;k<f;k++)
printf("\t%d",frame[k]);
printf("\tH");
}
else if(frame[k] == -1)
{
avail =1;
fcount++;
frame[k] = ref_str[i];
for(k=0; k<f; k++)
printf("\t%d",frame[k]);
printf("\tF");
break;
}
if(avail == 0) // if ref_string is not in frame
{
for(k=0; k<f; k++)
{
if(frame[k] == ref_str[i-1] ) // check most recently used page (ref_str[i-1])
{
frame[k] = ref_str[i];
fcount++;
for(k=0;k<f;k++)
{
printf("\t%d",frame[k]);
}
printf("\tF");
}
}
}
printf("\n");
}
printf("\nTotal number of Page Fault: %d",fcount);
}
/* OUTPUT:
Pls. Enter the no. of pages:15
Enter the Reference String:8
5
7
8
5
7
2
3
7
3
5
9
4
6
2
Enter the no. of frames:3
Ref String Frames page falut or hit
8 8 -1 -1 F
5 8 5 -1 F
7 8 5 7 F
8 8 5 7 H
5 8 5 7 H
7 8 5 7 H
2 8 5 2 F
3 8 5 3 F
7 8 5 7 F
3 8 5 3 F
5 8 5 3 H
9 8 9 3 F
4 8 4 3 F
6 8 6 3 F
2 8 2 3 F
Total number of Page Fault: 11
*/
Optimal Page Replacement Algorithm:
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page 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]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
s
s
nice
ReplyDeletegreat
ReplyDeleteAmazing bro..🥰
ReplyDelete❤️
ReplyDelete//OPtimal page replacement algorithm
ReplyDelete#include
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page 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])
{
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < no_of_frames; ++j)
{
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k)
{
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j)
{
if(temp[j] == -1)
{
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
printf("\n pg=%d |",pages[i]);
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
/****************OUTPUT*************
[ty92@ugilinux ~]$ cc OPT.c
[ty92@ugilinux ~]$ ./a.out
Enter number of frames: 3
Enter number of pages: 12
Enter page reference string: 1 2 3 4 1 2 5 1 2 3 4 5
pg=1 |1
pg=1 |-1
pg=1 |-1
pg=2 |1
pg=2 |2
pg=2 |-1
pg=3 |1
pg=3 |2
pg=3 |3
pg=4 |1
pg=4 |2
pg=4 |4
pg=1 |1
pg=1 |2
pg=1 |4
pg=2 |1
pg=2 |2
pg=2 |4
pg=5 |1
pg=5 |2
pg=5 |5
pg=1 |1
pg=1 |2
pg=1 |5
pg=2 |1
pg=2 |2
pg=2 |5
pg=3 |3
pg=3 |2
pg=3 |5
pg=4 |4
pg=4 |2
pg=4 |5
pg=5 |4
pg=5 |2
pg=5 |5
Total Page Faults = 7[ty92@ugilinux ~]$
*/
//MFU
ReplyDelete#include
struct node
{
int pno,freq;
}frames[20];
int n;
int page_found(int pno)
{
int fno;
for(fno=0;fnoframes[selfno].freq)
selfno=fno;
return selfno;
}
void main()
{
int p_request[100];
int size;
int page_fault=0,i,j,fno;
printf("\nEnter how many pages : ");
scanf("%d",&size);
printf("\nEnter Reference string : ");
for(i=0;i<size;i++)
scanf("%d",&p_request[i]);
printf("\nHow many frames:"); scanf("%d",&n);
//initialize frames
for (i=0; i<n; i++)
{
frames[i].pno=-1;
frames[i].freq=0;
}
printf("\nPageNo Page Frames Page Fault");
printf("\n---------------------------------------------------");
for(i=0;i<size;i++)
{
j=page_found(p_request[i]);
if(j==-1) //page fault occurs
{
j=get_free_frame();
if (j==-1) //no free frame - do page replacement
j=get_mfu_frame();
page_fault++;
frames[j].pno=p_request[i];
frames[j].freq=1;
printf("\n%d\t ",p_request[i]);
for (fno=0; fno<n; fno++)
printf("%d\t",frames[fno].pno);
printf(" : YES");
}
else //page found in frame j
{
printf("\n%d\t ",p_request[i]);
frames[j].freq++;
for (fno=0; fno<n; fno++)
printf("%d\t",frames[fno].pno);
printf(" : NO");
}
}
printf("\n-------------------------------------------------------");
printf("\n Number of Page_Falts=%d",page_fault);
}
/********OUTPUT********
[ty92@ugilinux ~]$ cc MFU.c
[ty92@ugilinux ~]$ ./a.out
Enter how many pages : 12
Enter Reference string : 1 2 3 4 1 2 5 1 2 3 4 5
How many frames:3
PageNo Page Frames Page Fault
---------------------------------------------------
1 1 -1 -1 : YES
2 1 2 -1 : YES
3 1 2 3 : YES
4 4 2 3 : YES
1 1 2 3 : YES
2 1 2 3 : NO
5 1 5 3 : YES
1 1 5 3 : NO
2 2 5 3 : YES
3 2 5 3 : NO
4 2 5 4 : YES
5 2 5 4 : NO
-------------------------------------------------------
Number of Page_Falts=8[ty92@ugilinux ~]$
*/
//FIFO
ReplyDelete#include
int page[40];
int n,frame[5],ref[50],victim=-1,pf=0;
int searchp(int p)
{
int i;
for(i=0;i<n;i++)
if(frame[i]==p)
return i;
return -1;
}
int selectvictim()
{
victim++;
return victim%n;
}
main()
{
int i,m;
printf("\nEnter the no. of pages:");
scanf("%d",&m);
printf("\nEnter the reference string:");
for(i=0;i<m;i++)
scanf("%d",&page[i]);
printf("\nEnter the no. of frames:");
scanf("%d",&n);
for(i=0;i<n;i++)
frame[i]=-1;
for(i=0;i<m;i++)
{
int k,j;
k=searchp(page[i]);
if(k==-1)
{
pf++;
k=selectvictim();
frame[k]=page[i];
}
printf("\n req=%d",page[i]);
for(j=0;j<n;j++)
printf("\t%d",frame[j]);
}
printf("\nPage Fault = %d",pf);
}
/**************OUTPUT************
[ty92@ugilinux ~]$ cc FIFO.c
[ty92@ugilinux ~]$ ./a.out
Enter the no. of pages:12
Enter the reference string:1 2 3 4 1 2 5 1 2 3 4 5
Enter the no. of frames:3
req=1 1 -1 -1
req=2 1 2 -1
req=3 1 2 3
req=4 4 2 3
req=1 4 1 3
req=2 4 1 2
req=5 5 1 2
req=1 5 1 2
req=2 5 1 2
req=3 5 3 2
req=4 5 3 4
req=5 5 3 4
Page Fault = 9[ty92@ugilinux ~]$
*/