0% found this document useful (0 votes)
40 views69 pages

Os Slip Ans

Answers and problems of os

Uploaded by

pratiktrade03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views69 pages

Os Slip Ans

Answers and problems of os

Uploaded by

pratiktrade03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

SLIP1

Q1) 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”. [10 Marks]

//fork()

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

int main()

int pid;

int status;

pid = fork();

if(pid == -1)

perror("bad fork");

exit(1);

if (pid == 0)

printf("hello world %d.\n",getpid());

else

wait(&status);

printf("hi %d.\n",getpid());

return 0;

Q2) 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. Implement FIFO Reference String : 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1

//FIFO
#include <stdio.h>

int main()

int incomingStream[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};

int pageFaults = 0;

int frames = 3;

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");

int temp[frames];

for(m = 0; m < frames; m++)

temp[m] = -1;

for(m = 0; m < pages; m++)

s = 0;

for(n = 0; n < frames; n++)

if(incomingStream[m] == temp[n])

s++;

pageFaults--;

pageFaults++;

if((pageFaults <= frames) && (s == 0))

temp[m] = incomingStream[m];
}

else if(s == 0)

temp[(pageFaults - 1) % frames] = incomingStream[m];

printf("\n");

printf("%d\t\t\t",incomingStream[m]);

for(n = 0; n < frames; n++)

if(temp[n] != -1)

printf(" %d\t\t\t", temp[n]);

else

printf(" - \t\t\t");

printf("\nTotal Page Faults:\t%d\n", pageFaults);

return 0;

SLIP2

1 . Write a program to illustrate the concept of orphan process ( Using fork() and sleep()).[10marks]

//FORK() SLEEP()

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

int main()

int pid = fork();

if(pid>0)

{
//getpid() returns process id and 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(5);

// As this time parent process has finished, it will show different


//parent 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;

Q2) 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. Implement OPT Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8 .
[20Marks]

//OPTIMAL PAGE REPLACEMENT

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

SLIP3

Q1) 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 [10 Marks]

//nice()
#include<unistd.h>
#include<stdlib.h>/* contains prototype for wait */
#include<stdio.h>
int main()
{
int pid;
int retnice;
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
{
retnice = nice(1);
printf("Child process and Process id = %d.\n",retnice);
sleep(1);
}else
{
retnice= nice(4);
printf("Parent process and Process id = %d.\n", retnice);
sleep(1);
}
}
Q2) 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. Implement LRU Reference String
:12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8 [20Marks]
//LRU
#include<stdio.h>
int main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}

}
printf("\nThe no of page faults is %d",c);
}
SLIP4

Q1) 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. [10 Marks]

//nice()
#include<unistd.h>
#include<stdlib.h>/* contains prototype for wait */
#include<stdio.h>
int main()
{
int pid;
int retnice;
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
{
retnice = nice(1);
printf("Child process and Process id = %d.\n",retnice);
sleep(1);
}else
{
retnice= nice(4);
printf("Parent process and Process id = %d.\n", retnice);
sleep(1);
}
}

Q2) 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. Implement FIFO Reference String :3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6 [20Marks]

//FIFO

#include <stdio.h>

int main()

int incomingStream[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};

int pageFaults = 0;
int frames = 3;

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");

int temp[frames];

for(m = 0; m < frames; m++)

temp[m] = -1;

for(m = 0; m < pages; m++)

s = 0;

for(n = 0; n < frames; n++)

if(incomingStream[m] == temp[n])

s++;

pageFaults--;

pageFaults++;

if((pageFaults <= frames) && (s == 0))

temp[m] = incomingStream[m];

else if(s == 0)

temp[(pageFaults - 1) % frames] = incomingStream[m];

}
printf("\n");

printf("%d\t\t\t",incomingStream[m]);

for(n = 0; n < frames; n++)

if(temp[n] != -1)

printf(" %d\t\t\t", temp[n]);

else

printf(" - \t\t\t");

printf("\nTotal Page Faults:\t%d\n", pageFaults);

return 0;

SLIP5

Q1) 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”. [10 Marks]

//fork()

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

int main()

int pid;

int status;

pid = fork();

if(pid == -1)

perror("bad fork");

exit(1);
}

if (pid == 0)

printf("hello world %d.\n",getpid());

else

wait(&status);

printf("hi %d.\n",getpid());

return 0;

Q2) Write the simulation program for demand paging and show the page scheduling and total number
of page faults according to SECOND CHANCE page replacement algorithm. Assume the memory of 3
frames. Request Page Numbers: 9,14,10, 11, 15, 9, 11, 9, 15, 10, 9, 15, 10, 12, 15 [20Marks]

#include<stdio.h>

int refstring[20],pt[10],u[10],nof,nor;

void accept()

int i;

printf("\nEnter the reference string:");

for(i=0;i<nor;i++)

printf("[%d]=",i);

scanf("%d",&refstring[i]);

int search(int s)

int i;
for(i=0;i<nof;i++)

if(pt[i]==s)

return(i);

return(-1);

void second_chance()

int i,j,k,faults=0,sp=0;

for(i=0;i<nor;i++)

printf("%d",refstring[i]);

k=search(refstring[i]);

if(k==-1)

while(1)

if(u[sp]==0)

pt[sp]=refstring[i];

u[sp]=1;

sp=(sp+1)%nof;

faults++;

break;

else

u[sp]=0;

sp=(sp+1)%nof;
}

for(j=0;j<nof;j++)

printf("\n%3d%3d",pt[j],u[j]);

if(j==sp)

printf("*");

printf("\t");

printf("Total page faults=%d\n",faults);

int main()

printf("Enter length of reference string:");

scanf("%d",&nor);

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

scanf("%d",&nof);

accept();

second_chance();

getch();

SLIP6

Q1) Write a program to find the execution time taken for executing a given set of instructions (use
clock () function) [10 Marks]

#include<stdlib.h>/* contains prototype for wait */


#include<stdio.h>
int main()
{
int input;
int clock1,clock2;
clock1=clock();
input = fork();
if(input==0)
{
printf("Child process");
}
else{
printf("Parent process");
}
clock2=clock();
clock2=clock2-clock1;
printf("Time taken to execute is %d.\n",clock2);
}

Q2) 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
[20Marks]

//bankeralgo

#include<stdio.h>

int main() {

/* array will store at most 5 process with 3 resoures if your process or

resources is greater than 5 and 3 then increase the size of array */

int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5], terminate = 0;

printf("Enter the number of process and resources");

scanf("%d %d", & p, & c);

// p is process and c is diffrent resources

printf("enter allocation of resource of all process %dx%d matrix", p, c);

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

for (j = 0; j < c; j++) {

scanf("%d", & alc[i][j]);

}
printf("enter the max resource process required %dx%d matrix", p, c);

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

for (j = 0; j < c; j++) {

scanf("%d", & max[i][j]);

printf("enter the available resource");

for (i = 0; i < c; i++)

scanf("%d", & available[i]);

printf("\n need resources matrix are\n");

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

for (j = 0; j < c; j++) {

need[i][j] = max[i][j] - alc[i][j];

printf("%d\t", need[i][j]);

printf("\n");

/* once process execute variable done will stop them for again execution */

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

done[i] = 0;

while (count < p) {

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

if (done[i] == 0) {

for (j = 0; j < c; j++) {

if (need[i][j] > available[j])

break;

//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {

safe[count] = i;

done[i] = 1;

/* now process get execute release the resources and add them in available resources */

for (j = 0; j < c; j++) {

available[j] += alc[i][j];

count++;

terminate = 0;

} else {

terminate++;

if (terminate == (p - 1)) {

printf("safe sequence does not exist");

break;

if (terminate != (p - 1)) {

printf("\n available resource after completion\n");

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

printf("%d\t", available[i]);

printf("\n safe sequence are\n");

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

printf("p%d\t", safe[i]);

}
return 0;

SLIP7

Q1) .Write a program to illustrate the concept of orphan process (Using fork () and sleep ()) [10 Marks]
Q2) 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. Implement OPT Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8 [20
Marks]

SLIP8

Q1) Write a program to find the execution time taken for executing a given set of instructions (use
clock () function) [10 Marks] Q2) Write a simulation program using FCFS. The arrival time, CPU burst
time and number of process should be given as input to the system. The output should give the
Turned around time and waiting time for each process and calculate Average Waiting Time and
Turned around Time.[20MARKS]

SLIP9

Q1) Given an initial state of a 8-puzzle problem and final state to be reached

Given:

283

164

7 5

Output:

123

8 4

765

Find the most cost-effective path to reach the final state from initial state using A* Algorithm.
Consider g(n)=Depth of node and h(n)=Number of misplaced tiles [10 Marks]

#include<stdio.h>

#include<string.h>

#include<unistd.h>

#include<sys/types.h>

#include <sys/stat.h>
#include <stdlib.h>

#include<time.h>

#define r 3

#define c 3

char matrix[r][c];

char new[r][c];

int count;

char final[r][c] = {{'1','2','3'},{'4','5','6'},{'7','8',' '}};

int i,j;

char z ;

int p,q,x,y;

int t =0;

int result = 0;

void load()

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

for(j=0;j<3;j++)

if(new[i][j] == '0')

matrix[i][j]= ' ';

continue;

}
matrix[i][j]= new[i][j];

void blank()

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

for(j=0;j<3;j++)

new[i][j]= ' ';

int main()

time_t T= time(NULL);

struct tm tm = *localtime(&T);

char f[4];

int rsl ;

int random,t;

int randvalues[9];

main:

count = 0;
blank();

T= time(NULL);

tm = *localtime(&T);

srand(tm.tm_sec);

while(count!=9)

rsl=rand()%9;

sprintf(f,"%d",rsl);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((new[i][j]) == f[0])

i = 4; j = 4;

continue;

}else if((new[i][j]) == ' ')

new[i][j] = f[0];

i = 4; j = 4;

count++;

}
}

load();

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

while(1)

printf("enter value to change its position to blank space\n");

scanf(" %c",&z);

if(z=='q')

printf("\n*****You Quit*****\n");

goto main;

// break;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

{
if((matrix[i][j])== z)

p = i;

q = j;

}else if((matrix[i][j])== ' ')

x = i;

y = j;

t =0;

int m , n ;

m = p - 1;

n=q;

if(m>=0)

if((matrix[m][n])== ' ')t=1;

m = p + 1;

if(m<=2)

if((matrix[m][n])== ' ')t=1;

}
m = p;

n=q-1;

if(n>=0)

if((matrix[m][n])== ' ')t=1;

n=q+1;

if(n<=2)

if((matrix[m][n])== ' ')t=1;

if(t==1)

matrix[x][y] = z;

matrix[p][q] = ' ';

t = 0;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((matrix[i][j])== final[i][j])

t++;
}

system("clear");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

if(t==9)

printf("\n****you Win****\n");

break;

return 1;

Q2) Write a simulation program using SJF. The arrival time, CPU burst time and number of process
should be given as input to the system. The output should give the Turned around time and waiting
time for each process and calculate Average Waiting Time and Turned around Time. [ 20 Marks]

//SJF

#include<stdio.h>
#include<conio.h>

#include<stdlib.h>

void swap(int *x, int *y)

int temp=*x;

*x=*y;

*y=temp;

void sortat(int p[], int at[], int bt[], int n)

int i, j;

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

for(j=i+1;j<n;j++)

{ /* sort the process having less arrival*/

if(at[i]>at[j])

swap(&p[i], &p[j]);

swap(&at[i], &at[j]);

swap(&bt[i], &bt[j]);

/* if two processes have the same arrival time than sort them having

less burst time */

else if(at[i]==at[j])

if(bt[i]>bt[j])

swap(&p[i], &p[j]);

swap(&at[i], &at[j]);

swap(&bt[i], &bt[j]);
}

/* calculate turnaround time and waiting time */

void tatwt( int ct[], int at[], int bt[], int tat[], int wt[], int n)

int i;

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

tat[i]=ct[i]-at[i];

wt[i]=tat[i]-bt[i];

int main()

int *p, *at, *bt, *tat, *wt, *ct, pos, i, j, min=1000, n;

float awt=0, atat=0;

printf("\nenter the number of process:");

scanf("%d", &n);

p=(int*)malloc(n*sizeof(int));

at=(int*)malloc(n*sizeof(int));

bt=(int*)malloc(n*sizeof(int));

ct=(int*)malloc(n*sizeof(int));

wt=(int*)malloc(n*sizeof(int));

tat=(int*)malloc(n*sizeof(int));

printf("enter the process");

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

{
scanf("%d",&p[i]);

printf("enter the arrival time");

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

scanf("%d",&at[i]);

printf("enter the burst time");

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

scanf("%d",&bt[i]);

sortat(p, at, bt, n);

ct[0]=at[0] + bt[0];

for(i=1; i<n; i++)

for(j=i; j<n; j++)

if(at[j]<=ct[i-1])

if(bt[j]<min)

min=bt[j];

pos=j;

/* when you get less burst time process, swap p, at, bt at position 2,

and when getting 2nd less burst time swap at position 3rd and so on. */
swap(&p[i],&p[pos]);

swap(&at[i],&at[pos]);

swap(&bt[i],&bt[pos]);

min=1000;

ct[i]=ct[i-1]+bt[i];

}tatwt(ct,at,bt,tat,wt,n);

printf("\np\tat\tbt\tct\ttat\twt");

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

printf("\n%d\t%d\t%d\t%d\t%d\t%d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);

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

atat+=tat[i];

awt+=wt[i];}//average turnaround time andaveragewaitingtime

atat=atat/n;

awt=awt/n;

printf("\navgtat=%.2fandavgwt=%.2f",atat,awt);

return 0;

SLIP10

Q1) 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”. [10 Marks]

//fork()

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

int main()

int pid;

int status;
pid = fork();

if(pid == -1)

perror("bad fork");

exit(1);

if (pid == 0)

printf("hello world %d.\n",getpid());

else

wait(&status);

printf("hi %d.\n",getpid());

return 0;

Q2) 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
[20Marks]

//bankeralgo

#include<stdio.h>

int main() {

/* array will store at most 5 process with 3 resoures if your process or

resources is greater than 5 and 3 then increase the size of array */

int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5], terminate = 0;

printf("Enter the number of process and resources");

scanf("%d %d", & p, & c);

// p is process and c is diffrent resources


printf("enter allocation of resource of all process %dx%d matrix", p, c);

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

for (j = 0; j < c; j++) {

scanf("%d", & alc[i][j]);

printf("enter the max resource process required %dx%d matrix", p, c);

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

for (j = 0; j < c; j++) {

scanf("%d", & max[i][j]);

printf("enter the available resource");

for (i = 0; i < c; i++)

scanf("%d", & available[i]);

printf("\n need resources matrix are\n");

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

for (j = 0; j < c; j++) {

need[i][j] = max[i][j] - alc[i][j];

printf("%d\t", need[i][j]);

printf("\n");

/* once process execute variable done will stop them for again execution */

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

done[i] = 0;

while (count < p) {


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

if (done[i] == 0) {

for (j = 0; j < c; j++) {

if (need[i][j] > available[j])

break;

//when need matrix is not greater then available matrix then if j==c will true

if (j == c) {

safe[count] = i;

done[i] = 1;

/* now process get execute release the resources and add them in available resources */

for (j = 0; j < c; j++) {

available[j] += alc[i][j];

count++;

terminate = 0;

} else {

terminate++;

if (terminate == (p - 1)) {

printf("safe sequence does not exist");

break;

if (terminate != (p - 1)) {

printf("\n available resource after completion\n");


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

printf("%d\t", available[i]);

printf("\n safe sequence are\n");

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

printf("p%d\t", safe[i]);

return 0;

SLIP11

Q1) Given an initial state of a 8-puzzle problem and final state to be reached

283

164

7 5

123

8 4

7 6 5 Find the most cost-effective path to reach the final state from initial state using A* Algorithm.
Consider g(n)=Depth of node and h(n)=Number of misplaced tiles [10 Marks]

#include<stdio.h>

#include<string.h>

#include<unistd.h>

#include<sys/types.h>

#include <sys/stat.h>

#include <stdlib.h>

#include<time.h>

#define r 3
#define c 3

char matrix[r][c];

char new[r][c];

int count;

char final[r][c] = {{'1','2','3'},{'4','5','6'},{'7','8',' '}};

int i,j;

char z ;

int p,q,x,y;

int t =0;

int result = 0;

void load()

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

for(j=0;j<3;j++)

if(new[i][j] == '0')

matrix[i][j]= ' ';

continue;

matrix[i][j]= new[i][j];

}
}

void blank()

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

for(j=0;j<3;j++)

new[i][j]= ' ';

int main()

time_t T= time(NULL);

struct tm tm = *localtime(&T);

char f[4];

int rsl ;

int random,t;

int randvalues[9];

main:

count = 0;

blank();

T= time(NULL);

tm = *localtime(&T);
srand(tm.tm_sec);

while(count!=9)

rsl=rand()%9;

sprintf(f,"%d",rsl);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((new[i][j]) == f[0])

i = 4; j = 4;

continue;

}else if((new[i][j]) == ' ')

new[i][j] = f[0];

i = 4; j = 4;

count++;

load();

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

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

while(1)

printf("enter value to change its position to blank space\n");

scanf(" %c",&z);

if(z=='q')

printf("\n*****You Quit*****\n");

goto main;

// break;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((matrix[i][j])== z)

p = i;
q = j;

}else if((matrix[i][j])== ' ')

x = i;

y = j;

t =0;

int m , n ;

m = p - 1;

n=q;

if(m>=0)

if((matrix[m][n])== ' ')t=1;

m = p + 1;

if(m<=2)

if((matrix[m][n])== ' ')t=1;

m = p;

n=q-1;

if(n>=0)
{

if((matrix[m][n])== ' ')t=1;

n=q+1;

if(n<=2)

if((matrix[m][n])== ' ')t=1;

if(t==1)

matrix[x][y] = z;

matrix[p][q] = ' ';

t = 0;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((matrix[i][j])== final[i][j])

t++;

}
system("clear");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

if(t==9)

printf("\n****you Win****\n");

break;

return 1;

Q2) Write a simulation program using SJF. The arrival time, CPU burst time and number of process
should be given as input to the system. The output should give the Turned around time and waiting
time for each process and calculate Average Waiting Time and Turned around Time. [ 20 Marks]

//SJF

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void swap(int *x, int *y)


{

int temp=*x;

*x=*y;

*y=temp;

void sortat(int p[], int at[], int bt[], int n)

int i, j;

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

for(j=i+1;j<n;j++)

{ /* sort the process having less arrival*/

if(at[i]>at[j])

swap(&p[i], &p[j]);

swap(&at[i], &at[j]);

swap(&bt[i], &bt[j]);

/* if two processes have the same arrival time than sort them having

less burst time */

else if(at[i]==at[j])

if(bt[i]>bt[j])

swap(&p[i], &p[j]);

swap(&at[i], &at[j]);

swap(&bt[i], &bt[j]);

}
}

/* calculate turnaround time and waiting time */

void tatwt( int ct[], int at[], int bt[], int tat[], int wt[], int n)

int i;

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

tat[i]=ct[i]-at[i];

wt[i]=tat[i]-bt[i];

int main()

int *p, *at, *bt, *tat, *wt, *ct, pos, i, j, min=1000, n;

float awt=0, atat=0;

printf("\nenter the number of process:");

scanf("%d", &n);

p=(int*)malloc(n*sizeof(int));

at=(int*)malloc(n*sizeof(int));

bt=(int*)malloc(n*sizeof(int));

ct=(int*)malloc(n*sizeof(int));

wt=(int*)malloc(n*sizeof(int));

tat=(int*)malloc(n*sizeof(int));

printf("enter the process");

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

scanf("%d",&p[i]);

printf("enter the arrival time");


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

scanf("%d",&at[i]);

printf("enter the burst time");

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

scanf("%d",&bt[i]);

sortat(p, at, bt, n);

ct[0]=at[0] + bt[0];

for(i=1; i<n; i++)

for(j=i; j<n; j++)

if(at[j]<=ct[i-1])

if(bt[j]<min)

min=bt[j];

pos=j;

/* when you get less burst time process, swap p, at, bt at position 2,

and when getting 2nd less burst time swap at position 3rd and so on. */

swap(&p[i],&p[pos]);

swap(&at[i],&at[pos]);

swap(&bt[i],&bt[pos]);
min=1000;

ct[i]=ct[i-1]+bt[i];

}tatwt(ct,at,bt,tat,wt,n);

printf("\np\tat\tbt\tct\ttat\twt");

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

printf("\n%d\t%d\t%d\t%d\t%d\t%d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);

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

atat+=tat[i];

awt+=wt[i];}//average turnaround time andaveragewaitingtime

atat=atat/n;

awt=awt/n;

printf("\navgtat=%.2fandavgwt=%.2f",atat,awt);

return 0;

SLIP12

Q1)Implement AO* algorithm on the following graph [10 Marks]

class Graph:

def __init__(self, graph, heuristicNodeList, startNode):

self.graph = graph

self.H = heuristicNodeList

self.start = startNode

self.parent = {}

self.status = {}

self.solutionGraph = {}

def applyAOStar(self):

self.aoStar(self.start, False)
def getNeighbors(self, v):

return self.graph.get(v, '')

def getStatus(self, v):

return self.status.get(v, 0)

def setStatus(self, v, val):

self.status[v] = val

def getHeuristicNodeValue(self, n):

return self.H.get(n, 0)

def setHeuristicNodeValue(self, n, value):

self.H[n] = value

def printSolution(self):

print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE:", self.start)

print("------------------------------------------------------------")

print(self.solutionGraph)

print("------------------------------------------------------------")

def computeMinimumCostChildNodes(self, v):

minimumCost = 0

costToChildNodeListDict = {}

costToChildNodeListDict[minimumCost] = []

flag = True
for nodeInfoTupleList in self.getNeighbors(v):

cost = 0

nodeList = []

for c, weight in nodeInfoTupleList:

cost = cost+self.getHeuristicNodeValue(c)+weight

nodeList.append(c)

if flag == True:

minimumCost = cost

costToChildNodeListDict[minimumCost] = nodeList

flag = False

else:

if minimumCost > cost:

minimumCost = cost

costToChildNodeListDict[minimumCost] = nodeList

return minimumCost, costToChildNodeListDict[minimumCost]

def aoStar(self, v, backTracking):

print("HEURISTIC VALUES :", self.H)

print("SOLUTION GRAPH :", self.solutionGraph)

print("PROCESSING NODE :", v)

print("-----------------------------------------------------------------------------------------")

if self.getStatus(v) >= 0:

minimumCost, childNodeList = self.computeMinimumCostChildNodes(v)

print(minimumCost, childNodeList)

self.setHeuristicNodeValue(v, minimumCost)

self.setStatus(v, len(childNodeList))

solved = True
for childNode in childNodeList:

self.parent[childNode] = v

if self.getStatus(childNode) != -1:

solved = solved & False

if solved == True:

self.setStatus(v, -1)

self.solutionGraph[v] = childNodeList

if v != self.start:

self.aoStar(self.parent[v], True)

if backTracking == False:

for childNode in childNodeList:

self.setStatus(childNode, 0)

self.aoStar(childNode, False)

print("Graph - 1")

h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2,

'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1}

graph1 = {

'A': [[('B', 1), ('C', 1)], [('D', 1)]],

'B': [[('G', 1)], [('H', 1)]],

'C': [[('J', 1)]],

'D': [[('E', 1), ('F', 1)]],

'G': [[('I', 1)]]


}

G1 = Graph(graph1, h1, 'A')

G1.applyAOStar()

G1.printSolution()

Q2) Write a simulation program using FCFS. The arrival time, CPU burst time and number of process
should be given as input to the system. The output should give the Turned around time and waiting
time for each process and calculate Average Waiting Time and Turned around Time. [20Marks]

//DYNAMIC PROGRAM FCFS


#include <stdio.h>

// Function to find the waiting time for all processes

int waitingtime(int proc[], int n,

int burst_time[], int wait_time[]) {

// waiting time for first process is 0

wait_time[0] = 0;

// calculating waiting time

for (int i = 1; i < n ; i++ )

wait_time[i] = burst_time[i-1] + wait_time[i-1] ;

return 0;

// Function to calculate turn around time

int turnaroundtime( int proc[], int n,

int burst_time[], int wait_time[], int tat[]) {

// calculating turnaround time by adding

// burst_time[i] + wait_time[i]

int i;

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

tat[i] = burst_time[i] + wait_time[i];

return 0;

}
//Function to calculate average time

int avgtime( int proc[], int n, int burst_time[]) {

int wait_time[n], tat[n], total_wt = 0, total_tat = 0;

int i;

//Function to find waiting time of all processes

waitingtime(proc, n, burst_time, wait_time);

//Function to find turn around time for all processes

turnaroundtime(proc, n, burst_time, wait_time, tat);

//Display processes along with all details

printf("Processes Burst Waiting Turn around \n");

// Calculate total waiting time and total turn

// around time

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

total_wt = total_wt + wait_time[i];

total_tat = total_tat + tat[i];

printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);

printf("Average waiting time = %f\n", (float)total_wt / (float)n);

printf("Average turn around time = %f\n", (float)total_tat / (float)n);

return 0;

// main function

int main() {

//process id's

int proc[] = { 1, 2, 3};

int n = sizeof proc / sizeof proc[0];

//Burst time of all processes

int burst_time[] = {5, 8, 12};

avgtime(proc, n, burst_time);
return 0;

Q1) Implement AO* algorithm on the following graph [10 Marks]

class Graph:

def __init__(self, graph, heuristicNodeList, startNode):

self.graph = graph

self.H = heuristicNodeList

self.start = startNode

self.parent = {}

self.status = {}

self.solutionGraph = {}

def applyAOStar(self):

self.aoStar(self.start, False)

def getNeighbors(self, v):

return self.graph.get(v, '')

def getStatus(self, v):

return self.status.get(v, 0)

def setStatus(self, v, val):

self.status[v] = val

def getHeuristicNodeValue(self, n):

return self.H.get(n, 0)
def setHeuristicNodeValue(self, n, value):

self.H[n] = value

def printSolution(self):

print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE:", self.start)

print("------------------------------------------------------------")

print(self.solutionGraph)

print("------------------------------------------------------------")

def computeMinimumCostChildNodes(self, v):

minimumCost = 0

costToChildNodeListDict = {}

costToChildNodeListDict[minimumCost] = []

flag = True

for nodeInfoTupleList in self.getNeighbors(v):

cost = 0

nodeList = []

for c, weight in nodeInfoTupleList:

cost = cost+self.getHeuristicNodeValue(c)+weight

nodeList.append(c)

if flag == True:

minimumCost = cost

costToChildNodeListDict[minimumCost] = nodeList

flag = False

else:

if minimumCost > cost:

minimumCost = cost
costToChildNodeListDict[minimumCost] = nodeList

return minimumCost, costToChildNodeListDict[minimumCost]

def aoStar(self, v, backTracking):

print("HEURISTIC VALUES :", self.H)

print("SOLUTION GRAPH :", self.solutionGraph)

print("PROCESSING NODE :", v)

print("-----------------------------------------------------------------------------------------")

if self.getStatus(v) >= 0:

minimumCost, childNodeList = self.computeMinimumCostChildNodes(v)

print(minimumCost, childNodeList)

self.setHeuristicNodeValue(v, minimumCost)

self.setStatus(v, len(childNodeList))

solved = True

for childNode in childNodeList:

self.parent[childNode] = v

if self.getStatus(childNode) != -1:

solved = solved & False

if solved == True:

self.setStatus(v, -1)

self.solutionGraph[v] = childNodeList

if v != self.start:

self.aoStar(self.parent[v], True)

if backTracking == False:

for childNode in childNodeList:


self.setStatus(childNode, 0)

self.aoStar(childNode, False)

print("Graph - 1")

h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2,

'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1}

graph1 = {

'A': [[('B', 1), ('C', 1)], [('D', 1)]],

'B': [[('G', 1)], [('H', 1)]],

'C': [[('J', 1)]],

'D': [[('E', 1), ('F', 1)]],

'G': [[('I', 1)]]

G1 = Graph(graph1, h1, 'A')

G1.applyAOStar()

G1.printSolution()

Q2) 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. Implement OPT Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
[20Marks]

//OPT

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

SLIP14
Q1) Given an initial state of a 8-puzzle problem and final state to be reached 2 8 3 1 6 4 7 5 1 2 3 8 4 7 6
5 Find the most cost-effective path to reach the final state from initial state using A* Algorithm.
Consider g(n)=Depth of node and h(n)=Number of misplaced tiles [10 Marks] .

#include<stdio.h>

#include<string.h>

#include<unistd.h>

#include<sys/types.h>

#include <sys/stat.h>

#include <stdlib.h>

#include<time.h>

#define r 3

#define c 3

char matrix[r][c];

char new[r][c];

int count;

char final[r][c] = {{'1','2','3'},{'4','5','6'},{'7','8',' '}};

int i,j;

char z ;

int p,q,x,y;

int t =0;

int result = 0;

void load()

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

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

if(new[i][j] == '0')

matrix[i][j]= ' ';

continue;

matrix[i][j]= new[i][j];

void blank()

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

for(j=0;j<3;j++)

new[i][j]= ' ';

int main()

time_t T= time(NULL);
struct tm tm = *localtime(&T);

char f[4];

int rsl ;

int random,t;

int randvalues[9];

main:

count = 0;

blank();

T= time(NULL);

tm = *localtime(&T);

srand(tm.tm_sec);

while(count!=9)

rsl=rand()%9;

sprintf(f,"%d",rsl);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((new[i][j]) == f[0])

i = 4; j = 4;

continue;

}else if((new[i][j]) == ' ')


{

new[i][j] = f[0];

i = 4; j = 4;

count++;

load();

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

while(1)

printf("enter value to change its position to blank space\n");

scanf(" %c",&z);

if(z=='q')

printf("\n*****You Quit*****\n");
goto main;

// break;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((matrix[i][j])== z)

p = i;

q = j;

}else if((matrix[i][j])== ' ')

x = i;

y = j;

t =0;

int m , n ;

m = p - 1;

n=q;

if(m>=0)

{
if((matrix[m][n])== ' ')t=1;

m = p + 1;

if(m<=2)

if((matrix[m][n])== ' ')t=1;

m = p;

n=q-1;

if(n>=0)

if((matrix[m][n])== ' ')t=1;

n=q+1;

if(n<=2)

if((matrix[m][n])== ' ')t=1;

if(t==1)

matrix[x][y] = z;

matrix[p][q] = ' ';

t = 0;
for(i=0;i<r;i++)

for(j=0;j<c;j++)

if((matrix[i][j])== final[i][j])

t++;

system("clear");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("|%c|",matrix[i][j]);

printf("\n");

if(t==9)

printf("\n****you Win****\n");

break;

}
}

return 1;

Q2) 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. Implement FIFO Reference String: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1 [20Marks]

//FIFO

#include <stdio.h>

int main()

int incomingStream[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};

int pageFaults = 0;

int frames = 3;

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");

int temp[frames];

for(m = 0; m < frames; m++)

temp[m] = -1;

for(m = 0; m < pages; m++)

s = 0;

for(n = 0; n < frames; n++)

if(incomingStream[m] == temp[n])

{
s++;

pageFaults--;

pageFaults++;

if((pageFaults <= frames) && (s == 0))

temp[m] = incomingStream[m];

else if(s == 0)

temp[(pageFaults - 1) % frames] = incomingStream[m];

printf("\n");

printf("%d\t\t\t",incomingStream[m]);

for(n = 0; n < frames; n++)

if(temp[n] != -1)

printf(" %d\t\t\t", temp[n]);

else

printf(" - \t\t\t");

printf("\nTotal Page Faults:\t%d\n", pageFaults);

return 0;

SLIP15

Q1) Write a program to illustrate the concept of orphan process (Using fork () and sleep()) [10 Marks]

//FORK() SLEEP()
#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

int main()

int pid = fork();

if(pid>0)

//getpid() returns process id and 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(5);

// As this time parent process has finished, it will show different


//parent 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;

Q2) Write the simulation program for demand paging and show the page scheduling and total number
of page faults according to SECOND CHANCE page replacement algorithm. Assume the memory of 3
frames. Request Page Numbers: 9,14,10, 11, 15, 9, 11, 9, 15, 10, 9, 15, 10, 12, 15 [20Marks]

#include<stdio.h>
int refstring[20],pt[10],u[10],nof,nor;
void accept()
{
int i;
printf("\nEnter the reference string:");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&refstring[i]);
}
}
int search(int s)
{
int i;
for(i=0;i<nof;i++)
if(pt[i]==s)
return(i);
return(-1);
}
void second_chance()
{

int i,j,k,faults=0,sp=0;
for(i=0;i<nor;i++)
{
printf("%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
while(1)
{
if(u[sp]==0)
{
pt[sp]=refstring[i];
u[sp]=1;
sp=(sp+1)%nof;
faults++;
break;
}
else
{
u[sp]=0;
sp=(sp+1)%nof;
}
}
}
for(j=0;j<nof;j++)
{
printf("\n%3d%3d",pt[j],u[j]);
if(j==sp)
printf("*");
}
}
printf("\t");
printf("Total page faults=%d\n",faults);
}
int main()
{
clrscr();
printf("Enter length of reference string:");
scanf("%d",&nor);
printf("Enter the no. of frames:");
scanf("%d",&nof);
accept();
second_chance();
getch();
}

You might also like