0% found this document useful (0 votes)
109 views27 pages

Program No: 1 WAP To Implement First Come First Serve (FCFS) Process Scheduling

The document contains code for implementing LRU page replacement algorithm. It takes number of frames and string length as input. It initializes frame and frequency array. Frequency array stores time since frame was last used. It checks if string element is already in frame, if yes it updates frequency of that frame as 0. If not, it replaces LRU frame using max frequency value and updates frequency of replaced frame as 0. After each loop, it increments frequency of all frames. In the end, it prints frames and calculates page faults.

Uploaded by

Amitoz Rathore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views27 pages

Program No: 1 WAP To Implement First Come First Serve (FCFS) Process Scheduling

The document contains code for implementing LRU page replacement algorithm. It takes number of frames and string length as input. It initializes frame and frequency array. Frequency array stores time since frame was last used. It checks if string element is already in frame, if yes it updates frequency of that frame as 0. If not, it replaces LRU frame using max frequency value and updates frequency of replaced frame as 0. After each loop, it increments frequency of all frames. In the end, it prints frames and calculates page faults.

Uploaded by

Amitoz Rathore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

Program No: 1 WAP to implement First Come First Serve (FCFS) process scheduling

#include<stdio.h> #include<conio.h> #include<process.h> void main() { char p[10][5]; int tot=0,wt[10],i,n; float avg=0; clrscr(); printf( ******** FIRST COME FIRST SERVE SCHEDULING ALGORITHM ********); printf("\n\nenter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:\n",i+1); scanf("%s",&p[i]); printf("enter process time"); scanf("%d",&pt[i]); } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; tot=tot+wt[i]; } avg=(float)tot/n; printf("p_name\t P_time\t w_time\n"); for(i=0;i<n;i++) printf("%s\t%d\t%d\n",p[i],et[i],wt[i]); printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

OUTPUT:

Program No: 2 WAP to implement Round Robin process scheduling

#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> void main() { char p[10][5]; int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m; float avgwt; clrscr(); printf(" ************ ROUND ROBIN SCHEDULING ALGORITHM *********** \n\n"); for(i=0;i<n;i++) { printf("enter the process name : "); scanf("%s",&p[i]); printf("enter the processing time : "); scanf("%d",&pt[i]); } m=n; wt[0]=0; i=0; do { if(pt[i]>timer) { rt=pt[i]-timer; strcpy(p[n],p[i]); pt[n]=rt; et[i]=timer; n++; } else { et[i]=pt[i]; } i++; wt[i]=wt[i-1]+et[i-1]; }while(i<n); count=0; for(i=0;i<m;i++) { for(j=i+1;j<=n;j++) { if(strcmp(p[i],p[j])==0) { count++; found=j; } } if(found!=0)

{ wt[i]=wt[found]-(count*timer); count=0; found=0; } } for(i=0;i<m;i++) { totwt+=wt[i]; } avgwt=(float)totwt/m; printf("\np_name p_time w_time"); for(i=0;i<m;i++) { printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]); } printf("\ntotal waiting time %d\n",totwt); printf("total avgtime %f",avgwt); getch(); } OUTPUT:

Program No: 3 WAP to implement Priority scheduling

#include<stdio.h> #include<conio.h> void main() { char p[10][5],temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf(******** PRIORITY SCHEDULING ALGORITHM ********); printf("\n\nenter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j]) { temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]); } printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch();

OUTPUT:

Program No: 4 WAP to implement Shortest Job First (SJF) process scheduling

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

#include<process.h> void main() { char p[10][5],temp[5]; int tot=0,wt[10],pt[10],i,j,n,temp1; float avg=0; clrscr(); printf(******** SHORTEST JOB FIRST SCHEDULING ALGORITHM ********); printf("\n\nenter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:\n",i+1); scanf("%s",&p[i]); printf("enter process time"); scanf("%d",&pt[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pt[i]>pt[j]) { temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; tot=tot+wt[i]; } avg=(float)tot/n; printf("p_name\t P_time\t w_time\n"); for(i=0;i<n;i++) printf("%s\t%d\t%d\n",p[i],et[i],wt[i]); printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

OUTPUT :

Program No: 5 WAP to implement Shortest Remaining Time First (SRTF) process scheduling
#include<conio.h> #include<stdio.h> typedef struct process

{ int id; int arrival; int burst; int wait; }; struct process struct process struct process struct process

pool[10]; ready[10]; exe[20]; temp,current,tmppro;

int scanpro(int time,int nop); arrange(); execute(); incwait1(); incwait2(); int time,x,y,z; float at[10],at1[10]; int main() { int tmp,nop,no; float at_float,awt,atrt; int j,i,maxarrtime; int counter=0; time=0; x=1; y=0; z=0; clrscr(); printf("Please enter the number of processes : "); scanf("%d",&nop); for(i=0;i<nop;i++) at[i]=0; printf("\nPlease enter the ID, Burst time & Arrival Time of each process : "); printf("\n"); for(i=0;i<nop;i++) { printf("\nID : P%d",i+1); pool[i].id=i+1; printf("\nBurst Time : "); scanf("%d",&pool[i].burst); printf("Arrival Time : "); scanf("%f",&at[i]); } for(i=0;i<nop;i++) { at1[i]=at[i]; at_float=at[i]+0.999; pool[i].arrival=(int)at_float; at[i]=(float)pool[i].arrival-at1[i];

} printf("\n* * * * * * * PROCESS TABLE * * * * * * *\n"); printf("\nP ID Burst Time Arrival"); for(i=0;i<nop;i++) { if(pool[i].id==0) break; printf("\nP%d\t\t%d\t%f",pool[i].id,pool[i].burst,at1[i]); } printf("\n\n"); //SRT Logic maxarrtime=pool[nop-1].arrival; while(time<=maxarrtime) { if(time==pool[z].arrival) { no=scanpro(time,nop); for(counter=0;counter<no;counter++) { ready[x]=pool[z]; x++; z++; } execute(); } else { if(current.burst>0) { current.burst--; incwait1(); time++; } else { if(current.burst==0) { execute(); } } } } counter=temp.burst-current.burst; tmp=0; while(current.burst!=0) { current.burst--; counter++;

tmp++; } i=1; while(ready[i].id>0 && ready[i].burst!=0) { ready[i].wait=ready[i].wait+tmp; i++; } temp.burst=counter; exe[y++]=temp; for(i=0;i<10;i++) ready[i]=ready[i+1]; arrange(); incwait2(); x=0; while(ready[x].id>0) exe[y++]=ready[x++]; for(i=0;i<20;i++) exe[i]=exe[i+1]; //setting wait time in pool for(i=0;i<nop;i++) { for(j=0;j<20;j++) { if(pool[i].id==exe[j].id) { pool[i].wait=exe[j].wait; } } at[i]=at[i]+pool[i].wait; } awt=0; atrt=0; for(i=0;i<nop;i++) { awt=awt+at[i]; atrt=atrt+(at[i]+pool[i].burst); } awt=awt/nop; atrt=atrt/nop; printf("\nAverage Waiting Time : %f",awt); printf("\nAverage Turn Around Time : %f",atrt); getch(); return 0; } execute() { ready[0]=current;

arrange(); temp.burst=temp.burst-current.burst; exe[y++]=temp; current=ready[0]; temp=ready[0]; current.burst--; incwait1(); time++; } incwait1() { int i=1; while(ready[i].id>0 && ready[i].burst!=0) { ready[i].wait++; i++; } } incwait2() { int i=1; int wt=0; wt=ready[0].burst; while(ready[i].id>0 && ready[i].burst!=0) { ready[i].wait=ready[i].wait+wt; wt=wt+ready[i].burst; i++; } } int scanpro(int time,int nop) { int i; int counter=0; for(i=0;i<nop;i++) if(pool[i].arrival==time) counter++; return counter; } arrange() {

int a,i,j,counter=0; for(i=1;i<10;i++) { for(j=0;j<10-i;j++) { if(ready[j].burst>ready[j+1].burst) { tmppro=ready[j]; ready[j]=ready[j+1]; ready[j+1]=tmppro; } } } i=0; while(ready[i].burst==0) { counter++; i++; } a=0; for(i=counter;i<10;i++) { ready[a++]=ready[i]; ready[i].id=0; ready[i].burst=0; } getch(); }

OUTPUT OF SRTF ALGORITHM:

Program No: 6 WAP to simulate Bankers Deadlock Avoidance Algorithm

#include<stdio.h> #include<conio.h> #define mx 20 void main() { int avail[mx],safe[mx],p,r,req[mx],max[mx][mx]; int alloc[mx][mx],need[mx][mx],work[mx],finish[mx],i,j,m,n,k,x; clrscr(); printf("Enter the no. of resources and processes:"); scanf("%d%d",&n,&m); printf("Enter available instances of %d resources",n); for(j=0;j<n;j++) { scanf("%d",&avail[j]); work[j]=avail[j]; } printf("Enter the max instances for processes\n"); for(i=0;i<m;i++) { printf("P%d: ",i); for(j=0;j<n;j++) scanf("%d",&max[i][j]); } printf("Enter the allocated instances for processes\n"); for(i=0;i<m;i++) { printf("P%d: ",i); for(j=0;j<n;j++) { scanf("%d",&alloc[i][j]); need[i][j]=max[i][j]-alloc[i][j]; } } for(i=0;i<m;i++) { finish[i]=0; for(j=0;j<n;j++) { if(need[i][j]<=work[j]) { work[j]=work[j]+alloc[i][j]; finish[i]=1; } } } if(finish[i]==1) printf("THE SYSTEM IS IN THE SAFE STATE\n"); getch(); }

OUTPUT:

Program No: 7 WAP to implement FIFO Page Replacement Algorithm

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

{int c,in,i,j,k,f[10],a[21],n,p,m; clrscr(); printf("enter the no. of frames"); scanf("%d",&k); printf("enter the length of string"); scanf("%d",&n); printf("enter the %d string enteries",n); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=k;i++) f[i]=-1; p=0; in=1; for(i=1;i<=n;i++) {c=0; for(j=1;j<=k;j++) { if(a[i]==f[j]) c=1; } if (c==1) p=p; else {f[in]=a[i]; p++; if(in==k) in=1; else in++; } printf("\nelements in frame after elmnt%d entry:\t",i); for(m=1;m<=k;m++) {if(f[m]==-1) printf("NIL\t"); else printf("%d\t",f[m]); } } printf("\npage faults are %d\n",p); printf("page fault rate is %f",p/(float)k); getch(); }

OUTPUT:

Program No: 8 WAP to implement LRU Page Replacement Algorithm

#include<stdio.h> #include<conio.h> void main() {int ft,c,si,in,i,j,k,f[10],a[20],n,p,m,fi[10],ni; clrscr(); printf("enter the no. of frames\t"); scanf("%d",&k); printf("enter the length of string\t");

scanf("%d",&n); printf("enter the %d string enteries:\t",n); for(i=1;i<=n;i++) {scanf("%d",&a[i]);} for(i=1;i<=k;i++) //initial entries in the frame enteries are set to -1 {f[i]=-1; fi[i]=0;} //fi[i] will store value that indicate the time since wen f[i] isnt used p=0; ft=1; si=0; //p will store no. of page faults

for(i=1;i<=n;i++) {c=0; //c will check whether new string is already in frames or not for(j=1;j<=k;j++) {if(a[i]==f[j]) {c=1; si=j;}} if (c==1) {p=p; //if c=1 i.e. new entry is already in the frames then page faults=p remain same fi[si]=0;} else {if(f[i]==-1) //till there is a nil entry in frames independent of LRU we first fill them {f[ft]=a[i]; ft++;} else {in=selmax(fi,k); //select the LRU frame having highest value of fi[i] f[in]=a[i]; fi[in]=0;} p++;} for(ni=1;ni<=k;ni++) //after each loop fi[i] enteries of each f[i] is incremented by 1,more is fi[i] thats mean that frame hvnt used for longest time or is LRU {fi[ni]++;} printf("\nelements in frame after elmnt%d entry:\t",i); for(m=1;m<=k;m++) {if(f[m]==-1) printf("NIL\t"); // where no entry is there nil will get printed else printf("%d\t",f[m]);} } printf("\npage faults are %d",p); printf("\npage fault rate is %f",p/(float)k); //page fault rate = page faults/no. of frames getch();}

int selmax(int fim[10],int num) {int t,i,ch; t=fim[1]; ch=1; for(i=2;i<=num;i++) {if(t<fim[i]) {t=fim[i]; ch=i;}} return ch; } OUTPUT:

//select the LRU frame N returns its index

Program No: 9 WAP to implement Optimal Page Replacement Algorithm

#include<stdio.h> #include<conio.h> int a[21],f[8],n,k; void main() {int ft,c,i,j,p,m,min; clrscr(); printf("enter the no. of frames\t"); scanf("%d",&k);

printf("enter the length of string\t"); scanf("%d",&n); printf("enter the %d string enteries\t",n); for(i=1;i<=n;i++) {scanf("%d",&a[i]);} for(i=1;i<=k;i++) {f[i]=-1;} p=0; ft=1; for(i=1;i<=n;i++) {c=0; for(j=1;j<=k;j++) {if(a[i]==f[j]) {c=1;}} if (c==1) {p=p;} else {if(f[i]==-1) //till there is frame with nil enteries we jst enter string in that empty frame {f[ft]=a[i]; ft++;} else {min=selminp(i); //min=index of frame wich will not be used for longest period of time f[min]=a[i];} p++; } printf("\nelements in frame after %d iteration:\t",i); for(m=1;m<=k;m++) {if (f[m]==-1) printf("NIL\t"); else printf("%d\t",f[m]);} } printf("\npage faults are %d",p); printf("\npage fault rate is %f",p/2.0); getch(); } int selminp(int curposit) //frame which will get used first in remaining string is assigned the largest no. {int i,m,futp[10],ch,t,c; //& if not present in rem. string then assigned -1 value for(i=1;i<=k;i++) for(m=1;m<=k;m++) {c=0; for(i=curposit+1;(i<=n)&&(c==0);i++) {if (f[m]==a[i]) {futp[m]=n-i+1; c=1;}

else {futp[m]=-1;} } } t=futp[1]; ch=1; for(i=2;i<=k;i++) for longest period of timme {if(t>futp[i]) {t=futp[i]; ch=i;}} return ch; } OUTPUT: //return the index of frame wich will not be used

Program No: 10 WAP to implement simulation of Counting Semaphores

#include<conio.h> #include<stdio.h> void signal(); void wait(); int count=1; void main() { int n;

clrscr(); getch(); do {printf("\nenter 1 if process want to enter in cs enter\nenter 2 if process want to leave cs\nenter 3 to exit "); scanf("%d",&n); if(n==1) {wait();} else if(n==2) signal(); else if(n==3) exit(); } while(1); } void wait() { count--; if(count<0) { printf("process is linked into the queue at %d",count); } else printf("process executing in cs"); } void signal() { printf("process completed execution in cs\n"); count++; if(count<=0) printf("process next in queue can execute"); }

OUTPUT:

Program No: 11 WAP to implement Producer-Consumer Problem

#include<stdio.h> #include<conio.h> void main() {int n,buffer,p,i,j,k,r; p=5; buffer=0; clrscr(); getch(); do {k=0; printf("\nPlese select from following \n enter 1 to produce \n enter 2 to consume \n enter 3 to exit"); scanf("%d",&n); if(n==1) {if(buffer>p) printf("u have to wait as buffer is full"); else {printf("how many item u want to produce"); scanf("%d",&r); for(j=1;j<=r;j++) {buffer++; if(buffer>p) break; k=j; } if(k==0) printf("buffer is full"); else printf("%d item being produce",k); }} else if(n==2) { printf("how many item u want to consume"); scanf("%d",&i); if(buffer<1) printf("no more item in buffer"); else {for(j=1;j<=i;j++) {k=j; buffer--; if(buffer<1) break; } if(k==0) printf("no more item in buffer"); else printf("%d item being consumed",k); }} else if(n==3)

exit(); } while(1); }

OUTPUT:

INDEX

S.NO DATE .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 07/09/2012 07/09/2012 14/09/2012 14/09/2012 14/09/2012 21/09/2012 21/09/2012 21/09/2012 21/09/2012 12/10/2012

PROGRAMS
Program to implement First Come First Serve (FCFS) Scheduling Algorithm Program to implement Round Robin Scheduling Algorithm Program to implement Priority Scheduling Program to implement Shortest Job First (SJF) Scheduling Algorithm Program to implement Shortest Remaining Time First (SRTF) Scheduling Algorithm Program to simulate Bankers Deadlock Avoidance Algorithm Program to implement FIFO Page Replacement Algorithm Program to implement LRU Page Replacement Algorithm Program to implement Optimal Page Replacement Algorithm Program to implement simulation of Counting Semaphores Program to implement Producer-Consumer Problem

REMARKS

11.

12/10/2012

You might also like