OS Full

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Q.1 Write a program to implement the concept of orphan process sleep() Using fork().

Q.2 Write the simulation program using FCFS. The arrival time and first CPU bursts of different jobs should be
input to the system. Assume the fixed I/O waiting time (2 units). The next CPU burst should be generated using
random function. The output should give the Gantt chart, Turnaround Time and Waiting time for each process
and average times.
#include<stdio.h>
#include<string.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];

struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];

int finish,time,n,k,prev;

void getinput()
{
int i;
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;

printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);

void bubble()
{
struct Input t;
int i,j;
for(i=1;i<n;i++)
for(j=0;j< n-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}

void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\n *******Final Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);

int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}

void processinput()
{
int i=0,j;
finish = k = 0;
time=tab[0].at;
while(finish!=n)
{
if(arrived(time))
{
if(tab[i].tbt!=0)
{
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
if(time < tab[(i+1)%n].at)
{
i=0;
}
else
i = (i+1)%n;

}
}

void ganttchart()
{
int i,j=1;
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
for(i=0;i<n;i++)
{
tab[i].tbt = tab[i].bt=rand()%10+1;
tab[i].at=tab[i].ct+2;
}
processinput();
printoutput();
ganttchart();
}
Q.1 Write a C program to accept the number of process and resources and find the need matrix content and display it.
#include<stdio.h>
int nop,nor,A[10][10],M[10][10],Av[10],N[10][10],finish[10];
void acceptdata(int x[10][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&x[i][j]);
}
}
}
void acceptav()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}
void calcneed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j]=M[i][j]-A[i][j];
}
void displaydata()
{
int i,j;
printf("\n\tAllocation \t\tMax\t\tNeed\n\t");
for(i=0;i<3;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
printf("\navailable");
for(i=0;i<nor;i++)
printf("%4d",Av[i]);
}
main()
{
printf("\nEnter No of Processes: ");
scanf("%d",&nop);
printf("\nEnter No. of Resources: ");
scanf("%d",&nor);
printf("\nEnter Allocation Matrix: ");
acceptdata(A);
printf("\nEnter Max Matrix: ");
acceptdata(M);
printf("\nEnter Availability:");
acceptav();
calcneed();
displaydata();

}
Q.2 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.
Reference String : 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1
a. Implement FIFO
#include<stdio.h>
int nor,nof,refstring[30],F[10];
void accept()
{
int i;
printf("\nEnter the Reference String:\n ");
for(i=0;i<nor;i++)
{
printf("[%d]: ",i);
scanf("%d",&refstring[i]);
}
}
int search(int page)
{
int i;
for(i=0;i<nof;i++)
{
if(page==F[i])
return i;
}
return -1;
}
void FIFO()
{
int i,j,k,fno=0,fault=0;
for(i=0;i<nor;i++)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
//printf("\nk=%d",k);
if(k==-1)
{
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
fno=(fno+1)%nof;
}
}
printf("\nTotal no of Page fault: %d",fault);
}
main()
{
printf("\nEnter the Length of the string: ");
scanf("%d",&nor);
printf("\nEnter no. of Frames: ");
scanf("%d",&nof);
accept();
FIFO();}

Q.1 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>
main()
{
int pid,retnice,i;
pid=fork();
for(i=0;i<3;i++)
{
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);
}
}
}
Q.2. Extend the shell to implement the commands ”list” which works as follows:
myshell$ list f dirname: It will display filenames in a given directory.
myshell$ list ndirname: 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>
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;
}
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-1);
break;
case 'w':
printf("No.of words:%d\n",wc);
break;
case 'l':
printf("No.of lines:%d\n",lc+1);
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();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}
return 0;
}
Q.1 Creating a child process using the command execv(). Note down process ids of the parent and the child processes,
check whether the control is given back to the parent after the child process terminates.
#include <stdio.h>
#include <sys/types.h>

void parse(char *line, char **argv)


{
while (*line != '\0') {
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0';
*argv++ = line;
while (*line != '\0' && *line != ' ' &&
*line != '\t' && *line != '\n')
line++;
}
*argv = '\0';
}

void execute(char **argv)


{
pid_t pid;
int status;

if ((pid = fork()) < 0) {


printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) {
if (execvp(*argv, argv) < 0) {
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else {
while (wait(&status) != pid)
;
}
}
void main(void)

char line[1024];
char *argv[64];

while (1) {
printf("Shell -> ");
gets(line);
printf("\n");
parse(line, argv);
if (strcmp(argv[0], "exit") == 0)
exit(0);
execute(argv);
}
}

Q.2 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>
struct Input
{
char pname[10];
int bt,at,ct,tbt,p;
}tab[5];

struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];

int finish,time,n,k,prev,q;

void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
printf("Enter priority: ");
scanf("%d",&tab[i].p);

tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;

printf("\nProcess\tBT\tAT\tpriority");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at,tab[i].p);
getch();
}

void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].p<min)
{
min = tab[i].p;
mini = i;
}
return mini;
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\nAverage TAT = %f",AvgTAT);
printf("\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
while(finish!=n)
{
if(arrived(time))
{
i=getmin(time);
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}

}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}

}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\nData after sorting according to arrival time-: ");
printinput();
processinput();

printoutput();

ganttchart();

for(i=0;i<n;i++)
{
tab[i].tbt = tab[i].bt=rand()%10+1;
tab[i].at=tab[i].ct+2;
}
printf("\nData after random generation: ");
printinput();
processinput();
printoutput();
ganttchart();
}
Q.1 Write a program to illustrate the concept of orphan process sleep() Using fork().
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0) {
printf("Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("Child process\n");
printf("ID: %d\n", getpid());
printf("Parent -ID: %d\n\n", getppid());
sleep(10);
printf("\nChild process \n");
printf("ID: %d\n", getpid());
printf("Parent -ID: %d\n", getppid());
}
else {
printf("Failed to create child process");
}
return 0;
}
Q.2.Write a C program to simulate Bankers algorithm for the purpose of deadlock avoidance. Consider the following
snapshot of system, A, B, C and D are the resource type

Allocation MA Available
X
A B C D A B C D A B C D
P0 0 0 1 2 P0 0 0 1 2 1 5 2 0
P1 1 0 0 0 P1 1 7 5 0
P2 1 3 5 4 P2 2 3 5 6
P3 0 6 3 2 P3 0 6 5 2
P4 0 0 1 4 P4 0 6 5 6

a) Calculate and display the content of need matrix?


b) Is the system in safe state? If display the safe sequence.
#include<stdio.h>
int nop,nor,A[10][10],M[10][10],Av[10],N[10][10],finish[10];
void acceptdata(int x[10][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&x[i][j]);
}
}
}
void acceptav()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}
void calcneed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j]=M[i][j]-A[i][j];

}
void displaydata()
{
int i,j;
printf("\n\tAllocation \t\tMax\t\tNeed\n\t");
for(i=0;i<3;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
printf("\navailable");
for(i=0;i<nor;i++)
printf("%4d",Av[i]);
}
int checkneed(int pno)
{
int i;
for(i=0;i<nor;i++)
if(N[pno][i]>Av[i])
return 0;
return 1;
}
void banker()
{
int p=0,j=0,k=0,flag=0,safe[10];
while(flag<2)
{
if(!finish[p])
{
printf("\n\nNeed of process P%d (,",p);
for(j=0;j<nor;j++)
printf("%d,",N[p][j]);
if(checkneed(p))
{
printf(") <= available (");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf(")");

printf("\nNeed is Satsified, So process P%d can be granted requiered


resources.\n After P%d finishes, it will realease all the resources.",p,p);
for(j=0;j<nor;j++)
Av[j]=Av[j]+A[p][j];

printf("New Availble=");
for(j=0;j<nor;j++)
printf("%d ",Av[j]);
finish[p]=1;
safe[k++]=p;

}
else
{
printf(") > available (");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf(")");

printf("\nNeed is not Satsified, So process P%d cannot be granted


required resources.\n process P%d has to wait.",p,p);
}
}
if((p+1)%nop==0)
flag++;
p=(p+1)%nop;
}//while
if(k==nop)
{
printf("\nSystem is in safe state...");
printf("\nSafe Sequence: ");
for(j=0;j<k;j++)
printf("P%d->",safe[j]);
}
else
printf("\nSystem is not in safe state....");
}
main()
{
printf("\nEnter No of Processes: ");
scanf("%d",&nop);
printf("\nEnter No. of Resources: ");
scanf("%d",&nor);
printf("\nEnter Allocation Matrix: ");
acceptdata(A);
printf("\nEnter Max Matrix: ");
acceptdata(M);
printf("\nEnter Availability:");
acceptav();
calcneed();
displaydata();
banker();
}

Q.1 Write a C program to accept the number of process and resources and find the need matrix content and display
it. {check Lowerside}

Q.2 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.
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
• Implement OPT
#include<stdio.h>
int nor,nof,refstring[30],F[10];
void accept()
{
int i;
printf("\nEnter the Reference String:\n ");
for(i=0;i<nor;i++)
{
printf("[%d]: ",i);
scanf("%d",&refstring[i]);
}
}
int search(int page)
{
int i;
for(i=0;i<nof;i++)
{
if(page==F[i])
return i;
}
return -1;

}
int getfno(int i)
{
int fno,prev,pos=0,fpos,flag;
for(fno=0;fno<nof;fno++)
{
flag=0;
for(prev=i+1;prev<nor;prev++)
{
if(F[fno]==refstring[prev])
{
flag=1;
if(prev>pos) //2<3
{
pos=prev; //pos=2
fpos=fno; //fpos=2
}
break;
}
}
if(flag==0)
{
fpos=fno;
break;
}
}
//printf("\nfpos=%d",fpos);
return fpos;

}
void optimal()
{
int i,j,k,fno,fault=0;
for(fno=0,i=0;fno<nof && i<nor;i++)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
//printf("\nk=%d",k);
if(k==-1)
{
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
fno++;
}
}
while(i<nor)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
//printf("\nk=%d",k);
if(k==-1)
{
fno=getfno(i);
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
}
i++;
}
printf("\nTotal no of Page fault: %d",fault)
}
main()
{
printf("\nEnter the Length of the string: ");
scanf("%d",&nor);
printf("\nEnter no. of Frames: ");
scanf("%d",&nof);
accept();
optimal();
}

Q.1 Write a program to find the execution time taken for execution use clock() function. (This function clock() is
called at the beginning of program and again at the end of the program and the difference between the values
returned gives the time spent by processor on the program.)
#include <stdio.h>
#include <time.h>
void my_function(){
float f;
for(f=0.0; f<1000000; f=f+1.0);
}
int main() {
clock_t start, end;
start = clock();
my_function();
end = clock();
double duration = ((double)end - start)/CLOCKS_PER_SEC;
printf("Time taken to execute in seconds : %f", duration);
return 0;
}
Q.2 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, turn
around time and waiting time for each process. Also find the average waiting time and turn around time.
#include<stdio.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev,q;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
printf("Enter Time Quantum: ");
scanf("%d",&q);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;

printf("\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\nAverage TAT = %f",AvgTAT);
printf("\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
time=tab[0].at;
while(finish!=n)
{
if(arrived(time))
{
if(tab[i].tbt!=0)
{
for(j=0;j<q;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
if(time < tab[(i+1)%n].at)
{
i=0;
}
else
i = (i+1)%n;
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
for(i=0;i<n;i++)
{
tab[i].tbt = tab[i].bt=rand()%10+1;
tab[i].at=tab[i].ct+2;
}
printf("\nData after random generation: ");
printinput();
processinput();
printoutput();
ganttchart();
}
Q.1 Create a child process using fork(), display parent and child process id. Child process will display the message
“Hello World” and the parent process should display “Hi”.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Error in creation of Child Process");
exit(1);
}
else if(pid==0)
{
printf("\n Hello I am Child Process");
printf("\n My pid=%d",getpid());
exit(0);
}
else
{
printf("\n Hello, I am Parent Process");
printf("\n My pid is =%d",getpid());
exit(1);
}
}

Q.2 Write the simulation program to implement demand paging and show the page schedulingand total
number of page faults for the following given page reference string. Give input nas the number of memory
frames.
Reference String :3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
a. Implement FIFO
#include<stdio.h>
int nor,nof,refstring[30],F[10];
void accept()
{
int i;
printf("\nEnter the Reference String:\n ");
for(i=0;i<nor;i++)
{
printf("[%d]: ",i);
scanf("%d",&refstring[i]);
}
}
int search(int page)
{
int i;
for(i=0;i<nof;i++)
{
if(page==F[i])
return i;
}
return -1;
}
void FIFO()
{
int i,j,k,fno=0,fault=0;
for(i=0;i<nor;i++)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
//printf("\nk=%d",k);
if(k==-1)
{
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
fno=(fno+1)%nof;
}
}
printf("\nTotal no of Page fault: %d",fault);
}
main()
{
printf("\nEnter the Length of the string: ");
scanf("%d",&nor);
printf("\nEnter no. of Frames: ");
scanf("%d",&nof);
accept();
FIFO();
}

Q.1 Creating a child process without terminating the parent process Write a program to create a child process using
fork().The parent should goto sleep state and child process should begin its execution. In the child process, use execl()
to execute the “ls” command.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int pid;
main()
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)// Child Process
{
execlp("/bin/ls","ls",NULL);
//system(buff);
}
else // Parent Process
sleep(5);
}
Q.2 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, turn around time and waiting
time for each process. Also find the average waiting time and turn around time.{check upperside or lowerside}

Q.1 Write Python program to find minimum jumps to reach a given destination from a given source.
import sys

def findMinJumps(nums, i, n, lookup):

if i == n - 1:
return 0
if i >= n or nums[i] == 0:
return sys.maxsize

if lookup[i]:
return lookup[i]
min_jumps = sys.maxsize
for j in range(i + 1, i + nums[i] + 1):
cost = findMinJumps(nums, j, n, lookup)
if cost != sys.maxsize:
min_jumps = min(min_jumps, cost + 1)

lookup[i] = min_jumps
return lookup[i]
def findMinimumJumps(nums):

if not nums:
return 0

lookup = [0] * len(nums)

return findMinJumps(nums, 0, len(nums), lookup)

if __name__ == '__main__':

nums = [1, 3, 6, 1, 0, 9]

print('The minimum jumps required to reach the destination are',


findMinimumJumps(nums))

Q.2 Consider the system with 3 resources types A,B, and C with 7,2,6 instances respectively. Consider the following
snapshot:

Allocation Request Total


Resources
A B C A B C A B C
P0 0 1 0 P0 0 0 0 7 2 6
P1 2 0 0 P1 2 0 2
P2 3 0 3 P2 0 0 1
P3 2 1 1 P3 1 0 0
P4 0 0 2 P4 0 0 2
Answer the following questions:

i. Display the contents of Available array?


ii. Is there any deadlock? Print the message
Q.1 Write Python program for a* algorithm.

def aStarAlgo(start_node, stop_node):


open_set = set(start_node)
closed_set = set()
g = {} #store distance from starting node
parents = {}
g[start_node] = 0
parents[start_node] = start_node
while len(open_set) > 0:
n = None
for v in open_set:
if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
n=v
if n == stop_node or Graph_nodes[n] == None:
pass
else:
for (m, weight) in get_neighbors(n):
if m not in open_set and m not in closed_set:
open_set.add(m)
parents[m] = n
g[m] = g[n] + weight
else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
print('Path does not exist!')
return None
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print('Path found: {}'.format(path))
return path
open_set.remove(n)
closed_set.add(n)

print('Path does not exist!')


return None
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 99,
'D': 1,
'E': 7,
'G': 0,
}

return H_dist[n]
Graph_nodes = {
'A': [('B', 2), ('E', 3)],
'B': [('C', 1),('G', 9)],
'C': None,
'E': [('D', 6)],
'D': [('G', 1)],
}
aStarAlgo('A', 'G')

Q.2 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.
Reference String :12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
• Implement LRU
#include<stdio.h>
int nor,nof,refstring[30],F[10];
void accept()
{
int i;
printf("\nEnter the Reference String:\n ");
for(i=0;i<nor;i++)
{
printf("[%d]: ",i);
scanf("%d",&refstring[i]);
}
}
int search(int page)
{
int i;
for(i=0;i<nof;i++)
{
if(page==F[i])
return i;
}
return -1;
}
int getfno(int i)
{
int fno,prev,pos=99,fpos;
for(fno=0;fno<nof;fno++)
{
for(prev=i-1;prev>=0;prev--)
{
if(F[fno]==refstring[prev])
{
if(prev<pos)
{
pos=prev;
fpos=fno;
}
break;
}
}
}
;
return fpos;
}
void LRU()
{
int i,j,k,fno,fault=0;
for(fno=0,i=0;fno<nof && i<nor;i++)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
fno++;
}
}
while(i<nor)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
fno=getfno(i);
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
}
i++;
}
printf("\nTotal no of Page fault: %d",fault);

}
main()
{
printf("\nEnter the Length of the string: ");
scanf("%d",&nor);
printf("\nEnter no. of Frames: ");
scanf("%d",&nof);
accept();
LRU();
}

Q.1 Create a child process using fork(), display parent and child process id. Child process will display the message
“Hello World” and the parent process should display “Hi”. { for answer Check the upperside}

Q.2 Write the simulation program using SJF(non-preemptive). The arrival time and first CPU bursts of different jobs
should be input to the system. The Assume the fixed I/O waiting time (2 units).The next CPU burst should be
generated using random function. The output should give the Gantt chart, Turnaround Time and Waiting time for
each process and average times.
#include<stdio.h>
#include<string.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev;
void getinput()
{
int i;
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;
printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\n *******Final Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].tbt<min)
{
min = tab[i].tbt;
mini = i;
}
return mini;
}
void processinput()
{
int i,j;
finish=k=0;
while(finish!=n)
{
if(arrived(time))
{
i = getmin(time);
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
tab[i].ct=time;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}
void ganttchart()
{
int i,j=1;
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
for(i=0;i<n;i++)
{
tab[i].tbt = tab[i].bt=rand()%10+1;
tab[i].at=tab[i].ct+2;
}
processinput();
printoutput();
ganttchart();

}
Q.1 Write a program to illustrate the concept of orphan process sleep() Using fork().
Q.2 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.
Reference String :7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
• Implement LRU { check the upperside program Just change value}
Q.1 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. {check upperside}
Q.2 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>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;

printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
clrscr();
printf("\n *******Final Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].tbt<min)
{
min = tab[i].tbt;
mini = i;
}
return mini;
}
void processinput()
{
int i,j;
finish=k=0;
while(finish!=n)
{
if(arrived(time))
{
i = getmin(time);

time++;
tab[i].tbt--;
tab[i].ct=time;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
if(tab[i].tbt==0)
{
finish++;
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
for(i=0;i<n;i++)
{
tab[i].tbt = tab[i].bt=rand()%10+1;
tab[i].at=tab[i].ct+2;
}
processinput();
printoutput();
ganttchart();
}
Q.1 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.{ check the upperside}

Q.2 Partially implement the Menu driven Banker’s algorithm for accepting Allocation, Max from
user.
a) Accept Available
b) Display Allocation, Max
c) Find Need and Display It,
d) Display Available
#include<stdio.h>
# include <stdlib.h>
int nop,nor,A[10][10],M[10][10],Av[10],N[10][10],finish[10];
void acceptdata(int x[10][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&x[i][j]);
}
}
}
void acceptav()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}
void calcneed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j]=M[i][j]-A[i][j];

}
void displayallomax()
{
int i,j;
printf("\n\tAllocation \t\tMax\n\t");
for(i=0;i<2;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
}
}
void displayneed()
{
int i,j;
printf("\n\tNeed\n\t");
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");

for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
}
void displayavai()
{
int i,j;
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
printf("\navailable");
for(i=0;i<nor;i++)
printf("%4d",Av[i]);
}
main()
{
while(1)
{
printf("\n1. Accept Available\n2. Display Allocation, Max\n3. Find Need and Display It,\n4. Display Available\n5.
Exit\nEnter your choice");
int n;
scanf("%d",&n);
switch(n)
{
case 1:printf("\nEnter No of Processes: ");
scanf("%d",&nop);
printf("\nEnter No. of Resources: ");
scanf("%d",&nor);
printf("\nEnter Availability:");
acceptav();
break;
case 2:printf("\nEnter No of Processes: ");
scanf("%d",&nop);
printf("\nEnter No. of Resources: ");
scanf("%d",&nor);
printf("\nEnter Allocation Matrix: ");
acceptdata(A);
printf("\nEnter Max Matrix: ");
acceptdata(M);
displayallomax();
break;
case 3:calcneed();
displayneed();
break;
case 4:displayavai();
break;
case 5:exit(1);
default:printf("!!!Enter correct choice!!! ");
break;
}
}
}

Q.1 Write a program to illustrate the concept of orphan process sleep() Using fork().
Q.2 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.
Reference String : 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1
• Implement FIFO{check upperside change string value}
Q.1 Write Python program to find minimum jumps to reach a given destination from a given source.{check upperside}

Q.2 Write the simulation program to implement demand paging and show the page schedulingand total
number of page faults for the following given page reference string. Give input nas the number of
memory frames.
Reference String : 2,5,2,8,5,4,1,2,3,2,6,1,2,5,9,8
i. Implement MRU
#include<stdio.h>
int nor,nof,refstring[30],F[10];
void accept()
{
int i;
printf("\nEnter the Reference String:\n ");
for(i=0;i<nor;i++)
{
printf("[%d]: ",i);
scanf("%d",&refstring[i]);
}
}
int search(int page)
{
int i;
for(i=0;i<nof;i++)
{
if(page==F[i])
return i;
}
return -1;

}
int getfno(int i)
{
int fno,prev,pos=-1,fpos;
for(prev=i-1;prev>=0;prev--)
{
for(fno=0;fno<nof;fno++)
{
if(F[fno]==refstring[prev])
{
if(prev>pos)
{
pos=prev;
fpos=fno;
}
break;
}
}
}
;
return fpos;
}
void MRU()
{
int i,j,k,fno,fault=0;
for(fno=0,i=0;fno<nof && i<nor;i++)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
fno++;
}
}
while(i<nor)
{
printf("\n%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
fno=getfno(i);
F[fno]=refstring[i];
for(j=0;j<nof;j++)
{
if(F[j])
printf("\t%d",F[j]);
}
fault++;
}
i++;
}
printf("\nTotal no of Page fault: %d",fault);
}
main()
{
printf("\nEnter the Length of the string: ");
scanf("%d",&nor);
printf("\nEnter no. of Frames: ");
scanf("%d",&nof);
accept();
MRU();
}
Q.1 Write a program to find the execution time taken for execution use clock() function. (This function clock() is called
at the beginning of program and again at the end of the program and the difference between the values returned
gives the time spent by processor on the program.){check upperside}

Q.2 Implement the shell program that accepts the command at $ prompt displayed by your shell (myshell$).
Implement the “count” command by creating child process which works as follows:
myshell$ count c filename: To display the number of characters in given file
myshell$ count w filename: To display the number of words in given file
myshell$ count l filename: To display the number of lines in given file
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp;
int pid;
void count(char *t2,char *t3)
{
int charcount=0,wordcount=0,linecount=0;
if((fp=fopen(t3,"r"))==NULL)
printf("File not found");
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch==' ')
wordcount++;
else if(ch=='\n')
{
linecount++;
wordcount++;
}
else
charcount++;
}

fclose(fp);
if(strcmp(t2,"c")==0)
printf("The total no. of characters :%d\n",charcount);
else if(strcmp(t2,"w")==0)
printf("The total no. of words :%d\n",wordcount);
else if(strcmp(t2,"l")==0)
printf("The total no. of lines :%d\n",linecount);
else
printf("Command not found");
}
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"pause")==0)
exit(0);
else
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)
{

if(strcmp(t1,"count")==0)
count(t2,t3);
}
else {
wait(NULL);
exit(0);
}
}
}
}

Q.1 Write a python program to for a* algorithm.

Q.2 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.
Reference String : 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1
• Implement FIFO {check upperside}

Q.1 Write a python program Program for hill climbing algorithm in AI.

import random

def randomSolution(tsp):
cities = list(range(len(tsp)))
solution = []
for i in range(len(tsp)):
randomCity = cities[random.randint(0, len(cities) - 1)]
solution.append(randomCity)
cities.remove(randomCity)
return solution
def routeLength(tsp, solution):
routeLength = 0
for i in range(len(solution)):
routeLength += tsp[solution[i - 1]][solution[i]]
return routeLength

def getNeighbours(solution):
neighbours = []
for i in range(len(solution)):
for j in range(i + 1, len(solution)):
neighbour = solution.copy()
neighbour[i] = solution[j]
neighbour[j] = solution[i]
neighbours.append(neighbour)
return neighbours

def getBestNeighbour(tsp, neighbours):


bestRouteLength = routeLength(tsp, neighbours[0])
bestNeighbour = neighbours[0]
for neighbour in neighbours:
currentRouteLength = routeLength(tsp, neighbour)
if currentRouteLength < bestRouteLength:
bestRouteLength = currentRouteLength
bestNeighbour = neighbour
return bestNeighbour, bestRouteLength

def hillClimbing(tsp):
currentSolution = randomSolution(tsp)
currentRouteLength = routeLength(tsp, currentSolution)
neighbours = getNeighbours(currentSolution)
bestNeighbour, bestNeighbourRouteLength = getBestNeighbour(tsp, neighbours)

while bestNeighbourRouteLength < currentRouteLength:


currentSolution = bestNeighbour
currentRouteLength = bestNeighbourRouteLength
neighbours = getNeighbours(currentSolution)
bestNeighbour, bestNeighbourRouteLength = getBestNeighbour(tsp, neighbours)

return currentSolution, currentRouteLength

def main():
tsp = [
[0, 400, 500, 300],
[400, 0, 300, 500],
[500, 300, 0, 400],
[300, 500, 400, 0]
]

print(hillClimbing(tsp))

if __name__ == "__main__":
main()

Q.2Write 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.
Reference String :7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
i. Implement LRU {check Upperside}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy