Sei sulla pagina 1di 49

SWCET

Date :

Week 9
1. Simulate the following CPU scheduling algorithms (a) Round Robin
#include<stdio.h> #include<conio.h> void main() { int et[30],ts,n,i,x=0,tot=0; char pn[10][10]; clrscr(); printf("Enter the no of processes:"); scanf("%d",&n); printf("Enter the time quantum:"); scanf("%d",&ts); for(i=0;i<n;i++) { printf("enter process name & estimated time:"); scanf("%s %d",pn[i],&et[i]); } printf("The processes are:"); for(i=0;i<n;i++) printf("process %d: %s\n",i+1,pn[i]); for(i=0;i<n;i++) tot=tot+et[i]; while(x!=tot) {

Op erating Systems

30

SWCET

Date :

for(i=0;i<n;i++) { if(et[i]>ts) { x=x+ts; printf("\n %s -> %d",pn[i],ts); et[i]=et[i]-ts; } else if((et[i]<=ts)&&et[i]!=0) { x=x+et[i]; printf("\n %s -> %d",pn[i],et[i]); et[i]=0;} } } printf("\n Total Estimated Time:%d",x); getch(); }

Op erating Systems

31

SWCET

Date :

Output
Enter the no of processes : 2 Enter the time quantum: 3

Enter the process name & estimated time : p1 12 Enter the process name & estimated time : p2 15 p1 ->3 p2 ->3 p1 ->3 p2 ->3 p1 ->3 p2 ->3 p1 ->3 p2 ->3 p2 -> 3 Total estimated time : 27

Op erating Systems

32

SWCET

Date :

(b) SJF
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10]; int totwt=0,totta=0; float awt,ata; char pn[10][10],t[10]; clrscr(); printf("Enter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter process name, arrival time & execution time:"); flushall(); scanf("%s%d%d",pn[i],&at[i],&et[i]); } for(i=0;i<n;i++) for(j=0;j<n;j++) { if(et[i]<et[j]) { temp=at[i]; at[i]=at[j]; at[j]=temp;

Op erating Systems

33

SWCET

Date :

temp=et[i]; et[i]=et[j]; et[j]=temp; strcpy(t,pn[i]); strcpy(pn[i],pn[j]); strcpy(pn[j],t); } } for(i=0;i<n;i++) { if(i==0) st[i]=at[i]; else st[i]=ft[i-1]; wt[i]=st[i]-at[i]; ft[i]=st[i]+et[i]; ta[i]=ft[i]-at[i]; totwt+=wt[i]; totta+=ta[i]; } awt=(float)totwt/n; ata=(float)totta/n; printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime"); for(i=0;i<n;i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]); printf("\nAverage waiting time is:%f",awt); printf("\nAverage turnaroundtime is:%f",ata);

Op erating Systems

34

SWCET

Date :

getch(); }

Output
Enter the number of processes: 3 Enter the process name, arrival time and burst time: p1 4 6 Enter the process name, arrival time and burst time: p2 5 15 Enter the process name, arrival time and burst time: p3 6 11 Pname p1 p3 p2 Arrival Time 4 6 5 Execution Time Waiting Time 6 11 15 0 4 16 Turnaround Time 6 15 31

Average waiting time : 6.6667 Average turn around time : 17.3333

Op erating Systems

35

SWCET

Date :

(c) FCFS
#include<stdio.h> #include<string.h> #include<conio.h> main() { char pn[10][10],t[10]; int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp; int totwt=0,tottat=0; int time_passed=0; //clrscr(); printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Process Name, Arrival Time & Burst Time:"); scanf("%s%d%d",&pn[i],&arr[i],&bur[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;

Op erating Systems

36

SWCET

Date :

temp=bur[i]; bur[i]=bur[j]; bur[j]=temp; strcpy(t,pn[i]); strcpy(pn[i],pn[j]); strcpy(pn[j],t); } } } for(i=0;i<n;i++) { if(arr[i]>time_passed) time_passed=arr[i]; star[i]=time_passed; time_passed += bur[i]; wt[i]=star[i]-arr[i]; finish[i]=star[i]+bur[i]; tat[i]=finish[i]-arr[i]; } printf("\nPName Arrtime Burtime WaitTime Start\tTAT"); for(i=0;i<n;i++) { printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t",pn[i],arr[i],bur[i],wt[i],star[i],tat[i]); totwt+=wt[i]; tottat+=tat[i]; } printf("\nAverage Waiting time:%f",(float)totwt/n);

Op erating Systems

37

SWCET

Date :

printf("\nAverage Turn Around Time:%f",(float)tottat/n); getch(); return 0; }

Output
Enter the number of processes: 3 Enter the process name, arrival time and burst time: p1 6 5 Enter the process name, arrival time and burst time: p2 4 15 Enter the process name, arrival time and burst time: p3 8 10 PName p2 p1 p3 Arrival Time 4 6 8 Burst Time 15 5 10 Wait Time 0 13 16 Start 4 19 24 TAT 15 18 26

Average waiting time : 9.666667 Average Turn around time: 19.666667

Op erating Systems

38

SWCET

Date :

(d) Priority
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10]; int totwt=0,totta=0; float awt,ata; char pn[10][10],t[10]; clrscr(); printf("Enter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter process name,arrivaltime,execution time & priority:"); flushall(); scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]); } for(i=0;i<n;i++) for(j=0;j<n;j++) { if(p[i]<p[j]) { temp=p[i]; p[i]=p[j]; p[j]=temp;

Op erating Systems

39

SWCET

Date :

temp=at[i]; at[i]=at[j]; at[j]=temp; temp=et[i]; et[i]=et[j]; et[j]=temp; strcpy(t,pn[i]); strcpy(pn[i],pn[j]); strcpy(pn[j],t); } for(i=0;i<n;i++) { if(i==0) { st[i]=at[i]; wt[i]=st[i]-at[i]; ft[i]=st[i]+et[i]; ta[i]=ft[i]-at[i]; } else { st[i]=ft[i-1]; wt[i]=st[i]-at[i]; ft[i]=st[i]+et[i]; ta[i]=ft[i]-at[i]; } totwt+=wt[i];

Op erating Systems

40

SWCET

Date :

totta+=ta[i]; } awt=(float)totwt/n; ata=(float)totta/n; printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime"); for(i=0;i<n;i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]); printf("\nAverage waiting time is:%f",awt); printf("\nAverage turnaroundtime is:%f",ata); getch(); }

Op erating Systems

41

SWCET

Date :

Output
Enter the number of processes: 3 Enter the process name, arrival time, execution time and priority: p1 2 3 1 Enter the process name, arrival time, execution time and priority: p2 4 5 2 Enter the process name, arrival time, execution time and priority: p3 5 6 3 Pname p1 p3 p2 Arrival Time Execution Time 2 4 5 3 5 6 Priority 1 2 3 Waiting Time Turnaround Time 0 1 5 3 6 11

Average waiting time : 2.0000 Average turn around time : 6.6667

Op erating Systems

42

SWCET

Date :

Week 10
2. Simulate the following file allocation strategies (a) Sequential
#include<stdio.h> #include<conio.h> main() { int a[50],n,i,j,k,fname,startblock,length,currentfilename; printf("Enter the number of files:\t"); scanf("%d", &n); for(i=0;i<50;i++) a[i]=0; for(i=0;i<n;i++) { printf("\nEnter the filename:\t\t"); scanf("%d", &fname); printf("Enter the Start Block:\t\t"); scanf("%d",&startblock); printf("Enter the length of the file:\t"); scanf("%d", &length); for(j=startblock;j<startblock+length;j++) { if(a[j]!=0) { printf("\nError! Memory cannot be overwritten!"); break; }

Op erating Systems

43

SWCET

Date :

if(j==startblock+length-1) for(k=startblock;k<startblock+length;k++) } } printf("\n\nThe memory is:\n"); for(j=0;j<50;j++) { printf("%d\t",a[j]); if((j+1)%10==0) printf("\n"); } startblock=0; for(j=0;j<50;j++) { if(a[j]!=0) { currentfilename=a[j]; a[startblock]=a[j]; a[j]=0; while(a[++j]==currentfilename) { a[++startblock]=a[j]; a[j]=0; } while(a[++startblock]!=0); } } printf("\n\nThe re-arranged memory:\n"); a[k]=fname;

Op erating Systems

44

SWCET

Date :

for(j=0;j<50;j++) { printf("%d\t",a[j]); if((j+1)%10==0) printf("\n"); } getch(); }

Output
Enter the number of files: 4 Enter the filename: 1 Enter the start block: 2 Enter the length of the file: 3

Enter the filename: 2 Enter the start block: 9 Enter the length of the file:5

Enter the filename: 3 Enter the start block: 18 Enter the length of the file:8

Enter the filename: 4 Enter the start block: 30 Enter the length of the file:2

Op erating Systems

45

SWCET

Date :

The memory is: 0 2 3 4 0 0 2 3 4 0 1 2 3 0 0 1 2 3 0 0 1 0 3 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 2 3 0 0 0

After Compaction: 1 3 0 0 0 1 3 0 0 0 1 3 0 0 0 2 3 0 0 0 2 3 0 0 0 2 3 0 0 0 2 4 0 0 0 2 4 0 0 0 3 0 0 0 0 3 0 0 0 0

Op erating Systems

46

SWCET

Date :

(b) Linked
#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int a[50][3],n,i,j,k,fname,startblock,length,currentfilename; int blocksallocated,currentlocation,previouslocation; printf("Enter the number of files:\t"); scanf("%d", &n); srand(time(NULL)); for(i=0;i<50;i++) { a[i][0]=0;a[i][1]=0;a[i][2]=0; }

for(i=0;i<n;i++) { printf("\nEnter the filename:\t\t"); scanf("%d", &fname); printf("Enter the length of the file:\t"); scanf("%d", &length); blocksallocated=0; while(blocksallocated<length) { currentlocation = rand()%50; if(a[currentlocation][0]!=0) continue;

Op erating Systems

47

SWCET

Date :

a[currentlocation][0]= fname; if(blocksallocated!=0) a[previouslocation][1] = currentlocation; else a[currentlocation][2] = 1; previouslocation = currentlocation; blocksallocated++; if(blocksallocated==length) a[currentlocation][1] = -1; } } printf("\n\nThe memory is:\n"); for(j=0;j<50;j++) { printf("%d %d %d %d \t",j,a[j][0],a[j][1],a[j][2]); if((j+1)%4==0) printf("\n"); } getch(); }

Op erating Systems

48

SWCET

Date :

Output
Enter the number of files: 2 Enter the filename: 1 Enter the length of the file: 4 Enter the filename: 2 Enter the length of the file:3 0 4 8 12 16 20 24 28 32 36 40 44 48 2 1 0 0 0 0 0 0 0 0 0 0 0 18 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 5 9 13 17 21 25 29 33 37 41 45 49 1 0 0 1 0 0 0 0 0 0 0 0 0 46 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 6 10 14 18 22 26 30 34 38 42 46 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 0 0 0 0 7 11 15 19 23 27 31 35 39 43 47 3 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

Op erating Systems

49

SWCET

Date :

Week 11
3. Simulate MVT and MFT MVT
#include<stdio.h> #include<conio.h> void main() { int m=0,m1=0,m2=0,p,count=0,i; clrscr(); printf("enter the memory capacity:"); scanf("%d",&m); printf("enter the no of processes:"); scanf("%d",&p); for(i=0;i<p;i++) { printf("\nenter memory req for process%d: ",i+1); scanf("%d",&m1); count=count+m1; if(m1<=m) { if(count==m) printf("there is no further memory remaining:"); printf("the memory allocated for process%d is: %d ",i+1,m); m2=m-m1; printf("\nremaining memory is: %d",m2); m=m2;

Op erating Systems

50

SWCET

Date :

} else { printf("memory is not allocated for process%d",i+1); } printf("\nexternal fragmentation for this process is:%d",m2); } getch(); }

Output
Enter the memory capacity: 80 Enter no of processes: 2 Enter memory req for process1: 23 The memory allocated for process1 is: 80 Remaining memory is: 57 External fragmentation for this process is: 57 Enter memory req for process2: 52 The memory allocated for process2 is: 57 Remaining memory is: 5 External fragmentation for this process is: 5

Op erating Systems

51

SWCET

Date :

MFT
#include<stdio.h> #include<conio.h> int main() { int m,p,s,p1; int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos; clrscr(); printf("Enter the memory size:"); scanf("%d",&m); printf("Enter the no of partitions:"); scanf("%d",&p); s=m/p; printf("Each partn size is:%d",s); printf("\nEnter the no of processes:"); scanf("%d",&p1); pos=m; for(i=0;i<p1;i++) { if(pos<s) { printf("\nThere is no further memory for process%d",i+1); m1[i]=0; break; } else {

Op erating Systems

52

SWCET

Date :

printf("\nEnter the memory req for process%d:",i+1); scanf("%d",&m1[i]); if(m1[i]<=s) { printf("\nProcess is alloca ted in partition%d",i+1); fra1=s-m1[i]; printf("\nInternal fragmentation for process is:%d",fra1); f1=f1+fra1; pos=pos-s; } else { printf("\nProcess not allocated in partition%d",i+1); s1=m1[i]; while(s1>s) { s1=s1-s; pos=pos-s; } pos=pos-s; fra2=s-s1; f2=f2+fra2; printf("\nExternal Fragmentation for this process is:%d",fra2); } } } printf("\nProcess\tallocatedmemory");

Op erating Systems

53

SWCET

Date :

for(i=0;i<p1;i++) printf("\n%5d\t%5d",i+1,m1[i]); f=f1+f2; printf("\nThe tot no of fragmentation is:%d",f); getch(); return 0; }

Output
Enter the memory size: 80 Enter the no of partitions: 4 Each partition size: 20 Enter the number of processes: 2 Enter the memory req for process1: 18 Process1 is allocated in partn1 Internal fragmentation for process1 is: 2 Enter the memory req for process2: 22 Process2 is not allocated in partn2 External fragmentation for process2 is: 18 Process 1 2 memory 20 20 allocated 18 22

The tot no of fragmentation is: 20

Op erating Systems

54

SWCET

Date :

4. Simulate all file organization techniques (a) Single level directory


#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> void main() { int gd=DETECT,gm,count,i,j,mid,cir_x; char fname[10][20]; clrscr(); initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice(); setbkcolor(GREEN); puts("Enter no of files do u have?"); scanf("%d",&count); for(i=0;i<count;i++) { cleardevice(); setbkcolor(GREEN); printf("Enter file %d name",i+1); scanf("%s",fname[i]); setfillstyle(1,MAGENTA); mid=640/count; cir_x=mid/3; bar3d(270,100,370,150,0,0); settextstyle(2,0,4);

Op erating Systems

55

SWCET

Date :

settextjustify(1,1); outtextxy(320,125,"Root Directory"); setcolor(BLUE); for(j=0;j<=i;j++,cir_x+=mid) { line(320,150,cir_x,250); fillellipse(cir_x,250,30,30); outtextxy(cir_x,250,fname[j]); } getch(); } }

Output
Enter no of files do u have? 1 Enter file 1 name: 1.c
ROOT

Op erating Systems

56

SWCET

Date :

(b) Two level directory


#include<stdio.h> #include<graphics.h> struct tree_element { char name[20]; int x,y,ftype,lx,rx,nc,level; struct tree_element *link[5]; }; typedef struct tree_element node; void main() { int gd=DETECT,gm; node *root; root=NULL; clrscr(); create(&root,0,"null",0,639,320); clrscr(); initgraph(&gd,&gm,"c:\\tc\\bgi"); display(root); getch(); closegraph(); } create(node **root,int lev,char *dname,int lx,int rx,int x) { int i,gap; if(*root==NULL)

Op erating Systems

57

SWCET

Date :

{ (*root)=(node*)malloc(sizeof(node)); printf("enter name of dir/file(under %s):",dname); fflush(stdin); gets((*root)->name); if(lev==0||lev==1) (*root)->ftype=1; else (*root)->ftype=2; (*root)->level=lev; (*root)->y=50+lev*50; (*root)->x=x; (*root)->lx=lx; (*root)->rx=rx; for(i=0;i<5;i++) (*root)->link[i]=NULL; if((*root)->ftype==1) { if(lev==0||lev==1) { if((*root)->level==0) printf("How many users"); else printf("hoe many files"); printf("(for%s):",(*root)->name); scanf("%d",&(*root)->nc); }

Op erating Systems

58

SWCET

Date :

else (*root)->nc=0; if((*root)->nc==0) gap=rx-lx; else gap=(rx-lx)/(*root)->nc; for(i=0;i<(*root)->nc;i++) create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2); } else (*root)->nc=0; } } display(node *root) { int i; settextstyle(2,0,4); settextjustify(1,1); setfillstyle(1,BLUE); setcolor(14); if(root!=NULL) { for(i=0;i<root->nc;i++) { line(root->x,root->y,root->link[i]->x,root->link[i]->y); } if(root->ftype==1)

Op erating Systems

59

SWCET

Date :

bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else fillellipse(root->x,root->y,20,20); outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++) { display(root->link[i]); } } }

Op erating Systems

60

SWCET

Date :

Output
enter name of dir/file(under null):sld How many users(forsld):2 enter name of dir/file(under sld):tld hoe many files(fortld):2 enter name of dir/file(under tld):hir enter name of dir/file(under tld):dag enter name of dir/file(under sld):bin hoe many files(forbin):2 enter name of dir/file(under bin):exe enter name of dir/file(under bin):obj

tld

bin

hir da ex

ob j

Op erating Systems

61

SWCET

Date :

Week 12
5. Simulate Bankers Algorithm for deadlock avoidance
#include<conio.h> #include<stdio.h> void main() { int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7]; int first,p,r,i,j,prc,count,t,total=0; count=0; for(i=1;i<=7;i++) comp[i]=0; printf("Enter the number of processes:"); scanf("%d",&p); printf("Enter the number of resources:"); scanf("%d",&r); printf("Enter the claim for each process:"); for(i=1;i<=p;i++) { printf("\n For process %d:",i); for(j=1;j<=r;j++) { scanf("%d",&clm[i][j]); }} printf("Enter the allocation for each process:"); for(i=1;i<=p;i++) { printf("\n For process %d :",i);
Op erating Systems 62

SWCET

Date :

for(j=1;j<=r;j++) { scanf("%d", &alloc[i][j]); }} printf("Enter total no of each resource:"); for(j=1;j<=r;j++) scanf("%d",&rsrc[j]); for(j=1;j<=r;j++) { avail[j]=0; //int total=0; for(i=1;i<=p;i++) {total+=alloc[i][j];} avail[j]=rsrc[j]-total; } do { for(i=1;i<=p;i++) { for(j=1;j<=r;j++) { req[i][j]=clm[i][j]-alloc[i][j]; }} printf("\n\nAvailable resorces is:"); for(j=1;j<=r;j++) { printf(" " "%d", avail[j]); } printf("\nClaim matrix:\t\tAllocation matrix:\n"); for(i=1;i<=p;i++) { for(j=1;j<=r;j++)

Op erating Systems

63

SWCET

Date :

{ printf("%d",clm[i][j]); } printf("\t\t\t"); for(j=1;j<=r;j++) { printf("%d",alloc[i][j]); } printf("\n"); } prc=0; for(i=1;i<=p;i++) { if(comp[i]==0)//if not completed { prc=i; for(j=1;j<=r;j++) { if(avail[j]<req[i][j]) { prc=0;break; } } } if(prc!=0) break; } if(prc!=0) { printf("\nProcess %d ", prc ,"runs to completion!"); count++; for(j=1;j<=r;j++) {

Op erating Systems

64

SWCET

Date :

avail[j]+=alloc[prc][j]; alloc[prc][j]=0; clm[prc][j]=0; comp[prc]=1; } } } while(count!=p&&prc!=0); if(count==p) printf("\nThe system is in a safe state!!"); else printf("\nThe system is in an unsafe state!!"); getch(); }

Op erating Systems

65

SWCET

Date :

Output
Enter the number of processes : 4 Enter the number of resources : 1 Enter the claim matrix for each process: For process 1: 6 For process 2: 5 For process 3 : 4 For process 4 : 7 Enter the allocation matrix for each process: For process 1: 1 For process 2: 2 For process 3 : 2 For process 4 : 4 Enter the total number of resources : 10 Available resource is : 1 Claim matrix 6 5 4 7 Allocation matrix 1 2 2 4

The system is in an unsafe state.

Op erating Systems

66

SWCET

Date :

6. Simulate Bankers Algorithm for deadlock prevention

Deadlock Prevention:
o

Preventing deadlocks by constraining how requests for resources can be made in the system and how they are handled (system design).

The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.

Deadlock Avoidance:
o

The system dynamically considers every request and decides whether it is safe to grant it at this point,

The system requires additional apriori information regarding the overall potential use of each resource for each process.

Allows more concurrency.

Similar to the difference between a traffic light and a police officer directing traffic. Difference from avoidance is that here, the system itself is build in such a way that there are no deadlocks. The difference between the two is very subtle. The above program can be used as an implementation of deadlock prevention.

Conditions for deadlock .


Mutual exclusion: resources cannot be shared. Hold and wait: processes request resources incrementally, and hold on to what they've got.

No preemption: resources cannot be forcibly taken from processes. Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain. For Deadlock prevention ,

Make sure atleast one of the 4 deadlock conditions is never satisfied. This may however be even more conservative than deadlock avoidance strategy.

Op erating Systems

67

SWCET

Date :

Week 13
7. Simulate all page replacement algorithms (a) FIFO
#include<stdio.h> #include<conio.h> #define PAGES_IN_WINDOW 3 void main() { int window[PAGES_IN_WINDOW], window_old[PAGES_IN_WINDOW]; int pages[50]; int number_of_pages; int page_faults = 0; int i,j,k,l; clrscr(); printf("\nEnter the number of pages:\t"); scanf("%d", &number_of_pages); printf("\nEnter the pages in order:\t"); for(l=0;l<number_of_pages;l++) { scanf("%d",&pages[l]); } for(l=0;l<PAGES_IN_WINDOW;l++) { window[l]=0; window_old[l]=0; }

Op erating Systems

68

SWCET

Date :

for(i=0;i<number_of_pages;i++) { int page_number = pages[i]; printf("\n\nReferring %d",page_number);

for(k=0;k<PAGES_IN_WINDOW;k++) { if(page_number == window[k]) { break; } if(k==PAGES_IN_WINDOW-1) { for(j=0;j<PAGES_IN_WINDOW-1;j++) { window[j+1]=window_old[j]; }

window[0]=page_number; page_faults++; printf("\nPage Fault"); } } printf("\nWindow:\t"); for(j=PAGES_IN_WINDOW-1;j>=0;j--) { if((window[j]>0)&&(window[j]<10))

Op erating Systems

69

SWCET

Date :

printf("%d\t",window[j]); window_old[j]=window[j]; } } printf("\n\nNumber of page faults: %d",page_faults); getch(); }

Output
Enter the number of pages : 12 Enter the 12 page numbers: 232152453252 Referring 2 Page Fault Window : 2

Referring 3 Page Fault Window : 2 3

Referring 2 Window : 2 3

Referring 1 Page Fault Window : 2 3 1

Referring 5

Op erating Systems

70

SWCET

Date :

Page Fault Window : 3 1 5

Referring 2 Page Fault Window : 1 5 2

Referring 4 Page Fault Window : 5 2 4

Referring 5 Window : 5 2 4

Referring 3 Page Fault Window : 2 4 3

Referring 2 Window : 2 4 3

Referring 5 Page Fault Window : 4 3 5

Referring 2 Page Fault

Op erating Systems

71

SWCET

Date :

Window : 3 5 2

Number of page faults : 9

Op erating Systems

72

SWCET

Date :

(b) LRU
#include<stdio.h> #include<conio.h> #define PAGES_IN_WINDOW 3 void main() { int window[PAGES_IN_WINDOW]; int pages[50]; int number_of_pages; int page_faults = 0; int lru[PAGES_IN_WINDOW]; int i,j,k,l; clrscr(); printf("\nEnter the number of pages:\t"); scanf("%d", &number_of_pages); printf("\nEnter the pages in order:\t"); for(l=0;l<number_of_pages;l++) { scanf("%d",&pages[l]); } for(l=0;l<PAGES_IN_WINDOW;l++) { window[l]=0; lru[l]=0; } for(i=0;i<number_of_pages;i++) {

Op erating Systems

73

SWCET

Date :

int page_number = pages[i]; int maximum_u=0; int maximum_u_location=0; int found=0; printf("\n\nReferring %d",page_number); for(k=0;k<PAGES_IN_WINDOW;k++) { if(lru[k]>maximum_u) { maximum_u=lru[k]; maximum_u_location=k; } if(page_number == window[k]) { found=1; lru[k]=0; } else { lru[k]++; } if((found==0)&(k==PAGES_IN_WINDOW-1)) { window[maximum_u_location] = page_number; lru[maximum_u_location] = 0;

page_faults++;

Op erating Systems

74

SWCET

Date :

printf("\nPage Fault"); } } printf("\nWindow:\t"); for(j=0;j<PAGES_IN_WINDOW;j++) { if((window[j]>0)&&(window[j]<10)) printf("%d\t",window[j]); } } printf("\n\nNumber of page faults: %d",page_faults); getch(); }

Output
Enter the number of pages : 12 Enter the 12 page numbers: 232152453252 Referring 2 Page Fault Window : 2

Referring 3 Page Fault Window : 2 3

Referring 2 Window : 2 3

Op erating Systems

75

SWCET

Date :

Referring 1 Page Fault Window : 2 3 1

Referring 5 Page Fault Window : 2 5 1

Referring 2 Window : 2 5 1

Referring 4 Page Fault Window : 2 5 4

Referring 5 Window : 2 5 4

Referring 3 Page Fault Window : 3 5 4

Referring 2 Page Fault Window : 3 5 2

Op erating Systems

76

SWCET

Date :

Referring 5 Window : 3 5 2

Referring 2 Window : 3 5 2

Number of page faults : 7

Op erating Systems

77

SWCET

Date :

8. Simulate paging technique of memory management.


#include<stdio.h> #include<conio.h> main() { int np,ps,i; int *sa; printf("enter how many pages\n"); scanf("%d",&np); printf("enter the page size \n"); scanf("%d",&ps); sa=(int*)malloc(2*np); for(i=0;i<np;i++) { sa[i]=(int)malloc(ps); printf("page%d\t address %u\n",i+1,sa[i]); } getch(); }

Output
Enter how many pages 4 Enter the page size 2 Page 1 Page 2 Page 3 Page 4 address 1906 address 1914 address 1922 address 1930

Op erating Systems

78

Potrebbero piacerti anche