0% found this document useful (0 votes)
25 views

OS1-practsolutionset

OSI practical solutions

Uploaded by

artic1899
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

OS1-practsolutionset

OSI practical solutions

Uploaded by

artic1899
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

TyBSc.(comp.

)
Operating System-1
Practical Solution Set

1
Assignment 1 Operations on Processes
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 ChildProcess” and the
parent process should display “I am Parent Process”.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork() Create a child process
int pid = fork();
if (pid > 0) {
printf("I am Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("I am Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n", getpid());
}
else {
printf("Failed to create child process");
}
return 0;
}
Output :
I am Parent process
ID : 3698
I am Child process
ID: 3699

2
(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>
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
printf("\nI am child process, id=%d\n",getpid());
printf("\nPriority :%d,id=%d\n",nice (-7),getpid());
}
else
{
printf("\nI am parent process, id=%d\n",getpid());
nice(1);
printf("\nPriority :%d,id=%d\n",nice (15),getpid());
}
return 0;
}
Output:
I am parent process, id=6555
Priority :6,id=6555
I am child process, id=6556
Priority :-17,id=6556

3
SET –B
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.
Program :-
#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=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void insertionsort(int arr[30], int n)
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
j = i - 1;
while(j>=0 && temp <= arr[j])
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = temp;
}
}

4
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 :5
Enter the array elements :2 3 4 1 6
parent process
parent process id=3610
Elements Sorted Using bubblesort:
1,2,3,4,6,

5
Assignment No.2: Operations on Processes
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 <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 count(char *fn, char op)


{
int fh,cc=0,wc=0,lc=0;
char c;

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

6
}

while(read(fh,&c,1)>0)
{
if(c==' ') wc++;
else if(c=='\n')
{
wc++;
lc++;
}
cc++;
}

close(fh);

switch(op)
{
case 'c':
printf("No.of characters:%d\n",cc);
break;
case 'w':
printf("No.of words:%d\n",wc);
break;
case 'l':
printf("No.of lines:%d\n",lc);
break;
}
}

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],"count")==0)
count(args[2],args[1][0]);
else
{
pid = fork();

7
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}

SET-B
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 ‘list’ as
myshell$ list f dirname: It will display filenames in a given directory.
myshell$ list n dirname: It will count the number of entries in a given directory.
myshell$ list i dirname: It will display filenames and their inode number for the files in
a given directory.

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

8
}

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;

while(1)

9
{
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 (1)
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 ‘typeline’ as
myshell$ typeline n filename: It will display first n lines of the file.
myshell$ typeline -n filename: It will display last n lines of the file.
myshell$ typeline a filename: It will display all the lines of the file.
#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;

10
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)

11
{
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");
}
}
}

12
return 0;
}

(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 <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)

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

14
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;
}

15
Assignment 3- CPU Scheduling

Set A:

i. Write the program to simulate FCFS CPU-scheduling. The arrival time and
first CPU- burst 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.

#include<stdio.h>
#define MAX 20

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

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

16
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;
}

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();
fifo();
disp();

return 0;
}

17
ii)
a 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.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

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


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

p->bt1 = p->bt;
p->next = NULL;

if(first==NULL)
first=p;
else

18
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname\tat\tbt\tct\ttat\twt\n");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s\t%d\t%d\t%d\t%d\t%d\n",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f\tAvg WT=%f\n",


avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname\tat\tbt\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\n",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;

19
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_sjf()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{

20
if(p->at<=time && p->bt1!=0 &&
p->bt1<min)
{
min = p->bt1;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void sjfnp()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_sjf();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

21
p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
sjfnp();
print_output();
print_gantt_chart();

22
return 0;
}

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.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

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


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)

23
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname\tat\tbt\tct\ttat\twt\n");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s\t%d\t%d\t%d\t%d\t%d\n",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f\tAvg WT=%f\n",


avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname\tat\tbt\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\n",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()

24
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_sjf()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)

25
{
if(p->at<=time && p->bt1!=0 &&
p->bt1<min)
{
min = p->bt1;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void sjfp()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_sjf();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time++;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

26
p->ct = time;
p->bt1--;

if(p->bt1==0)
n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
sjfp();
print_output();

27
print_gantt_chart();

return 0;
}

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1,p;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

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


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

printf("Enter priority:");
scanf("%d",&p->p);

28
p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname\tat\tbt\tp\ttct\ttat\twt\n");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n",
p->pname,p->at,p->bt,p->p,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f\tAvg WT=%f\n",


avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname\tat\tbt\tp\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\n",

29
p->pname,p->at,p->bt1,p->p);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

t = p->p;
p->p = q->p;
q->p = t;
}

q=q->next;
}

p=p->next;
}
}

30
int time;

NODE * get_p()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->p<min)
{
min = p->p;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void pnp()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_p();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;

31
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);

32
}
}

int main()
{
accept_info();
sort();
pnp();
print_output();
print_gantt_chart();

return 0;
}

33
Assignment No. 4: Demand Paging

Set A
i i. Write the simulation program to implement demand paging and show the page
scheduling and total number of page faults for the following given page reference string.
Give input n as the number of memory frames.
1) Implement FIFO
2) Implement LRU

Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8

1]FIFO
#include<stdio.h>
#define MAX 20

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

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++)

34
{
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;
}

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();
fifo();
disp();

35
return 0;
}

2) LRU
#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");
}

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

37
}
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;
}
}

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

return 0;
}

Set B:
I I. Write the simulation program to implement demand paging and show the page
scheduling and total number of page faults for the following given page reference string.
Give input n as the number of memory frames.
1) Implement LFU
2) Implement MFU
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8

1) LFU
#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,

38
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++)
{

39
if(frames[i]==pno)
return i;
}

return -1;
}

int get_lfu(int sp)


{
int i,min_i,min=9999;

i=sp;
do
{
if(count[i]<min)
{
min = count[i];
min_i = i;
}
i=(i+1)%n;
}while(i!=sp);

return min_i;
}

void lfu()
{
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++)

40
{
k = search(ref[i]);
if(k==-1)
{
sp = get_lfu(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();
lfu();
disp();

return 0;
}

2) MFU

#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++)

41
{
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
{

42
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]++;

43
}
}

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

return 0;
}

44

You might also like