You are on page 1of 25

Practical : 1 /* Program to implement problem of First Come First Serve */ Source Code

#include<stdio.h> #include<conio.h> int main() { int jb; float brstime[10]; float strtime[10]; int i,j; float wtngtime=0; float avg; printf("enter num of process "); scanf("%d", &jb); for(i=0;i<jb;i++) { printf("enter cpu burst time for process %d\t",i+1); scanf("%f",&brstime[i]); } i=0; strtime[i]=0; for(i=1;i<jb;i++) { strtime[i]=brstime[i-1]+strtime[i-1]; wtngtime+=strtime[i]; } avg=wtngtime/jb; printf("Avg waiting time %f ",avg); getch(); return 0; }

Output

&jb). } for(x=0.x++) { scanf("%d".j<jb.s=0.h> int main() { int i.cb[x]).p[100]. printf("CPU Burst time\n").x<jb. .j++) { i=j-1.sum=0.&cb[x]). for(x=0. key=cb[j].x.cb[100]. while(i>-1 && cb[i]>key) { cb[i+1]=cb[i].x++) { s=s+cb[x]. } for(x=0.x<jb.x<jb-1. printf("Enter number of job").h> #include<conio.jb. for(j=1.Practical : 02 /* Program to implement problem of Shortest Job First */ Source Code #include<stdio. float avg. i--. printf("burst time before sorting\n"). } printf("Burst time after sorting\n").x++) { printf("%d\n".j. } cb[i+1]=key. scanf("%d".key.

} Output .} avg=s/(float)jb. return 0. printf("avg waiting time is:=%f\n".avg). getch().

lat[10]. .wav=0.taav.s. printf("Enter the number of process : "). ct[p]=0. lat[p]=t.w[10].taav=0.h> #include<conio.&n).ta[10].i. return 1.i++) if(ct[i]>0) return 0.bt[10]. printf("\n Enter the time slice : "). int allover() { for(int i=0. } if(ct[p]==0) ta[p]=t.h> int t. } void select(int p) { w[p]+=t-lat[p].ct[10].t=0. } int main() { int p=0. scanf("%d". } else { t+=ct[p].i<n.Practical : 03 /* Program to implement Round Robin Algorithm */ Source Code #include<stdio. t+=s.n.h> #include<iostream.wav. if(ct[p]>=s) { ct[p]-=s.

taav).wav).i<n. ct[i]=bt[i]. p=(p+1)%n. printf("\n\nAverage waiting time : %d ". for(i=0.i++) { printf("\n %d\t\t%d\t\t%d\t\t%d". } while(!allover()) { if(ct[p]!=0) select(p).bt[i].&bt[i]). printf("\n\nAverage turn around time : %d ".scanf("%d". taav/=n.ta[i]).i++) { printf("\n process %d : ". } wav/=n. getch().i<n. return 0. for(i=0. scanf("%d". } . } printf("\n\n Process Burst time Wait time Turn around \n\n"). printf("\nEnter the burst time of processes \n ").w[i]. wav+=w[i].i+1). taav+=ta[i].&s). lat[i]=0.i+1.

Output .

i. }} cout<<"Enter total no of each resource:". cin>>r. cin>>p. cout<<"Enter the claim for each process:".prc. for(i=1.req[7][5].i++) { cout<<"\nFor process "<<i<<":".count.r.avail[5]. cout<<"Enter the no of processes:".alloc[7][5].i++) comp[i]=0. }} cout<<"Enter the allocation for each process:".j<=r.j<=r.j<=r.i<=p.j<=r. for(j=1. for(i=1. for(j=1.i++) { cout<<"\nFor process "<<i<<":". int first.t.Practical : 04 /* Program to implement Banker’s Algorithm Source Code #include<iostream. for(j=1. count=0.j++) cin>>rsrc[j].h> #include<conio.j++) { cin>>clm[i][j].j++) { */ .comp[7]. for(j=1.j.h> int main() { int clm[7][5].rsrc[5]. cout<<"Enter the no of resources:".j++) { cin>>alloc[i][j].p.i<=p. for(i=1.i<=7.

} do { for(i=1.j<=r.i++) { if(comp[i]==0)//if not completed { prc=i. } prc=0. int total=0.j<=r.i<=p.i<=p.i++) { for(j=1.} avail[j]=rsrc[j]-total. for(j=1.j++) { cout<<alloc[i][j]<<" ". }} cout<<"\n\nAvailable resorces is:". } cout<<"\n". for(i=1.j++) { req[i][j]=clm[i][j]-alloc[i][j].i++) {total+=alloc[i][j].i<=p.j<=r. } cout<<"\t\t\t".i++) { for(j=1.break. for(i=1. } cout<<"\nClaim matrix:\t\tAllocation matrix:\n". for(j=1. } }} if(prc!=0) break. } if(prc!=0) { . for(i=1.j++) { cout<<clm[i][j]<<" ".avail[j]=0.j<=r.j++) { if(avail[j]<req[i][j]) { prc=0.j<=r.j++) { cout<<" "<<avail[j]. for(j=1.i<=p.

} . getch().cout<<"\nProcess "<<prc<<"runs to completion!". for(j=1. comp[prc]=1. } } } while(count!=p&&prc!=0).j<=r. return 0. clm[prc][j]=0. if(count==p) cout<<"\nThe system is in a safe state!!".j++) { avail[j]+=alloc[prc][j]. count++. else cout<<"\nThe system is in an unsafe state!!". alloc[prc][j]=0.

Output .

scanf("%d %d".pt[10].Practical : 05 /* Program to implement Priority Scheduling Algorithm*/ Source Code #include<stdio.&n). pt[j]=x. printf("\nGREATER NUMBER GREATER PRIORITY\n"). p[i]=i+1. p[j]=x. pp[i]=pp[j].pp[10]. pt[i]=pt[j].i+1).&pp[i]). .j<n.w[10].i.j++) { if(pp[i]<pp[j]) { x=pp[i]. } for(i=0. printf("\n Enter Burst Time & Time priorities \n").&pt[i]. awt=0. x=pt[i]. printf("\nEnter the number of process : "). p[i]=p[j]. for(i=0.i++) { for(int j=i+1. x=p[i]. } } } w[0]=0.p[10]. pp[j]=x.awt.h> #include<conio.i++) { printf("\nProcess no %d : ". scanf("%d".i<n.i<n-1.atat.h> #include<iostream.h> int main() { int x.n.t[10].

atat+=t[i]. t[i]=w[i]+pt[i]. for(i=1. atat/=n. for(i=0.i++) { w[i]=t[i-1]. awt+=w[i].p[i]. } Output .pt[i].t[0]=pt[0]. awt/=n. printf("\n Average Turn Around Time : %d \n".w[i].i<n. printf("\n Average Wait Time : %d \n". return 0. atat=t[0].pp[i]).atat).t[i].awt).i<n. } printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n").i++) printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n". getch().

0.Frame[1].7. printf("\n\nList of pages:>\n").i++) { printf("%d ".f1=0. int temp[3].1.Practical : 06 /* Program to implement LRU Page Replacement Algorithm*/ Source Code #include<stdio.2. // Frame printf("%d %d %d \n". for(i=3.pagefault=0.Frame[0].Place. if(Frame[j]==Page[i]) // Checking whether the current page is in Frame { f=1.//Printing pages } printf("\n\nList of Content of frame\n").4.0.3.f0=0.f2=0.j<3.0.i<20.1.0. // first three pages to the Frame[2]=Page[2]. Frame[0]=Page[0].j++) { f=0.break. int Frame[3].3.2.0.f=0.k.Page[i]). //Set of pages printf("Defining a three frame architecture").0.Frame[2]). } } if(f!=1) //If not { .i<20. for(i=0.l. //Defining a three frame architecture int Page[20]={7.2.i++) { for(j=0.3. // Initialising Frame[1]=Page[1].1}.j.2.h> int main() { int i.1.

//Resetting all the flag variables } printf("\n\n\tNumber of page faults <%d>\n".break.}// page to be if(f2==0 && Page[k]==Frame[2]){f2=1.} // caculating the page fault } printf("%d %d %d \n".} else if(f1==1 && f2==1){Frame[0]=Page[i].for(k=i-1.}//replaced if(f0==1 && f1==1){Frame[2]=Page[i].k--) { if(f0==0 && Page[k]==Frame[0]){f0=1.k>=0.} //Checking the if(f1==0 && Page[k]==Frame[1]){f1=1.pagefault++.Frame[1].break.f1=0.Frame[2]).f2=0.pagefault++.pagefault).} else //Replacing the least if(f0==1 && f2==1){Frame[1]=Page[i].pagefault++. //Printing the number of page faults } Output .break. //Printing the content of the frame } f0=0.f=0.Frame[0].

i<3.index.5.h> #include<conio.l.Practical : 07 /* Program to implement Optimal Page Replacement Algorithm*/ Source Code #include<stdio.pf=0.5. . int p[12]={2. } } if(flag1==0) { for(i=0.4.2. } for(j=0.1.3. break.k. for(i=0. printf("\nOptimal Page Repalcement for 3 Page Frames\n"). flag2=0.i<3.2.i. flag2=1.3.i++) { fr[i]=-1.i<3.2}.frsize=3.j<12.i++) { if(fr[i]==p[j]) { flag1=1.lg[3]. flag2=1. printf("\n -1 Represents no page present in frame\n\n").fs[3]. int main() { void display(). int max.found=0.j++) { flag1=0.h> int fr[3].flag2=0.2.j.5. for(i=0.i++) { if(fr[i]==-1) { fr[i]=p[j].flag1=0.

} } } found=0. index=0. for(i=0. break.i<frsize. } } } if(flag2==0) { for(i=0.i++) { if(max<lg[i]) { max=lg[i]. } } if(found==0) { max=lg[0]. index=i.i++) lg[i]=0.i<frsize.i<3.k<12.break. .i++) { if(lg[i]==0) { index=i.i<frsize. break.k++) { if(fr[i]==p[k]) { lg[i]=k-j. found=1. for(i=1.i++) { for(k=j+1. for(i=0.

getch(). for(i=0. return(0).i<3.} } } fr[index]=p[j]. } printf("\n no of page faults:%d".pf). } Output .fr[i]). printf("\n").i++) printf("\t%d". pf++. } display(). } void display() { int i.

j++) { flag1=0. flag2=0. printf("\n -1 Represents No page present in frame\n").page[12]={2.2.h> #include<conio. for(i=0.i++) { fr[i]=-1.pf=0. } } if(flag1==0) { for(i=0.1.i<12.i<frsize.h> int fr[3]. break. flag2=1. printf("\nFrame Size is 3 Total Page 12\n"). } . break.Practical : 08 /* Program to implement FIFO Page Replacement Algorithm*/ Source Code #include<stdio. flag2=1.frsize=3.2. } for(j=0. for(i=0.j.5.i<3.2.i++) { if(fr[i]==page[j]) { flag1=1.j<12.2}.i++) {if(fr[i]==-1) {fr[i]=page[j].3. int i.3.5.5. int main() { void display().top=0.flag2=0.4. int flag1=0.

i<3.fr[i]). } void display() { int i.i++) printf("%d\t". top++. for(i=0. pf++. } printf("Number of page faults : %d ". if(top>=frsize) top=0.} } if(flag2==0) { fr[top]=page[j]. printf("\n"). } Output . } display(). getch(). return 0.pf).

scanf("%d".i++){ for(j=i+1.init_head.j++) head_mov=head_mov+(queue[j+1]-queue[j]). queue[i]=queue[i]-queue[j].j<n. printf("Enter the %d requests:". for(i=0.j++){ if(queue[i]>queue[j]){ queue[i]=queue[i]+queue[j]. head_mov=head_mov+(cylin-queue[j]).i<n-1.h> int main() { int queue[10]. . head_mov=head_mov+(cylin-queue[pos-1]).&init_head). queue[j]=queue[i]-queue[j]. printf("Enter the number of cylinders:").i++){ if(queue[i]>init_head){ pos=i.n.&cylin). } } head_mov=head_mov+(queue[pos]-init_head).Practical : 09 /* Program to implement Scan Disk Scheduling Algorithm*/ Source Code #include<stdio.pos.cylin. scanf("%d".n). for(i=0.j<n-1.&n). break.i<n.&queue[i]). } } } for(i=0.i++) scanf("%d".i<n. printf("Enter the number the number of requests:").j. scanf("%d". printf("Enter the initial head position:").h> #include<conio.i. for(j=pos.head_mov=0.

j>0. printf("\n\nTotal Head Movements = %f". float f=head_mov/(float)n.for(j=pos-1.j--) head_mov=head_mov+(queue[j]-queue[j-1]).head_mov). getch(). return 0. printf("\nTotal Head Movements = %d\t".f). } Output .

queue[i]=queue[i]-queue[j].&cylin).cylin.j<n. printf("Enter the number the number of requests:"). for(i=0.j.n). printf("Enter the %d requests:".i++){ for(j=i+1.n. printf("Enter the initial head position:").j++){ if(queue[i]>queue[j]){ queue[i]=queue[i]+queue[j]. } } } for(i=0.i<n.init_head.&n).i<n.pos. printf("Enter the number of cylinders:").i.i<n-1.head_mov=0. scanf("%d".&init_head).Practical : 10 /* Program to implement C-Scan Disk Scheduling Algorithm*/ Source Code #include<stdio. scanf("%d". queue[j]=queue[i]-queue[j].&queue[i]).h> #include<conio.i++) scanf("%d". } } .h> int main() { int queue[10]. break. scanf("%d".i++){ if(queue[i]>init_head){ pos=i. for(i=0.

} Output . float f=head_mov/(float)n.j<n-1. getch(). printf("Total Head Movements = %d".head_mov). head_mov=head_mov+(cylin-queue[j]). return 0. printf("Average Head Movements = %f".j++) head_mov=head_mov+(queue[j+1]-queue[j]).f).head_mov=head_mov+(queue[pos]-init_head). for(j=pos.

6.S.No 1. 5. 7. 2. 10. 9. Practical Name First Come First Serve Shortest Job First Round Robin Banker’s Algorithm Priority Scheduling LRU Page Replacement Optimal Page Replacement FIFO Page Replacement SCAN Disk Scheduling Algorithm C-Scan Disk Scheduling Algorithm Date Sign . 8. 3. 4.