+ All Categories
Home > Documents > Os Lab Manual II a(ECM)

Os Lab Manual II a(ECM)

Date post: 27-Oct-2015
Category:
Upload: mahesh-borkar
View: 55 times
Download: 11 times
Share this document with a friend
Description:
ghj
Popular Tags:
53
1 OPERATING SYSTEM PROGRAMMING LAB CODE :11EM208 LAB IIYR BTECH/ IV SEM BRANCH: ECM SECTION : II A EXPNO:1(a) AIM: To write a C program to simulate the shortest job first algorithm with pre- emption. DESCRIPTION: For implementing SJF algorithm with preemption, we consider the arrival times of each process, the burst times of all the previously arrived processes. After the arrival of all the processes into the ready queue, the average waiting time and turn around time can be calculated by using the above algorithm. ALGORITHM: 1) start 2) read the no of processes to n 3) initialize i to 0 4) if i<n repeat the dollowing steps a) read the burst time to a[i] b) if a[i]<0 then write “invalid input” and go to step 12 c) initialize b[i] to 0 d) add a[i] to tt and reassign it to tt 5) read the arrival time of each process into an array. 6) from i=0 to n-1 step 1 repeat the following steps a) if a[i]!=0 while(at[i+1]!=s) i) k=i; ii) from j=0 to i step 1 do if a[i]>a[j] k=j iii)a[k]=a[k-1] iv)increment b[k] v) increment s 7) from i=0 to n do from j=1 to n do if a[i]>a[j] then a) swap a[i] and a[j] b) swap at[i] and at[j] c) swap b[i] and b[j] 8) from i=0 to n do a) w[i]=s-b[i]-at[i] b) s=s+a[i] c) avg=avg+w[i] 9)tt=tt+avg
Transcript
Page 1: Os Lab Manual II a(ECM)

1

OPERATING SYSTEM PROGRAMMING LAB

CODE :11EM208 LAB IIYR BTECH/ IV SEM BRANCH: ECMSECTION : II A

EXPNO:1(a)AIM: To write a C program to simulate the shortest job first algorithm with pre-emption.

DESCRIPTION: For implementing SJF algorithm with preemption, we consider the arrival times of each process, the burst times of all the previously arrived processes. After the arrival of all the processes into the ready queue, the average waiting time and turn around time can be calculated by using the above algorithm.

ALGORITHM:1) start2) read the no of processes to n3) initialize i to 04) if i<n repeat the dollowing steps

a) read the burst time to a[i]b) if a[i]<0 then write “invalid input” and go to step 12c) initialize b[i] to 0d) add a[i] to tt and reassign it to tt

5) read the arrival time of each process into an array.6) from i=0 to n-1 step 1 repeat the following steps a) if a[i]!=0 while(at[i+1]!=s) i) k=i;

ii) from j=0 to i step 1 do if a[i]>a[j] k=j iii)a[k]=a[k-1] iv)increment b[k] v) increment s 7) from i=0 to n do from j=1 to n do if a[i]>a[j] then a) swap a[i] and a[j] b) swap at[i] and at[j] c) swap b[i] and b[j] 8) from i=0 to n do a) w[i]=s-b[i]-at[i] b) s=s+a[i] c) avg=avg+w[i] 9)tt=tt+avg

Page 2: Os Lab Manual II a(ECM)

2

10) tt=tt/n 11) avg=avg/n 12)write the turn around time and average waiting time 13) stop

PROGRAM:

#include<stdio.h>main(){ int a[10],b[10],w[10],at[10],n,s=0,i,j,k,t,t1,t2; float avg=0,tt=0; printf(“enter no. of processes\n”); /* reading no of processes */ scanf(“%d”,&n); printf(“enter burst times of processes\n”); /* reading burst times for processes */ for(i=0;i<n;i++) { scanf(“%d”,&a[i]); if(a[i]<0) { printf(“invalid input”); exit(0); } b[i]=0; tt=tt+a[i]; } printf(“enter arrival times\n”); /* reading arrival times */ for(i=0;i<n;i++) scanf(“%d”,&at[i]); k=0; j=1; for(i=0;i<n-1;i++) { if(a[i]!=0) { while(at[i+1]!=s) { k=i; for(j=0;j<i;j++) { if(a[i]>a[j]) k=j; } a[k]=a[k]-1; b[k]++; s=s+1;

Page 3: Os Lab Manual II a(ECM)

3

} } } for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(a[i]>a[j]) /* swaping */ { t=a[i]; a[i]=a[j]; a[j]=t; t1=at[i]; at[i]=at[j]; at[j]=t1; t2=b[i]; b[i]=b[j]; b[j]=t2; } } } for(i=0;i<n;i++) { w[i]=s-b[i]; s=s+a[i]; } for(i=0;i<n;i++) { w[i]=w[i]-at[i]; avg=avg+w[i]; } tt=tt+avg; /* calculating average time and turn around time */ tt=tt/n; Avg=avg/n; printf(“turn around time is %f”,tt); printf(“avg waiting time is %f”,avg); getch();}

RESULT: The program is executed using SJF preemption scheduling algorithm. The average waiting time and the average turn around time are calculated.

OUTPUT:1) enter the number of processes 4 enter the burst times 8 4 9 5 enter the arrival times 0 1 2 3 turn around time is 13 avg waiting time is 6.52) enter the number of processes 4 enter the burst times 130 540 120 60

Page 4: Os Lab Manual II a(ECM)

4

enter the arrival times of processes 0 5 9 15 turn around time is 148 avg waiting time is 136.253) enter the number of processes 3

enter the burst times 160 -140 invalid input

OBSERVATIONS: Errorf:\cnlab\05_568\SJFPREEM.C:/:function call missing in function main.

CONCLUSION: Hence the program is error free.

Page 5: Os Lab Manual II a(ECM)

5

EXPNO: 1(b)

AIM: To write a program on Shortest Job First algorithm with Non-Preemption

Description: Initially, In the shortest job first algorithm with non-preemption, the sorting of the process can be done based on their burst time in ascending order. The above algorithm calculates the averaging waiting time and turn around time of each process .

ALGORITHM:1) start2) read the number of process into n3) for i=0 to n step1 do

a) read the burst time of process into b[i]b) check if b[i] is less than 0

i. write invalid input ii. goto step 11

c) tt = tt+b[i]4) for i=0 to n do step1

i. for j=1 to n step1 do if b[i]>b[j] t=b[j] b[i]=b[j] b[j]=t5) assign 0 to w[0]6) for i=1 to n do

w[i]=w[i-1]+b[i] avg=avg+w[i]7) tt=tt+avg

8) avg=avg/n 9) tt=tt/n

10) Print the average waiting time and turn around time11) Stop

Page 6: Os Lab Manual II a(ECM)

6

PROGRAM:/********************************************************************

***To implement Shortest Job First (NonPre-Emption)*********************************************************************

**/#include<stdio.h>main(0{ int w[10],b[10],n,I,j,t; float avg=0,tt=0; printf(“\n Enter number of processes”); /*reading processes */ scanf(“%d”,&n) printf(“Enter burst times”); /* reading burst times */ for(i=0;i<n;i++) { scanf(“%d”,&b[i]); if(b[i]<0) { pritnf(“\n Invalid input”); exit(0); } tt=tt+b[i]; } for(i=0;i<n;i++) { for(j=1;j<n;j++) { if(b[i]>b[j]) { t=b[i]; /* swaping */ b[i]=b[j]; b[j]=t; } } } w[0]=0; for(i=1;i<n;i++) { w[i]=w[i-1]+b[i-1]; /* calculating average time */ avg=avg+w[i]; } tt=tt+avg; /* calculating turn around time */ tt=tt/n; avg=avg/n; printf(“\n Turn Aound Tme: %f”,tt); printf(“\n Average Waiting Time:%f”,avg); getch();}

Page 7: Os Lab Manual II a(ECM)

7

/***********************************************************************

End of the Program*********************************************************************

**/

RESULT:In this method the process with the shortest job or the shortest burst time

will be executed first and the average burst time and the turn around time is calculated.

INPUT/OUTPUT:1) Enter number of processes: 3

Enter the burst times: 24 3 3Turn Around Time: 9.75Average Waiting Time: 3

2) Enter number of processes: 2Enter the burst times: 60 70Turn Around Time: 46.5Average Waiting Time: 30

2) Enter number of processes: 3Enter the burst times: -5

Invalid Input

OBSERVATIONS:

Error: statement missing in function mainCorrection: Semicolon is placed at required position.

CONCLUSION:The SJF non-preemption is simulated successfully.

Page 8: Os Lab Manual II a(ECM)

8

Exp No:1(c)

Aim: To write a c program to simulate First Come First Served(FCFS)algorithm using CPU Scheduling.

Description:In FCFS algorithm, which ever the process that enters first in the ready

queue will be allocated CPU first. Initially, the waiting time of the first process is kept zero. In this algorithm, the waiting time of the second process is the burst time of the first process and the waiting time of the third process is the sum of the burst times of the first and the second process and so on . After calculating the waiting times of each process, the average waiting time is calculated.

Algorithm:

1) start2) read the no of processes and also read the burst times of all the processes into an array b.3)assign 0 to avg and 0 to tt4)read the no of processes into n5)read the burst times a)for i=0 ton step1 do i)read b[i] ii)if b[i]<0 1)write invalid input 2)goto step 12 iii)tt=tt+b[i]6)assign 0 t0 a[0]7)for i=1 to n step 1 do i)a[i]=a[i-1]+b[i-1] ii)avg=avg+a[i];8)tt=tt+avg9)tt=tt/n10)avg=avg/n11)write the turn around time and the average waiting time12)stop

Page 9: Os Lab Manual II a(ECM)

9

Program: /**********to implement FCFS**********/#include<stdio.h>

main(){ int a[10],b[10],n,i; float avg=0,tt=0; printf(“enter the no of processes”); /* reading no of processes */ scanf(“%d”,&n); printf(“\nenter the burst times”); /* reading burst times */ for(i=0;i<n;i++) { scanf(“%d”,&b[i]); if(b[i]<0) { printf(“\n invalid input”); exit(0); } tt=tt+b[i]; /* calculating turn around time */

}a[0]=0;for(i=1;i<n;i++) { a[i]=a[i-1]+b[i-1]; avg=avg+a[i]; } tt=tt+avg;tt/n;avg=avg/n;printf(“Turn around time:%f”,tt);printf(“\nAvg.waiting time:%f”,avg);getch();

}

Result: In this technique the process which comes first into the ready queue will be executed first. If the process that comes later will have to wait till the execution of the previous process is over. The average waiting time and the turnaround time is calculated.

Output:1)enter the no of processes 3

enter the burst time 24 3 3 The average waiting time is:17.0 The turn around time is:27.0 2) enter the no of processes 2 enter the burst time 540 450 The average waiting time is:270.0

Page 10: Os Lab Manual II a(ECM)

10

The turn around time is:765.0 3) enter the no of processes 6 enter the burst time -5 Invalid input

Observation: Error:Statement missing in function main Correction:The : is placed after the statement

Conclusion: The FCFS algorithm is successfully implemented.

Page 11: Os Lab Manual II a(ECM)

11

EXPNO: 1(d)AIM: To write a c program to simulate the cpu scheduling priority algorithm.DESCRIPTION: To calculate the average waiting time in the priority algorithm, sort the burst times according to their priorities and then calculate the average waiting time of the processes. The waiting time of each process is obtained by summing up the burst times of all the previous processes.ALGORITHM:1.Start.2. read the no.of processes to n.3. for i=0 to n do step 1 a. read burst times into bt[i]. b.if bt[i]<0, i. write invalid input. ii. goto step 13. c. tt=t+b[i].4. for i=0 to n step 1 do Read the priorities to p[i].

1. sort the priorities of the processes accordingly.Sort all the burst times.

6.asign 0 to w[0].7. assign 0 to s.8. for i=1 to n do a.s=s+bt[i-1]. b.w[i]=s. c. avg=avg+w[i].9. tt=tt+avg10. tt=tt/n.11 avg=avg/n12. write the turn around time and the average waiting time.13 Stop.

PROGRAM:#include<stdio.h>main(){ int bt[10],w[10],p[10],I,j,n,s,t,t1; float avg=0,tt=0. clrscr(); printf(“enter no.of processes”); /* reading processes */ scanf(“%d”,&n); printf(“enter the burst times”); /* reading burst times */ for(i=0;i<n;i++) { scanf(“%d”,&bt[i]); if(bt[i]<0) { printf(“\n INVALID DATA”); exit(0);

Page 12: Os Lab Manual II a(ECM)

12

} tt=tt+bt[i]; } printf(“enter the priorities”); /* reading priorities */ for(i=0;i<n;i++) scanf(“%d”,&p[i]); for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(p[i]>p[j]) { t=bt[i]; /* swaping */ bt[i]=bt[j]; bt[j]=t; t1=p[i]; p[i]=p[j]; p[j]=t1; } } } w[0]=0; s=0; for(i=1;i<n;i++) { s=s+bt[i-1]; w[i]=s; avg=avg+w[i]; } tt=tt+avg; /* calculating turn around time and acerage time */ tt=tt/n; avg=avg/n; printf(“\n turn around time is :%f”,tt); printf(“\n average waiting time is :%f”,avg); getch(); }

RESULT: In this method the process with the highest priority will be executed first and if the burst times of the process are more than the average waiting time is more and if they are less than the waiting time is also less.

INPUT/OUTPUT:1. enter number of process 4

enter the burst time 12 7 72 18Enter the priorities 3 4 1 2Turn around time 95.0Average waiting time 66.0

Page 13: Os Lab Manual II a(ECM)

13

2. enter number of process 4enter the burst time 100 240 370 800Enter the priorities 700 800 300 100Turn around time 1350.0

Average waiting time 810 3. enter number of process 4

enter the burst time -3 INVALID INPUT.

OBSERVATIONS: Error : undefined symbol t1 in function main

CONCLUSION: The priority cpu scheduling algorithm is implemented.

Viva:-

1) which among the scheduling algorithms is efficient?Ans) round robin scheduling algorithm

2) What is average turn around time?Ans) measure of average service provided to jobs

3) what is throughput?Ans) measure of system’s performances

Page 14: Os Lab Manual II a(ECM)

14

EXPERIMENT NO:2(a)

AIM: To implement a file allocation method using contiguous method,

DESCRIPTION: This is the most common form of file structure. In this type of fileorganization, a fixed format is used for records. All records (of the system) have the same length, consisting of the same number of fixed length fields in a particular order. Because the length and position of each field are known, only the values of fields need to be stored. The field name and length for each field are attributes of the file . One particular field, usually the first field in each record, is reffered to as the ‘key field’ and the key field uniquely identifies the record.ALGORITHM:1. Start.2. Print main menu 1.insertion 2.deletion 3.retrieval 4.exit.3. Read the choice to ch.4. Repeat the below steps while true:5. if ch is 1 then do the following: a) read the file name to p[m].name. b)read the length to p[m].len. c)while true do the following:

assign random(q) to no if b[no]=0 then for j=no+1 incrementing it in steps of 1 do the following till j<no+p[m].len if b[j]=1 then break.

If j=no+p[m].len then break.d)p[m].start=no.e)for i=no incrementing it in steps of 1 assign b[i]=1 until i<no+p[m].f)increment m by 1.g) print p[i].name ,p[i].start,p[i].len.

5. if ch is 2 then do the following a) read the file name you want to delete. b)for i=0 to n do the following incrementing in steps of 1 if the input given and p[i].name are same then do: assign b[j]=0 copy null to p[i].name assign p[i].start=-1 assign p[i]=-1 print “deleted successfully”

c)for i=0 to n print p[i].name, p[i].start, p[i].lenand break.6. if ch is 3 then do the following: a)read the file name you want to retrieve. b)if the given input and p[i].name are same then print p[i].start. c)break.7. if ch is 4 exit(0) otherwise print INVALID CHOICE.8. Stop.

Page 15: Os Lab Manual II a(ECM)

15

PROGRAM:/*File allocation using contiguous method*/#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>

int q=100,b[100],m;main (){

int ch,I,j,no;char fnm[20],tnm[20]=”null”;struct FAT /* declaration of structure*/ {

char name[20];int start;int len;

}p[20];clrscr( );m=0;printf(“\n SIMULATION OF FILE ALLOCATION METHODS\N\N”); do /* reading choices */{ printf(“\n\n Main Menu\n\n\t1.insertion \t2.deletion\n\t3.retrieval\n\t4.exit

\n\nEnter your choice:”); scanf(“%d”,&ch);

switch(ch){ case 1:printf(“\n enter file name”);

scanf(“%d”,&(p[m].len));while(1){

no=random(q); if(b[no]==0) { for(j=no+1;j<no+p[m].len;j++) if(b[j]==1) break;

} if(j==no+p[m].len) break; }

p[m].start=no; for(i=no;i<no+pm.len;i++) b[i]=1; printf(“page table”);

m++;for(i=0;i<m;i++) /* printing file name*/{

Page 16: Os Lab Manual II a(ECM)

16

printf(“\n%s\t%d\t%d”,p[i].name,p[i].start,p[i].len); printf(“\n”);}break;

case2:printf(“enter file name you want to delete”); /* deleting file*/

scanf(“%s”,fnm);for(i=0;i<m;i++){ if(strcmp(p[i].name,fnm)==0) { for(j=p[i].start ;j<(p[i].len+p[i].start);j++) b[j]=0; strcpy(p[i].name,”null”);

p[i].start=-1; p[i].len=-1; printf(“%s deleted successfully”,fnm); break;}

}for(i=0;i<m;i++){ printf(“%s”,p[i].name); printf(“%d\t%d\t”,p[i].start,p[i].len); printf(“\n”);}

case 3:printf(“enter file name you want to retrieve”); /* file retrieving */

scanf(“%s”,fnm);printf(“\nblocks allocated are \n”);for(i=0;i<m;i++){ if(strcmp(p[i].name,fnm))==0)

{for(j=p[i].start;j<(p[i].start+p[i].len);j++) printf(“%d\t”,j);break;

}}break;

case 4:exit(0);default:printf(“INVALID CHOICE”);}

}while(1); }

Page 17: Os Lab Manual II a(ECM)

17

INPUT/OUTPUT:SIMULATION OF FILE ALLOCATION METHODSMain Menu1.insertion2.deletion3.retrieval4.exitenter your choice :1enter file name:qenter length(in kb):12page tableqq 46 12Main Menu1.insertion 2.deletion 3.retrieval4.exitenter your choice :1enter file name:wwenter length(in kb):3page tableqq 46 12ww 30 3Main Menu1.insertion2.deletion3.retrieval4.exitenter your choice :3enter file nameyou want to retrieve wwblocks allocated are30 31 32Main Menu1.insertion2.deletion3.retrieval4.exitenter your choice :3enter file name you want to delete wwww is deleted successfullyqq 46 12null -1 -1Main Menu1.insertion2.deletion3.retrieval4.exitenter your choice:4

Page 18: Os Lab Manual II a(ECM)

18

OBSERVATIONS:Error:function pointer value error at p[m].startCorrection:Missed for p[m]and start

CONCLUSION:The program is correct since thye output is error free.

Page 19: Os Lab Manual II a(ECM)

19

Exp No:2(b)

Aim:To implement file allocation method using linked list method

Description: In the chained method file allocation table, it contains a field which points to starting block of memory. From it for each block, a pointer is kept to next successive block. Using this method, there is no external fragmentation.

Algorithm: Struct define FAT character type array name integer type len,start,arr[10]

1)start 2)write 1.create 2.delete data from file 3.retrive file 4.exit 3)read the choice ch 4) i)if choice is 1 a)increment m b)read the file name c)read the length of the file d)assign 0 to l e)for j :=0 to p[m].len 1)rn=random(q) 2)if b[rn]==0 then i)p[m].arr is assigned rn ii)increment l iii)assign 1 to b[rn] iv) goto to f f)assign p[m].start to p[m].arr[0] g)write the page table h)write the name start length i)for i=0 tom do 1)write p[i].name,p[i].start,p[i].len j)go to step ii ii)if ch==2 then a)read the file name that you want to delete b)for i =0 to m do 1)if strcmp(p[i].name,fnm)==0) i)for j=0 to p[i].len a)assign p[i].arr[j] to v b)assign 0 to b[v] c)assign 0 to p[i].arr

ii)assign -1 to p[i].len iii)assign -1 to p[i].start 2)copy the string NULL to p[i].name c)write the page table for i=0 to m

Page 20: Os Lab Manual II a(ECM)

20

d)goto step iii iii)if choice is 3 then a)read the name of the file that you want to retrive b)for i=0 to do 1) if strcmp(p[i].name,fnm)is equal to 0 i)for j=0 to p[i].len a)write p[i].arr[j] c)goto step iv iv)if choice is 4 then exit 7)stop

Program:

/********file allocation using chained method********/#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>Main(){ Struct FAT /* declaration of structure */ { char name[5]; int len,start,arr[10]; }p[20]; int ch,l,I,j,rn,v; char fnm[5]; int b[100]; clrscr(); for(i=0;i<100;i++) b[i]=0; while(1) { Printf(“ 1.create 2.delete data from file 3.retrive file 4.exit “); /* reading choices */ Scanf(“%d”,& ch); switch(ch) { case 1: m++; printf(“enter the file name”); scanf(“%s”,&p[m].name); printf(“enter the length of the file”); scanf(“%d”,&p[m].len); l=0; for(j=0;j<p[m].len;j++) { Do { rn=random(q);

Page 21: Os Lab Manual II a(ECM)

21

if(b[rn]==0) { p[m].arr = rn l++; b[rn]=1; break; } }while(b[rn]==1); p[m].start=p[m].arr[0] printf(“ page table is”); printf(“name start length”); for(i=0;i<m;i++) printf(“\n%s %d %d”, p[i].name,p[i].start,p[i].len); break; case 2: printf(“enter the file name that you want to delete”); /* deleting file*/ for(i =0; i<m;i++) { If(strcmp(p[i].name,fnm)==0)) { For(j=0;j< p[i].len;j++) { v=p[i].arr[j] ; b[v]=0; p[i].arr=0; } p[i].len=-1; p[i].start=-1; strcmp( p[i].name,NULL); } } printf(“the page tableis :\n”); for(i=0;i<=m;i++) { printf(“%s”,p[i].name); printf(“%d %d”,p[i].start,p[i].len); } break; case 3: printf(“enter the name of the file that you want to retrive”); /*file retrieving*/ scanf(“%s”,&fnm); for(i=0; i<=m;i++) { if(strcmp(p[i].name,fnm)==0) { for (j=0;j<=p[i].len;j++) printf(“%d\t”,p[i].arr[j]); } } break;

Page 22: Os Lab Manual II a(ECM)

22

case 4:exit(0); default:printf(“invalid”); } } getch(); }

Input/Output:

Enter the choice: 1.create 2.delete data from file 3.retrive file 4.exit 1 Enter the file name:fdhgf Enter the length of file:12 Page table is Name start length fdfgf 46 12

Enter the choice: 1.create 2.delete data from file 3.retrive file 4.exit 1 Enter the file name:kjkl Enter the length of file:10 Page table is Name start length fdfgf 46 12 kjkl 71 10 Enter the choice: 1.create 2.delete data from file 3.retrive file 4.exit 3 enter the name of the file that you want to retrive kjkl 71 79 60 12 21 63 47 19 41 Enter the choice: 1.create 2.delete data from file 3.retrive file 4.exit 2 Enter the file you want to delete kjkl Page table is Name start length

Page 23: Os Lab Manual II a(ECM)

23

fdfgf 46 12 NULL -1 -1 Enter the choice: 1.create 2.delete data from file 3.retrive file 4.exit 4

Result: The chained algorithm is simulated and the file allocation is done using the filesObservations: Error: There was an error in the random function Correction:The function was declaredConclusion: The output is correct and hence the program is error free.

Viva questions:1) what are different file allocation techniques?Ans) contigous allocation, indexed allocation, chained allocation, etc.

2) what is simplest file allocation method?Ans) contigous file allocation method

Page 24: Os Lab Manual II a(ECM)

24

EXPNO: 3(a)

AIM: To implement and simulate the Memory Fragmentation Table algorithm.

DESCRIPTION: In this method, the memory is divided into parts and process is fit into it. The process which is best suited is put into appropriate memory block by checking the memory partitions. If it suits, its status should be changed.

ALGORITHM:1) start2) read the total memory space3) read the choice among equal partition and unequal partition.4) If choice is 1 then perform the following steps else go to step 5

a) read the size of each blockb) assign tms/s to nbc) initialize i to 0 d) if i<nb do sz[i][0]=s;

5) Read the no. of blocks and size of each block.6) Read no. of jobs7) Enter jobid and jobsize8) Initialize frag to 09) For all jobs assign 9999 to t and jobs[i][1] to element10) For each job repeat the following steps

a)if sz[j][0]>=element and sz[i][0]<=t then if sz[j][1]!=1 then t=sz[j][0] index=j b)if sz[index][1]!=1 then sz[index][1]=1 jobs[i][2]=2 frag=frag+(t-element) increment count

11) write the total internal fragmantation frag12) write the no. of free blocks to nb-count13) write the jobs which are not allocated if count==nj then write 0 for all jobs if jobs[i][2]!=2 then write jobs[i][0] and jobs[i][j]14) stop

Page 25: Os Lab Manual II a(ECM)

25

PROGRAM:

/*TO IMPLEMENT MFT*/#include<stduio.h>#include<conio.h>main(){ int tms,element,nb,I,j,t,index,frag,ch,count=0; static int sz[20][2],nj,s; printf(“enter total memory space”); /* reading memory */ scanf(“%d”,&tms); /* reading choices */ printf(“enter choice\n1.equal partition 2.unequal partition\n”); scanf(“%d”,&ch); if(ch==1) { printf(“enter size of each block”); /* reading size of block */ scanf(“%d”,&s); nb=tms/s; for(i=0;i<nb;i++) scanf(“%d”,&sz[i][0]); } else { printf(“enter no. of blocks”); scanf(“%d”,&nb); printf(“enter size of %d blocks”); for(i=0;i<nb;i++) scanf(“%d”,sz[i][0]); } printf(“enter no. of jobs”); /* reading no of jobs */ scanf(“%d”,&nj); printf(“enter job information 1.jobid 2.job size\n”); for(i=0;i<nj;i++) scanf(“%d%d”,&jobs[i][0],&jobs[i][1]); frag=0; for(j=0;j<nj;j++) { if(sz[j][0]>=element && sz[i][0]<=t) { if(sz[j][1]!=1) { t=sz[j][0]; index=j; } } } if(sz[index][1]!=1) { sz[index][1]=1; jobs[i][2]=2;

Page 26: Os Lab Manual II a(ECM)

26

frag=frag+(t-element); count++; } } printf(“total internal fragmentation : %d”,frag); printf(“no. of free blocks: %d”,nb-count); printf(“the jobs which are not allocated”); if(count==nj) printf(“0”); for(i=0;i<nj;i++) { if(jobs[i][2]!=2) printf(“jobid ------%d\tjob size-----%d\n”,jobs[i][0],jobs[i][1]); } getch();}

RESULT: The MFT algorithm is implemented and simulated.

INPUT/OUTPUT: Enter total memory space: 100

enter choice 1. equal partition 2. unequal parttion 2enter no. of blocks: 3enter size of 3 blocks: 50 25 25enter no. of jobs 4enter job information 1.jodid 2. jobsize1 252 303 264 205 25total internal fragmentation : 9no. of free blocks : 0the jobs which are noit allocated :job id----4 jobsize----20jobid----5 jobsize----25

OBSERVATIONS:error: statement ; missing in function maincorrection: semicolon is placed in required position

CONCLUSION: Hence the program is error free.

Page 27: Os Lab Manual II a(ECM)

27

EXPNO: 3(b)AIM: To write a program to simulate the MVT algorithmDescription:

To calculate the MVT to manage the arrangement of the process in the memory. The memory is divided into parts and process is fit into it. The process which is best suited into it is placed into the particular memory. We have to check the memory partition if it suits, its states should be changed.ALGORITHM:

1) start2) read the time3) read the number of jobs4) write a)jobid b)jobsize5) read jobs[i][0] and jobs[i][1]6) for i=0 to nj if tms>=jobs[i][1] then tms=tms-jobs[i][1] increament nb assign 1 to flag[i]7) write total memory space available which is not allocated tms8) write jobs which are not allocated

for i=0 to nj do if flag[i] = = 0 then write jobs[i][0] and jobs[i][1]

9) if nb!=nj then write jobid to deallocate for i=0 to nj do if jobs[i][0] = = k then if(flag[i]==1) then tms=tms+jobs[i][1] flag[i]=2 write deallocated job jobs[i][0],jobs[i][1]10) for i=0 to nj do if tms>=jobs[i][1] then if flag[i] = = 0 then tms=tms-jobs[i][1] assign 1 to flag[i]11) write remaining memory in tms12) write jobs which are not allocated are for i=0 to nj step1 do if flag[i] = =0 then write jobs[i][0],jobs[i][1]13) read whether any jab has to be deallocate14) if choice isequal to 2 goto step 15 else goto step 915) Stop

Page 28: Os Lab Manual II a(ECM)

28

PROGRAM:/*****************************************************************

***To simulate MVT algorithm********************************************************************/#include<stdio.h>#include<conio.h>main{ static int jobs[20[2],flag[10]; int ch; static int i,k,nj,nb,tms; clrscr(); printf(“Enter time”); /* reading time */ scanf(“%d”,&tms); printf(“Enter no. of jobs”); /* reading no of jobs */ scanf(“%d”,&nj); printf(“Enter job information 1.jobid 2.jobsize”); for(i=0;i<nj;i++) scanf(“%d%d”,&jobs[i][0],&jobs[i][1]); for(i=0;i<nj;i++) { if(tms>=jobs[i][1]) { tms=tms-jobs[i][1]; nb=nb+1; flag[i]=1; } } printf(“Total memory space available which is not allocated is:%d\n”,tms); printf(“Jobs which are not allocated:”);

for(i=0;i<nj;i++) if(flag[i] = = 0) printf(“%d\t%d\n”,jobs[i][0],jobs[i][1]);

if(nb!=nj) {

while(1) { printf(“enter jobid to deallocate:”);

scanf(“%d”,&k); for(i=0;i<nj;i++) { if(jobs[i][0] = = k) { if(flag[i] = =1) { tms=tms+jobs[i][1]; flag[i]=2;

Page 29: Os Lab Manual II a(ECM)

29

printf(“Deallocated job %d\t%d\n”, jobs[i][0],jobs[i][1]);

} } }

for(i=0;i<nj;i++) { if (tms>=jobs[i][1]) { if(flag[i] = = 0) { tms=tms-jobs[i][1]; flag[i]=1; } } }

printf(“Remaining memory is: %d”,tms); printf(“Jobs which are not allocated are:”);

for( i=0;i<nj;i++) /* dellocating mamory*/ if(flag[i] = =0) printf(“%d\t%d\n”, jobs[i][0],jobs[i][1]); printf(“Do you want to deallocate 1.Yes 2.No”); scanf(“%d”,&ch); if(ch = =2) break;

} }printf(“Allocated jobs are:”); for(i=0;i<nj;i++) if(flag[i]==1) printf(“%d\t%d\n”,jobs[i][0],jobs[i][1]);getch();}

/***********************************************************************End of the program

***********************************************************************/

Result:In this the memory is divided into partitions and the jobs which are

having enough partition are stored. The partitions which are not having enough memory are remained same and when there is any job deallocated it is assigned to new job.

Page 30: Os Lab Manual II a(ECM)

30

INPUT/OUTPUT:1) Enter time: 100 Enter no. of jobs: 5

Enter job information 1.jobid 2.jobsize 1 202 253 154 305 15

Total memory space available which is not allocated is: 10 Jobs which are not allocated: 5 15 Enter jobid to deallocate: 1 Deallocated job: 1 20 Remaining memory is: 15 Jobs which are not allocated are: Do you want to deallocate 1.Yes 2.No : 1 Enter jobid to deallocate: 4 Deallocated job: 4 30 Remaining memory is 45 Jobs which are not allocated are: Do you want to deallocate 1.Yes 2.No : 2 Allocated jobs are

2 253 15

5 15

2) Enter time: 100 Enter no. of jobs: 4

Enter job information 1.jobid 2.jobsize 1 252 303 404 25

Total memory space available which is not allocated is: 10 Jobs which are not allocated: 4 25 Enter jobid to deallocate: 3 Deallocated job: 3 40 Remaining memory is: 20 Jobs which are not allocated are: Do you want to deallocate 1.Yes 2.No : 2 Allocated jobs are

1 252 303 25

Page 31: Os Lab Manual II a(ECM)

31

OBSERVATIONS:Error: Undefined symbol k in function mainCorrection: k is declared in function main

CONCLUSION: The MVT algorithm is simulated successfully.

VIVA QUESTIONS:1) which is best among MFT & MVT?Ans) MFT

2)what is swapping?Ans) it is a technique of temporarily removing inactive programs from the memory of the computer system.

3)what is time slicing?Ans) largest amount of CPU time any program can consume and scheduled to execute on the CPU.

Page 32: Os Lab Manual II a(ECM)

32

EXPNO: 4

AIM: To implement Bankers algorithm for Deadlock Detection

DESCRIPTION: In this, the program implements an algorithm through which a process the leads to a dead lock is found. In the dead lock detection, the dead lock will be detected. there will be a temporary matrix which will check the request matrix and the allocation matrix and find out which process leads to dead lock.

ALGORITHM:1) start2) read no. of processes ‘np’, no. of resources ‘nr’, request matrix ‘request’,

allocation matrix ‘allocation’, available matrix ‘available’.3) for all the processes, initialize flag to 04) initialize i to 05) while i<np repeat the following steps

a) initialize count to 0b) from j=1 to nr repeat the following steps i)if allocation[i][j]=0 then increment count by 1c) if count=nr, then make flag[i]=1

6) while p<=np repeat the following stepsa) from i=1 to np do

if flag[i]=0 theni) assign 0 to countii)from j=1 to nr if available[j]>=request[i][j] then increment count by 1iii) if count=nr then for j=1 to nr do available[j]+=allocatio[i][j]

b)increment p by 1 7) for all the processes repeat the following steps if flag[i]=0 then write “process p[i] leads to dead lock else write “ process p[i] does not lead to dead lock”

8) stop

Page 33: Os Lab Manual II a(ECM)

33

PROGRAM:

/*DEADLOCK DETECTION*/

#include<stdio.h>#include<conio.h>void main(){ int request[10][10],allocation[10][10],available[10]; int np,nr,i,j,count=0,flag[10],p=0; printf(“DEADLOCK DETECTION\n”); printf(“enter no. of processes\n”); /*reading processes and resources*/ scanf(“%d”,&np); printf(“enter no. of resources\n”); scanf(“%d”,&nr); printf(“enter the request matrix\n”); for(i=0;i<np;i++) { for(j=0;j<nr;j++) { scanf(“%d”,&request[i][j]); } flag[i]=0; } printf(“enter the allocation matrix”);/*reading allocation matrix*/ for(i=0;i<np;i++) for(j=0;j<nr;j++) scanf(“%d”,&allocation[i][j]); printf(“enter the available matrix\n”); for(j=1;j<=nr;j++) scanf(“%d”,&available[j]); for(i=0;i<np;i++) { count=0; for(j=0;j<nr;j++) { if(allocation[i][j]=0) count++; } if(coun==nr) flag[i]=1; } while(p<=np) { for(i=0;i<np;i++) { if(flag[i]=0) { count=0;

Page 34: Os Lab Manual II a(ECM)

34

for(j=0;j<nr;j++) { if(available[j]>=request[i][j]) /* if available is greater than request */ count++; } if(count==nr) { for(j=0;j<nr;j++) available[j]+=allocatio[i][j]; flag[i]=1; } } } p++; } for(i=0;i<np;i++) { if(flag[i]=0) printf(“p[%d] leads to deadlock state”,i); else printf(“p[%d] does not create deadlock”); } getch();

}

RESULT: Hence the banker’s safe state algorithm for deadlock detection is implemented.

INPUT/OUTPUT:

1) DEADLOCK DETECTION enter the no. of processes 4 enter the no. of resources 4 enter the request matrix

0 1 0 0 10 0 1 0 10 0 0 0 11 0 1 0 1

enter the allocation matrix 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 enter the available matrix 0 0 0 0 1 p[1] is in deadlock state p[2] is in deadlock state p[3] does not create deadlock state p[4] does not create deadlock state

Page 35: Os Lab Manual II a(ECM)

35

2) DEADLOCK DETECTIONenter no. of processes 4enter no. of resources 5enter request matrix 0 0 0 1 1 0 0 1 0 1 0 0 0 0 1

1 0 1 0 1 enter allocation matrix 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 enter available matrix 1 0 1 0 1 p[1] does not create deadlock p[2] does not create deadlock p[3] does not create deadlock p[4] does not create deadlock

OBSERVATINS:error: statement ; missing in function maincorrection: missing semicolon is placed

CONCLUSION: Hence the program is error free.

VIVA QUESTIONS:

1) What are foue necessary conditions for dead lock?

Ans) mutual exclusion Non preemption Hold and wait Circular wait

Page 36: Os Lab Manual II a(ECM)

36

EXP NO:5AIM: To implement the program for the deadlock avoidance.DESCRIPTION: This program is about to implement the Banker’s algorithm for the prevention of the deadlock. The sequence in which the process are to be executed for the avoidance of the deadlock is found by finding the safe sequence of executing the processes. By finding the safe sequence, the execution of process is done and there will be no deadlock for the process.ALGORITHM:

1. Start2. Read the number of processes to n.3. read the number of resources to r.4. read the allocation matrix to alloc.5. read the max matrix to max6. read the available matrix to avail7. for each I and j from 0to n do need[i][j] as difference of max[i][j] and all[i][j]8. repeat

a. for each I from 0 to n and initialize flag to 0.i. if finish[i] is equal to zero. 1. for each j from 0 to r step 1 do if need[i] [j] is less than or equal to avail[j] then

continue goto 1 else break goto 2 2. if j is equal to r for j from 0 to r step 1 do avail[j]+=all[i][j] assign f as 1 ss[p++]=i+1 increment e by 1. assign 1 to finish[i]. b. if f is equal to 0 or e is equal to n then, goto 99.if f is equal to 0 then write the system is in deadlock. Else Write the safe sequence which in ss.10.Stop.

Page 37: Os Lab Manual II a(ECM)

37

PROGRAM: #include<stdio.h> main() { int alloc[10][10],max[10][10],need[10][10],avail[10]; int work[10],finish[10]={0},i,n,j,r,c=0,p=0,ss[10],f; printf(“enter no of processes”); scanf(“%d”,&r); printf(“enter the allocation matrix”); for(i=0;i<n;i++) for(j=0;j<r;j++) scanf(“%d”,&alloc[i][j]); /*reading max matrix*/ printf(enter max matrix”); for(i=0;i<n;i++) for(j=0;j<r;j++) scanf(“%d”,&max[i][j]); printf(“enter available matrix”); for(j=0;j<r;j++) scanf(“%d”,&avail[j]); for(i=0;i<n;i++) for(j=0;j<r;j++) need[i][j]=max[i][j];/*assign max matrix to need */ while(1) { for(f=0,i=0;i<n;i++) { if(finish[i]==0) continue; else break; } if(j==r) { for(j=0;j<r;j++) avail[j[+=alloc[i][j]; f=1; finish[i]=1; ss[p++]=i+1; c++; } } } if(f==0) break; } if(f==0 ||c==n) break; else { printf(“safe sequence is:”);

Page 38: Os Lab Manual II a(ECM)

38

for(i=0;i<n;i++) /*printing safe sequence*/ printf(“%d”,ss[i]); } getch(); } INPUT/OUTPUT: 1. Enter no.of processes 5 Enter no.of resources 3 Enter allocation matrix 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Enter max matrix 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 Enter available matrix 3 3 2 The safe sequence is :2 4 5 1 3

2. Enter no.of processes 3 Enter no.of resources 3 Enter allocation matrix 1 0 0 6 1 2 2 1 1 Enter max matrix 3 2 2 6 1 3 3 1 4 Enter available matrix 0 1 1 The safe sequence is : 2 3 1

3. Enter no.of processes 2 Enter no.of resources 2 Enter allocation matrix 0 1 2 0 Enter max matrix 7 5

Page 39: Os Lab Manual II a(ECM)

39

3 2 Enter available matrix 3 3 The system is in DEADLOCK STATE.

RESULT: In this program using the bankers algorithm the safe sequence of processes are found out which will avoid the deadlock.

OBSERVATION: Error: function call missing ) in function main

CONCLUSION: The deadlock xcan be prevented by finding the swquence from the program.VIVA QUESTIONS:

1) what are the essential conditions for chance of occurance of deadlock?

Ans) 1. mutual exclusion 2.non-pre-emption 3.hold & wait2) what is mutual exclusion?Ans) resources cannot be shared.

Page 40: Os Lab Manual II a(ECM)

40

EXPERIMENT NO:6AIM:To simulate Bankers algorithm for deadlock prevention.DESCRIPTION: An indirect method of deadlock prevention is to prevent the occurrence mutual exclusion. Through no preemption and hold and wait. A direct method of deadlock preventionis the occurrence of a circular wait.

ALGORITHM:1. Start.2. Read the number of processes and resources to p and r.3. Read the allocation matrix.4. Read the maximum matrix.5. Read the available matrix.6. Assign need[i][j]=max[i][j]-alloc[i][j].7. Call the function fun( ).8. if flag=0 then do the following

if finish[i]is not equal to 1 then for j=0 to r check whether avail[i]<need[j].if true then assign need[i][j] to avail[j].

call fun() function now check for no preemption condition: for j=0 to rdo the

following if avail[i]<need[i][j]then assign avail[j]=need[i][j] assign alloc[i][j]=0. call fun( ) function. check for hold and wait condition :for j=0 to r do the following if avail[j]<need[i][j] then assign avail[j]=need[i][j]

call fun( ) function.9.Stop.

FUNCTION DEFINITION:fun( )-1.repeat the following steps while true i)initializing flag to 0 i to 0 repeat the following steps until i<p

if finish[i]=0 then do the following r times: if need[i][j]<=avail[j] then continue otherwise break if j=r then assign avail[j]=avail[j]+alloc[i][j] assign flag=1 assign finish[i]=1 if flag=0then break.

2. Stop.

Page 41: Os Lab Manual II a(ECM)

41

PROGRAM:/*To implement dead lock prevention*/#include<stdio.h>#include<conio.h>int max[10][10],alloc[10][10],need[10][10],avail[10],I,j,p,r,finish[10]={0},flag=0;main( ){ clrscr( );

printf(“\n\nSIMULATION OF DEADLOCK PREVENTION”);printf(“Enter no. of processes, resources”);scanf(“%d%d”,&p,&r);printf(“Enter allocation matrix”);for(i=0;i<p;i++) for(j=0;j<r;j++) scanf(“%d”.&alloc[i][j]);printf(“enter max matrix”);for(i=0;i<p;i++) /*reading the maximum matrix and availale matrix*/ for(j=0;j<r;j++) scanf(%d”,&max[i][j]);printf(“enter available matrix”);for(i=0;i<r;i++) scanf(“%d”,&avail[i]);for(i=0;i<p;i++) for(j=0;j<r;j++) need[i][j]=max[i][j]-alloc[i][j];fun(); /*calling function*/if(flag==0){ if(finish[i]!=1) { printf(“\n\n Failing :Mutual exclusion”);

for(j=0;j<r;j++){ /*checking for mutual exclusion*/ if(avail[j]<need[i][j]) avail[j]=need[i][j];}fun();printf(“\n By allocating required resources to process %d dead lock is prevented “,i);printf(“\n\n lack of preemption”);for(j=0;j<r;j++){ if(avail[j]<need[i][j]) avail[j]=need[i][j]; alloc[i][j]=0;}fun( );

Page 42: Os Lab Manual II a(ECM)

42

printf(“\n\n daed lock is prevented by allocating needed resources”);printf(“ \n \n failing:Hold and Wait condition “);for(j=0;j<r;j++){ /*checking hold and wait condition*/ if(avail[j]<need[i][j]) avail[j]=need[i][j];}fun( );printf(“\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVEMT DEADLOCK”);}

} getch( );

}fun( ){ while(1) { for(flag=0,i=0;i<p;i++) { if(finish[i]==0) { for(j=0;j<r;j++)

{if(need[i][j]<=avail[j])

continue;else

break; } if(j==r) { for(j=0;j<r;j++)

avail[j]+=alloc[i][j]; flag=1; finish[i]=1;}

} } if(flag==0) break; }}

Page 43: Os Lab Manual II a(ECM)

43

RESULT:The process which are causing the deadlock are prevented.

INPUT/OUTPUT:

SIMULATION OF DEADLOCK PREVENTION

Enter no. of processes,resources 3,2enter allocation matrix 2 4 5

3 4 5 Enter max matrix4

3 4 5 6

1Enter available matrix2

5Failing:Mutual Exclusion

by allocating required resources to process dead is preventedLack of no preemption

dead lock is prevented by allocating neede resources

Failing:Hold and Wait conditionBY AVOIDING ONE OF THE ABOVE CONDITION, YOU CAN PREVENT DEADLOCK

OBSERVATIONS:Error:finish[] cannot be accessed within the functon.Correction:finish should be declared globally before main function. CONCLUSION:The output is correct and hence the program is error free.

VIVA QUESTIONS:1) what is binary semaphore?Ans) it takes 0’s and 1’s only.

Page 44: Os Lab Manual II a(ECM)

44

EXP NO: 7(a)

AIM: To implement page replacement algorithm FIFO.

DESCREPTION: The First-In-First-Out policy treats the page frames allocated to a process as a circular buffer and pages are removed in round robin style. All that is required is a pointer that circles through the page frames of the process. This is therefore, one of the simplest page replacement policies to implement the logic behind this choice, other than its simplicity is that one is replacing the page that has been in memory the largest. A page fetched into memory a long time ago may have now fallen ou t of use.

ALGORITHM: 1. Start 2. read the no.of frames. 3. read the page reference string of length prscount to prs 4. for i=0 to prscount do Write the pages present in frames as for j 0 to n Write frames[j] Write page fault as k. If page_found(prs[i]) is equal to 1 then, Assign 0 to k and continue. fff= findfreeframes() If fff>=0 then frames[fff]=prs[i] else frames[t]=prs[1] t=(t+1)%n. Assign 1 to k. Increment pfc6. write pages present in frames as for j, 0 to n do Write page faults as k. Write total page faults as pfc.7.Stop.

Page_found(int a) 1. start 2. for i=0 to n do If frames[i]==a then, return 1. return 0. 3. Stop.

Findfreeframe() 1. start. 2. for i=0 to n do If frames[i]==0 then, return i. return -1.

Page 45: Os Lab Manual II a(ECM)

45

3.Stop.

PROGRAM: #include<stdio.h> #include<conio.h> int prs[20],frames[20]; int n; main() { int page_found(int a); int findfreeframe(void); static int prscount,t,fff,pfc; int i,j,k; clrscr(); printf(“enter no.of frames”); /*reading number of frames*/ scanf(“%d”,&n); printf(“enter page reference string & enter -1 at end”); i=0; while(1) { scanf(“%d”,&prs[i]); if(prs[i]==-1) break; i++; prscount++; } t=0; k=0; for(i=0;i<prscount;i++) { printf(“pages present in frames:”); for(j=0;j<n;j++) printf(“%d”,frames[i]); printf(“page fault %d”,k); if(page_found(prs[i])==1) { k=0; continue; } fff=findfreeframe(); if(fff>=0) frames[fff]=prs[i]; else { frames[t]=prs[i]; t=(t+1)%n; } k=1; pfc++; }

Page 46: Os Lab Manual II a(ECM)

46

printf(“pages present in frames”); for(j=0;j<n;j++) printf(“%d”,frames[j]); printf(“page faults %d”,k); printf(“no.of page faults are %d”,pfc); /* printing page faults */ getch(); }

int page_found(int a) /* functions for page finding */{ int I; for(i=0;i<n;i++) { if(frames[i]==a) return 1; } return 0;}

int findfreeframe() /*function for finding free frames*/{ int I; for(i=0;i<n;i++) { if(frames[i]==0) return i; } return -1;}

INPUT/OUTPUT:1. enter no.of frames: 3 Enter page refence strings & enter -1 at end 5 4 3 2 5 1 4 5 2 -1 Pages present in frames: 0 0 0 page fault 0 Pages present in frames: 5 0 0 page fault 1 Pages present in frames: 5 4 0 page fault 1 Pages present in frames: 5 4 3 page fault 1 Pages present in frames: 2 4 3 page fault 1 Pages present in frames: 2 5 3 page fault 1 Pages present in frames: 2 5 1 page fault 1 Pages present in frames: 4 5 1 page fault 1 Pages present in frames: 4 5 1 page fault 0 Pages present in frames: 4 2 1 page fault 1No.of page faults are : 8

2. enter no.of frames: 4 Enter page refence strings & enter -1 at end 1 2 3 5 4 3 2 5 4 1 -1 Pages present in frames: 0 0 0 0 page fault 0 Pages present in frames: 1 0 0 0 page fault 1 Pages present in frames: 1 2 0 0 page fault 1

Page 47: Os Lab Manual II a(ECM)

47

Pages present in frames: 1 2 3 0 page fault 1 Pages present in frames: 1 2 3 5 page fault 1 Pages present in frames: 4 2 3 5 page fault 1 Pages present in frames: 4 2 3 5 page fault 0 Pages present in frames: 4 2 3 5 page fault 0 Pages present in frames: 4 2 3 5 page fault 0 Pages present in frames: 4 2 3 5 page fault 0 Pages present in frames: 4 1 3 5 page fault 1 No.of page page faults are 6

OBSERVATIONS: Error: return type of function is not mentioned.

RESULT: The page replacement algorithm using FIFO is simulated.

CONCLUSION: The output is correct and hence the program is error free.

Page 48: Os Lab Manual II a(ECM)

48

EXP NO:7(b)

AIM:To implement the algorithm for the page replacement LRU:least recently used

DESCRIPTION:The least recently used*lru)policy replaces the page in memory that has

not been referenced for the longest time.By the principle of locality this would be the page least likely to be referenced in the near future.The problem with the approach is the difficulty in implementation.One approach would to tag each page with the time of its last reference.This would have to be done at each memory reference,both instruction and data.Even if the hardware if you support such scheme,the overhead would be tremendrous ALGORITHM: 1)start 2)read the no.of frames to n 3)read the page referencestring and the count of no.of pages will be in prscount 4)for i=0to prscount a)increment count b)write pages present in frames as j=0 to n in frames[j] c)write the page faults in k d)if page_found(prs[i]==1) i)assign 0 to k ii)assign count to g[z] and continue e)fff=find_free_frame() f)if fff> =0 then i)t[i] is given the value of count ii)frames[fff]=prs[i] else i)lru(i) g)assign 1 to k h)increment pfc5)write the pages present in the frame6) write page faults as k7) write total no of pages as pfc 8)stop

Algorithm page_found(a)1)start2)for i from 0 to n a)if frames==a then i)assign I to z ii)return 13)stop

Algorithm Find_Free_Frame()1)start2)for i from o to n

Page 49: Os Lab Manual II a(ECM)

49

a)if frames[i]==0 then i)return i3)stop

Algorithm LRU(b) 1)start 2)for f=0ton a)if t[f]<=small then i)assign tf to small ii)assign f to si b)frame[si]=prs[b] c)count 3)stop

PROGRAM:/*********program to implement LRU**********/#include<stdio.h>#include<conio.h>

int n,count,c,z;main() {int page_found(int a);int Find_Free_Frame(void);void LRU(int a);static int prscount,pfc,fff;int I,j,k=0,x,y;clrscr();pri ntf(“enter the no.of frames “); /* reading number of frames*/scanf(“%d”,&n);printf(“enter the page reference string :”);i=0;while(1){ scanf(“%d”,&prs[i]); if(prs[i]==-1) break; }for(i=0;i<prscount;i++) { count++; printf(“ pages present in frames as “); for(j=0 ;j<= n;j++) printf(“%d”,frames[j]); printf(“\t page faults”); if (page_found(prs[i]==1)) /* if page found*/ { k=0; t[z]=count ; continue

Page 50: Os Lab Manual II a(ECM)

50

} fff=find_free_frame(); if (fff> =0 ) /* if free frame exists*/ { t[i] = count; frames[fff]=prs[i]; } else lru(i); k=1; pfc++;printf(“\n the pages present in the frame:”)for(j=0;j<n;j++) printf(“%d”,frames[j]);printf(“\t page faults\t:%d\n:”,k);printf(“ total no of pages:%d”, pfc );getch();} int page_found(a)/*function for finding page*/ { for (i =0;i< n;i++) if (frames==a ) { z=i; return 1; } }

int Find_Free_Frame()/*function freeing frame*/{ for (i =o;i<n;i++) if (frames[i]==0) return I; return -1; }

void LRU(b) /*function for finding lru */ { for( f=0;f<n;f++) if (t[f]<=small) { Small=tf; Si=f; } frame[si]=prs[b]; t[si]=count; }

Page 51: Os Lab Manual II a(ECM)

51

Input/Output:1) enter no of frames:3

enter the page reference strings:5 3 4 1 2 4 5 1 2 -1pages present in frames:000 page faults:0pages present in frames:500 page faults:1pages present in frames:530 page faults:1pages present in frames:534 page faults:1pages present in frames:134 page faults:1pages present in frames:124 page faults:1pages present in frames:124 page faults:0pages present in frames:524 page faults:1pages present in frames:514 page faults:1pages present in frames:512 page faults:1total no of page faults:8

2) enter no of frames:4enter the page reference strings:1 4 5 2 3 1 2 4 5 -1pages present in frames:0000 page faults:0pages present in frames:1000 page faults:1pages present in frames:1400 page faults:1pages present in frames:1450 page faults:1pages present in frames:1452 page faults:1pages present in frames:3452 page faults:1pages present in frames:3152 page faults:1pages present in frames:3152 page faults:0pages present in frames:3152 page faults:0pages present in frames:3142 page faults:1pages present in frames:3542 page faults:1

total no of page faults 8 RESULT: The replacement of pages in main memory is done successfully and the lru algorithm is successfully executed.OBSERVATIONS: Error: statement : missing in function main Correction:keeping ; at the required positionCONCLUSION: The output is correct and hence the program is error free

VIVA QUESTIONS:1) which page replacement algorithm is efficient? Lru2) what is page fault ? Unavailability of page3) What are thrashing?Coincidence of high page traffic and low CPU utilization

Page 52: Os Lab Manual II a(ECM)

52

EXPNO: 8AIM: To write a program for simulation of paging technique for memory managementDESCRIPTION:

The main memory is partitioned into equal fixed size chunks, which are relatively small and that each process is also divided into small fixed size chunks of same size. Then the chunks of a process, known as pages, would be assigned to available chunks of memory known as frames or page frames. At a given point in time, some of the frames in memory are in use and some are free.

ALGORITHM:1) start2) read process size in KB of max 12KB to size3) total number of pages will be size/44) read the relative address in hexadecimal notation to ra5) write that the length of relative address is 16 bits and the size of offset is

12bits6) Page no. is ra/1000; s is ra%1000 write pageno.7) Write page table for i from 0 to n i, pagetable[i]8) Page frame no. is pagetable[pgno]9) Equivalent physical address is frameno. Of s10) Stop

PROGRAM:

/***********************************************************************To simulate paging technique for memory management***********************************************************************/#include<stdio.h>#include<conio.h>#include<math.h>main(){ int size,m,n,pgno,pagetable[3]={5,6,7},i,j,frameno; double m1; int ra=0,ofs; clrscr(); printf(“Enter process size (in KB of max 12KB):”);/*reading memeory size*/ scanf(“%d”,&size); m1=size/4; n=ceil(m1); printf(“Total No. of pages: %d”,n); pritnf(“\nEnter relative address (in hexadecimal notation eg.0XRA) \n”); printf(“The length of relative Address is : 16 bits \n\n The size of offset is :12 bits\n”); scanf(“%d”,&ra);

Page 53: Os Lab Manual II a(ECM)

53

pgno=ra/1000; /*calculating physical address*/ ofs=ra%1000; printf(“%d”,pgno); printf(“page table”); for(i=0;i<n;i++) printf(“\n %d [%d]”,i,pagetable[i]); frameno=pagetable[pgno]; printf(“\n Equivalent physical address : %d%d”,frameno.ofs); getch(); }

RESULT:The process is divided into partitions and placed as pages in main

memory.

INPUT/OUTPUT:Enter process size(in KB of max 12KB): 12Total no. of pages: 3Enter relative address(in hexadecimal notation eg.0XRA):The length of relative Address is: 16 bits The size of offset is: 12 bits26432

Page table0 [5]1 [6]2 [7]

OBSERVATIONS:Error: undefined symbol n in function mainCorrection: n is declared in main functionCONCLUSION:The paging technique for memory management is simulated successfully.VIVA QUESTIONS:

1) What is internal fragmentation?Wastage of memory when page size is less than memory size.

2)what is critical section? Ans) section of code which cannot be executed concurrently.


Recommended