os programs

37
PART-B 1. a) Round Robin technique. b) Shortest Job First. c) First Come First Serve. d) Priority Scheduling. 2. Simulate all file allocation strategies. a) Sequential b) Indexed c) Linked. 3. Simulate a) Multiprogramming with variable tasks b) Multiprogramming with fixed tasks. 4. Simulate all File organization techniques. a) Single level directory b) Two level c) Hierarchical d) DAG 5. Simulate banker’s algorithm for deadlock prevention. 6. a) First in first out. b) Least recently used. c) Least frequently used. ---------------------------------------- x x x -----------------------------------------

Upload: annekottam

Post on 12-Nov-2014

353 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Os Programs

PART-B

1.a) Round Robin technique.b) Shortest Job First.c) First Come First Serve.d) Priority Scheduling.

2. Simulate all file allocation strategies.a) Sequentialb) Indexedc) Linked.

3. Simulate a) Multiprogramming with variable tasksb) Multiprogramming with fixed tasks.

4. Simulate all File organization techniques.a) Single level directoryb) Two levelc) Hierarchical

d) DAG5. Simulate banker’s algorithm for deadlock prevention.

6.a) First in first out.b) Least recently used.c) Least frequently used.

---------------------------------------- x x x -----------------------------------------

Page 2: Os Programs

SIMULATE THE FOLLOWING CPU SCHEDULING ALGORITHMS

a) FIRST COME FIRST SERVE

#include<stdio.h>#include<conio.h>void main(){ int s[15],w[15],f[15],n,i,j,temp;

char p[15],t; clrscr( ); printf("Enter no of process:"); scanf("%d",&n); printf("Enter %d names:"); scanf("%s",p); printf("Enter %d service times:",n); for(i=0;i<n;i++) scanf("%d",&s[i]); f[0]=s[0]; for(i=1;i<n;i++) f[i]=f[i-1]+s[i]; for(i=0;i<n;i++) w[i]=f[i]-s[i]; printf("\nNAMES:\t"); for(i=0;i<n;i++)

{ printf("\t%c",p[i]);}printf("\nSERVICE TIMES:");for(i=0;i<n;i++)

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

}printf("\nFINISH TIMES:");for(i=0;i<n;i++)

{ printf("\t%d",f[i]); }

printf("\nWAIT TIMES:");for(i=0;i<n;i++)

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

}getch();}

Page 3: Os Programs

INPUT:

Enter no of process: 5

Enter 5 names: ABCDE

Enter 5 service times: 2 3 5 4 9

OUTPUT:

NAMES: A B C D E

SERVICE TIMES: 2 3 5 4 9

FINISH TIMES: 2 5 10 14 23

WAIT TIMES: 0 2 5 10 14

Page 4: Os Programs

b) SHORTEST JOB FIRST

#include<stdio.h>#include<conio.h>void main(){

int s[15],w[15],f[15],n,i,j,temp;char p[15],t;clrscr();printf("Enter no of process:");scanf("%d",&n);printf("Enter %d names:",n);scanf("%s",p);printf("Enter %d service times:",n);for(i=0;i<n;i++)scanf("%d",&s[i]);for(j=0;j<n;j++){

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

if(s[j]>s[i]){

//SEVICE TIMEStemp=s[j];s[j]=s[i];s[i]=temp;//NAMESt=p[j];p[j]=p[i];p[i]=t;

}}

}f[0]=s[0];for(i=1;i<n;i++)f[i]=f[i-1]+s[i];for(i=0;i<n;i++)w[i]=f[i]-s[i];printf("\nNAMES:\t");for(i=0;i<n;i++)

Page 5: Os Programs

{printf("\t%c",p[i]);

}printf("\nSERVICE TIMES:");for(i=0;i<n;i++){

printf("\t%d",s[i]);}printf("\nFINISH TIMES:");for(i=0;i<n;i++){

printf("\t%d",f[i]);}printf("\nWAIT TIMES:");for(i=0;i<n;i++){

printf("\t%d",w[i]);}getch();

}

INPUT:

Enter no of process: 5

Enter 5 names: ABCDE

Enter 5 service times: 5 6 8 1 2

OUTPUT:

NAMES: D E A B C

SERVICE TIMES: 1 2 5 6 8

FINISH TIMES: 1 3 8 14 22

WAIT TIMES: 0 1 3 8 14

Page 6: Os Programs

c)PRIORITY SCHEDULING

#include<stdio.h>#include<conio.h>void main(){

int s[15],w[15],f[15],n,i,j,temp,pr[15];char p[15],t;clrscr();printf("Enter no of process:");scanf("%d",&n);printf("Enter %d names:",n);scanf("%s",p);printf("Enter %d service times:",n);for(i=0;i<n;i++)scanf("%d",&s[i]);printf("Enter %d priorities:",n);for(i=0;i<n;i++)scanf("%d",&pr[i]);for(j=0;j<n;j++){

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

if(pr[j]>pr[i]){

//PRIORITEStemp=pr[j];pr[j]=pr[i];pr[i]=temp;//NAMESt=p[j];p[j]=p[i];p[i]=t;//SEVICE TIMEStemp=s[j];s[j]=s[i];s[i]=temp;

}}

}f[0]=s[0];for(i=1;i<n;i++)

Page 7: Os Programs

f[i]=f[i-1]+s[i];for(i=0;i<n;i++)w[i]=f[i]-s[i];printf("\nNAMES:\t");for(i=0;i<n;i++){

printf("\t%c",p[i]);}printf("\nSERVICE TIMES:");for(i=0;i<n;i++){

printf("\t%d",s[i]);}printf("\nPRIORITIES:");for(i=0;i<n;i++){

printf("\t%d",pr[i]);}printf("\nFINISH TIMES:");for(i=0;i<n;i++){

printf("\t%d",f[i]);}printf("\nWAIT TIMES:");for(i=0;i<n;i++){

printf("\t%d",w[i]);}getch();

}

Page 8: Os Programs

INPUT:Enter no of process:5

Enter 5 names: ABCDEEnter 5 service times: 2 6 5 3 4Enter 5 priorities: 2 3 6 7 1

OUTPUT:NAMES: E A B C D

SERVICE TIMES: 4 2 6 5 3

PRIORITIES: 1 2 3 6 7

FINISH TIMES: 4 6 12 17 20

WAIT TIMES: 0 4 6 12 1

Page 9: Os Programs

d)ROUND ROBIN TECHNIQUE

#include<stdio.h>#include<conio.h>void main(){

int s[15],a[10],w[15],t[15],n,i,j,flag=1,count=0,k,ts;clrscr();printf("Enter no of process:");scanf("%d",&n);printf("Enter %d service times:",n);for(i=1;i<=n;i++)scanf("%d",&s[i]);for(i=1;i<=n;i++)a[i]=s[i];printf("\n enter the timeslice:");scanf("%d",&ts);i=1;while(flag){

for(i=1;i<=n;i++) { f(s[i]==0) ontinue; f(s[i]<=ts)

{count=count+s[i];a[i]=count-a[i];

t[i]=count; s[i]=0;

} else

{ s[i]=s[i]-ts; count=count+ts;

} } flag=0;

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

if(s[i]!=0) {

flag=1; break;

Page 10: Os Programs

} }

}printf("\nprocess\t st\twt\t tat");for(i=1;i<=n;i++)printf("\n%d\t%d\t%d\t%d",i,a[i],w[i],t[i]);getch();

}

INPUT:Enter no of process: 5

Enter 5 service times: 6 4 3 8 7

enter the timeslice: 3

OUTPUT:

process st wt tat

1 6 12 18

2 4 15 19

3 3 6 9

4 8 19 27

5 7 21 28

Page 11: Os Programs

SIMULATE ALL PAGE REPLACEMENT ALGORITHMS

a)FIRST IN FIRST OUT

#include <stdio.h>#include<conio.h>void main( ){ int r[15],ff=0,n,f[3]={0,0,0},k,l,x=0,y=0,p=0,a;

clrscr();printf(“enter the no of referencepages;”);scanf(“%d”,&n);printf(“enter the frames”);scanf(%d”,&ff);for( k=0; k<n;k++){

scanf(“%d”,&r[k]);}for(l=0;l<n;l++){

for(k=0;k<ff;k++){

if(f[k]==r[l]) x=1;

} if(x==0) { f[y]=r[l]; for(a=0;a<ff;a++) { printf(“%d\t”,f[a]) } printf(“\n”); y++; p+=1; } if(x==1) x=0; if(y==ff) y=0;

}p=p-ff;printf(“no of page faults %d”,p);getch( );

Page 12: Os Programs

}INPUT:

Enter the no of reference paes 12Enter the frames 32 3 2 1 5 2 4 5 3 2 5

OUTPUT:2 0 02 3 02 3 1 5 3 15 2 15 2 43 2 43 5 4 3 5 2

no of page faults 6

Page 13: Os Programs

b)LEAST RECENTLY USED

#include <stdio.h>#include<conio.h>void main( ){ int r[15],n,f[5]={0,0,0,0,0},k,l,x=0,y=0,p=0,a,s,fr,d,b[5]={0,0,0,0,0}, c[5]={0,0,0,0,0};

clrscr();printf(“enter the no of referencepages;”);scanf(“%d”,&n);printf(“enter the frames”);scanf(%d”,&fr);for( k=0; k<n;k++){

scanf(“%d”,&r[k]);}for(l=0;l<n;l++){

for(k=0;k<fr;k++){

if(f[k]==r[l]) x=1;

}if(x==0){

f[y]=r[l];for(a=0;a<fr;a++){

printf(“%d\t”,f[a]); printf(“\n”); y++; p+=1;

}if(x==1)x=0;if(y==fr)break;

}for (;l<n;l++){ for(k=0;k<fr;k++)

{ ig(f[k]==r[l])

x=1;

Page 14: Os Programs

}if(x==0){

for(k=0;k<fr;k++){

for(d=l;d>=0;d--){

if(f[l]==r[d]&&b[k]==0){

c[k]=d; b[k]=1;

}}

}a=c[0];y=0;for(d=1;d<fr;d++){

if (a>c[d]) {

a=c[d]; y=d;

}}f[y]=r[l];for(s=0;s<fr;s++)printf(“%d”,f[s]);printf(“\n”);p++;for(d=0;d<fr;d++){

b[d]=0; c[d]=0;

}}if(x==1)x=0;

}p=p-fr;printf(“no of page faults are %d”,p);getch( );

}

Page 15: Os Programs

INPUT:Enter the no of reference paes 12Enter the frames 32 3 2 1 5 2 4 5 3 2 5

OUTPUT:2 0 0 2 3 02 3 12 5 12 5 43 5 43 5 2no of page faults are 4

Page 16: Os Programs

c)LEAST FREQUENTLY USED

#include<stdio.h>#include<conio.h>void main ( ){

int no,k,a,b,n,fr[30],m=0,c,p[50],pf=0,r[30],l,min,x;clrscr( );printf(“enter the no of frames”);scanf(“%d”,&n);printf(“enter the no of pages:”);scanf(“%d’,&no);printf(“enter the sequence of pages:”);for(a=0;a<no;a++)

scanf(“%d”,&p[a]);for(a=0;a<no;a++){

b=0,c=0;while (b<n){

if(p[a]!=fr[b])c++;b++;

}if(c==n){

l=0;for(k=0;k<n;k++){

c=0;for(x=a+1;x<no;x++){

if(fr[k]==p[x]) c++;

}r[l++]=c;

} min=100;for(k=0;k<n;k++){

if (min>r[k]) x=k; min=r[k];

}if(m<n)

Page 17: Os Programs

fr[m++]=p[a];else{

fr[x]=p[I];pf++;

}display(n,fr);}else{

display(n,fr);}

}printf(“|n noof page faults are %d”,pf);getch();

}display(int n,int fr){

int a; for(a=0;a<n;a++) print(“%2d”,fr[a]); printf(‘\n”);

}

INPUT:Enter the no of frames 3Ente the pages 12Enter the sequence of pages 2 3 2 1 5 2 4 5 3 2 5 2 OUPUT2 0 02 3 02 3 02 3 12 3 52 3 54 3 5 4 3 54 3 52 3 52 3 52 3 5no of page faults are 3

Page 18: Os Programs

SIMULATE PAGING TECHNIQUE OF MEMORY MANAGEMENT.

#include<stdio.h>#include<conio.h>#include<math.h>void main(){

int L[20],p[20],pt[20][10],a[6],b[10],i,j,m=0,n;clrscr();printf(“Enter 16 bit logical address”);for(i=0;i<16;i++)scanf(“%d”,&L[i]);for(i=0;i<16;i++)a[i]=L[i];for(i=0;i<16;i++)p[i]=L[i];for(i=5,j=0;i>=0;i--,j++)mt=a[i]*pow(2.0,j)printf(“page table address”);for(i=0;i<6;i++)printf(“%d”,a[i]);printf(“%d”,m);printf(“enter the page table size”);scanf(“%d”,&n);printf(“Enter %d bit string in page table of size of each string is 6”,n);for(i=0;i<n;i++){

b[i]=iprintf(“b[%d]=%d”,i,i);for(j=0;j<6;j++)scanf(“%d”,&pt[i][j]);

}for(i=0;i=n;i++)if(m==b[i]){

for(i=0;i<6;i++)p[i]=pt[m][i];

}printf(“the 16 bit physical address is “);for(i=0;i<16;i++)printf(“%d”,p[i]);

}INPUT:

Page 19: Os Programs

Enter 16 bit logical address : 0000010111011110 Page table address: 000001 Enter the page table size 3

OUTPUT:3 bit string in page table of size of each string is 6 : b[0]=0 000101 b[1]=1 000110 b[2]=2 000111 The 16 bit physical address is 0001100111011110

Page 20: Os Programs

SIMULATE BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE.

#include<stdio.h>#include<conio.h>viid main(){

int res[10],v[10],c[10][10],a[10][10],ca[10][10],tot[10],p=0;int r,I,j,n,flag,flag1,flag3[10],pos,count=0,r=0,order[10];clrscr();printf(“enter no. of processes\n”);scanf(“%d”,&n);for(i=0;i<n;i++){

flag3[i]=0;order[i]=0;

}printf(“enter the no.of resources\n”);scanf(“%d”,&r);printf(“enter the instances of each resource\n”);for(i=0;i<r;i++){

scanf(“%d”,&res[i]);}printf(“enter the max required from each process from each resorce”);for(i=0;i<n;i++){

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

scanf(“%d”,&c[i][j]);}

}printf(“\n enter the current allocation of resources “);for(i=0;i<n;i++){

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

scanf(“%d”,&a[i][j]);}

}start:

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

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

Page 21: Os Programs

ca[i][j]=c[i][j]-a[i][j];}

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

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

tot[i]=tot[i]+a[j][i];}

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

v[i]=res[i]-tot[i];}for(i=0;i<r;i++){

if(res[i]<tot[i]){

flag=0;goto out;

}else

flag=1;}out: for(i=0;i<r;i++)

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

if(res[j][i]>tot[i])flag1=0;goto next;

}else

flag1=1;}

}next:

if((flag==1)&&(flag1==1)printf(“the system is in safe state”);elseif((flag==0)||(flag1==0)){

printf(“the system is in unsafe state”);goto next;

}

Page 22: Os Programs

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

count=0;for(j=0;j<r;j++){

if((ca[i][j]<=v[i]&&flag3[i]==0))count++;

}if(count==r){

pos=i;flag3[pos]=1;goto cout;

}}cout:

order[p]=pos;p++;printf(“process %dis completed”,pos);for(i=0;i<r;i++){

v[i]=v[i]+a[pos][i];}for(i=0;i<r;i++){

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

c[pos][j]=0;ca[pos][j]=0;a[pos][j]=0;

}}printf(“C-A matrix “);for(i=0;i<n;i++){

printf(“\n”);for(j=0;j<r;j++){

printf(“%d”,ca[i][j]);

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

if(flag3[i]==1)t++:if(t==n)

Page 23: Os Programs

goto exit;elsegoto start;

}exit:

printf(“order of execution is”);for(i=0;i<n;i++)printf(“%d”,order[i]);

}getch();

}Output:

Enter the no of process4Enter the no of resources3enter the instances of each resource9 3 6enter the max required from each process for each resourece 3 2 2 6 1 3 3 1 4 4 2 2 enter the current allocation of resources0 0 6 1 2 2 1 1 0 0 2 The available matrix is 0 1 1 process 1 completed after completion of the 1 process available vecteor is 6 2 3 The available matrix is 6 2 3 process 0 completedafter completion of the 0 process available vecteor is 7 2 3 The available matrix is 7 2 3

process 2 completedafter completion of the 2 process available vecteor is 9 3 4

The available matrix is 9 3 4 process 3 completedafter completion of the 3 process available vecteor is 9 3 6

Order of execution of process 1 0 2 3

Page 24: Os Programs

BANKERS ALGORITHM FOR DEADLOCK PREVENTION

#include<conio.h>#include<stdio.h>#include<stdlib.h>void main(){

int max = 10; int i, j, z;int P, L, N; int A[max], R[max], M[max]; int RS[max]; int LS[max]; int CP[max]={0}; int MAX[max];int lim;

do{

printf("\HOW MANY PROCESSES DO YOU WANT? ");scanf(“%d”,&P);

}while(P>max);

printf("\nINSERT THE MULTEPLICITY OF THE RESOURCE: ");scanf(“%d”,&N);

for(i=0;i<P;i++){printf("\nHOW MANY UNIT HAS THE P %d PROCESS? ",i);scanf(“%d”,&A[i]);printf("\nHOW MANY UNIT REQUEST THE P %d PROCESS? ",i);scanf(“%d”,&R[i]);printf("\nWHAT IS THE MAX NUMBER OF UNITS OF THE P %dPROCESS? ",i);scanf(“%d”,&M[i]);printf("\n\n");}

L=N;for(i=0; i<P; i++)L-=A[i];

for(i=0; i<P; i++){if(R[i]<=L)

Page 25: Os Programs

{RS[i]=M[i]-A[i]-R[i];

LS[i]=L-R[i]; }else {LS[i]=-1;}}

for(i=0; i<P; i++)MAX[i]=-1; z=0;for(i=0; i<P; i++){if(i==0){for(j=0; j<P; j++){if(LS[j]>=MAX[z]){MAX[z]=LS[j];CP[z]=j;

lim=MAX[z];}}}else{for(j=0; j<P; j++){if((LS[j]>=MAX[z])&&(LS[j]<lim)){MAX[z]=LS[j];CP[z]=j;

lim=MAX[z];}}}

z++;}

Page 26: Os Programs

for(i=0; i<P; i++){if(LS[i]!=-1)printf("\nTHE PROCESS NUMBER %d IS THE ",i,CP[i];elseprintf("THE PROCESS NUMBER %d IS NOT SECURE",i);}

getch();}

Page 27: Os Programs

MULTI PROGRAMMING WITH FIXED NUMBER OF TASKS

#include<stdio.h>void main(){

int n,s,p_size,I,flag[100],k=-1,I_flag=0;char c,name[100];printf(“Enter the number of partitions”);scanf(“%d”,&n);printf(“Enter the size of each partition”);scanf(“%d”,&s);do{

printf(“Enter the process name”);scanf(“%d”,&name):printf(“Enter the size of process”);scanf(“%d”,&p_size);if(p_size<=1){

k++;flag[k]=p_size;

}else

printf(“The size of the process is too large”);if(k>=n-1){

clrscr();printf(“No more partitions are leftout free”);getch();break;

}}printf(“Any more process(y/n) : “);scanf(“%c”,&c);while(c==’y’||c==’Y’);for(I=0;I<=k;I++)I_flag=I_flag+flag[I];clrscr();printf(‘number of partitions occupied is %d”,k+1);printf(“number of partitions free”,n-k-1);printf(“Total internal fragmentation %d”,I_flag);getch();

}

Page 28: Os Programs

OUTPUT: Enter No. of partitions 4 Enter size of each partition 100 Enter the process name p1 Enter the size of the process 80

Any more process(y/n) : nNumber of partitions occupied 1Number of partitions free 3Total internal fragmentation 20

Page 29: Os Programs

MULTIPROGRAMMING WITH VARIABLE NO OF TASKS

#include <stdio.h>void main( ){ int I,h,holes[30],s,c,k,flag=0;

printf(“ente the no of holes”0); scanf(“%d”,&h); printf(“enter the size of the next process:”); scanf(“%d”,&s); printf(“enter the size of the each hole”); for(i=0;i<h;i++) scanf(“%d”,&holes[i]); printf(“select the algorithm from the list below”); printf(“\n 1.first fit 2.best fit 3.next fit”); scanf(‘%d”,&c); flag=0; if(c==1) {

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

if(holes[i]>=s){ printf(“\n the process can be placed in hole :%d”,(i+1));

flag=1;break;

}}

if(flag==0) printf(“\n the process cannot be placed in any hole”);

} else if (c==2) { for (i=0;i<h;i++) if (holes[i]>s) break; k=i; for (i=0;i<k;i++) { if(holes[i]>=s&&holes[i]<holes[k]) {

k=i; flag =1;

} } if(flag==1)

Page 30: Os Programs

printf(“\n process can be placed in hole:%d”,i+1); else

printf(“process cannot be placed in the hole :%d”,i+1); }

else if(c==3) {

printf(“\n enter the hole after previous insertion”);scanf(“%d”,&k);for(i=k-1;i<h;i++){

if(holes[i]>=s) flag=1; printf(“\n process can be placed in the hole %d”i+1); break;

}}if(flag==0)for(i=0;i<k;i++){

if(holes[i]>=s){

flag=1;printf(“\n the process can be placed in the hole :%d”,i+1);break;

}}

}if (flag ==1)printf(“ the process cannot be placed in the hole”);getch( );}

Page 31: Os Programs