User:Harsh019/sandbox

FCFS :


 * 1) include
 * 2) include

int main {	int bt[4],tt[4],i,j=0; float awt=0,att=0;

clrscr;

printf("Enter burst time for process : \n"); for(i=0;i<4;i++) {		printf("",i+1); scanf("%d",&bt[i]); }

for(i=0;i<4;i++) {		j=j+bt[i]; tt[i]=j; }

for(i=0;i<4;i++) att=att+tt[i]; att=att/4; printf("Average turn around time = %.2f",att);

for(i=w;i<4;i++) awt=awt+tt[i]; awt=awt/4; printf("Average waiting time = %.2f",awt); return 0; }

SJF :


 * 1) include
 * 2) include

void main {	int bt[20],p[20],wt[20],tot[20],i,j,n,total=0,pos,temp; float awt=0,att=0;

clrscr;

printf("Enter number of process : "); scanf("%d",&n); printf("Enter burst time for process : \n"); for(i=0;i",i+1); scanf("%d",&bt[i]); p[i]=i+1; }

for(i=0;i\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tot[i]); }	att=total/n; printf("Average waiting time = %.2f",awt); printf("Average turn around time = %.2f",att); }

Priority scheduling :


 * 1) include
 * 2) include

void main {	int p[5]={1,2,3,4,5},bt[5],t[5],pr[5],i,j,temp=0; float awt=0,att=0;

clrscr; flushall;

printf("Enter  for process : \n"); for(i=0;i<5;i++) {		printf("",i+1); scanf("%d %d",&bt[i],&pr[i]); }

for(i=0;i<5;i++) {		for(j=0;j<i;j++) {			if(pr[i]<pr[j]) {				sort :

temp=pr[i]; pr[i]=pr[j]; pr[j]=temp;

temp=bt[i]; bt[i]=bt[j]; bt[j]=temp;

temp=p[i]; p[i]=p[j]; p[j]=temp; }			if(pr[i]==pr[j] && (P[i]<p[j])) goto sort; }	}

temp=0; for(i=0;i<5;i++) {		temp=temp+bt[i]; t[i]=temp; }

for(i=0;i<5;i++) {		if(i==4) goto turn; awt=awt+t[i]; turn : att=att+t[i]; }	att=att/5; awt=awt/5; printf("Gnatt Chart : \n\n       Priority: "); for(i=0;i<5;i++) printf("%4d ",pr[i]); printf("\n\t\t"); for(i=0;i<5;i++) printf(" -"); printf("\n Process : "); for(i=0;i<5;i++) printf("|%3d    ",p[i]); printf("|\n\t\t"); for(i=0;i<5;i++) printf(" -"); printf("\n  time :       0"); for(i=0;i<5;i++) printf("%6d",t[i]); printf("Average waiting time = %.2f",awt); printf("Average turn around time = %.2f",att); }

round robin algo :

int main {  int count,j,n,time,remain,flag=0,time_quantum; int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; printf("Enter Total Process:\t "); scanf("%d",&n); remain=n; for(count=0;count0) {      time+=rt[count]; rt[count]=0; flag=1; }    else if(rt[count]>0) {      rt[count]-=time_quantum; time+=time_quantum; }    if(rt[count]==0 && flag==1) {      remain--; printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); wait_time+=time-at[count]-bt[count]; turnaround_time+=time-at[count]; flag=0; }    if(count==n-1) count=0; else if(at[count+1]<=time) count++; else count=0; }  printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); return 0; }
 * 1) include

another round robin :

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; for(i=0;itimer) { 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
 * 2) include
 * 3) include<process.h>
 * 4) include<string.h>

Multilevel queue :


 * 1) include<stdio.h>
 * 2) include<conio.h>

float avg_wt,avg_tt; int i = 0,ttl_wt=0,ttl_tt=0,qt;

struct process { int prn; char type; int bt; int wt; int tt; struct process *nxt; }*stfor,*stbck,*np,*endfor,*endbck,*temp;

void ins_node(struct process *np) { if(np->type == 'f') { if(stfor == NULL) stfor = endfor = np; else {  endfor->nxt = np; endfor = np; } } else { if(stbck == NULL) stbck = endbck = np; else {  endbck->nxt = np; endbck = np; } } }

void del_node(struct process *np,int choice) { if(np->type == 'f') { if(stfor == endfor) stfor = NULL; else stfor = stfor->nxt; if(choice == 1) delete np; } else {  if(stbck == endbck) stbck = NULL; else stbck = stbck->nxt; delete np; } }

void ins_dat { char ch; int j=0; stfor = endfor = stbck = endbck = NULL; do { np = new process; np->prn = ++j; printf("\n Enter the Burst time of Process #%d : ",np->prn); scanf("%d",&np->bt); printf("\n Enter the type of Process #%d (f-foreground or b-background): ",np->prn); np->type = getche; if(np->type == 'f') np->nxt = stfor; else np->nxt = NULL; np->wt = np->tt = 0; ins_node(np); printf("\n\n Continue ?? : "); ch = getche; }while(ch == 'y' || stfor == NULL); }

void Round_Robin { getch; printf("\n\n Foreground RR Process Scheduling (Quantum Size = %d)\n\n",qt); printf("\n\n Process  Burst Time   Waiting Time   Turnaround Time \n"); printf(" --- \n"); temp = stfor; do { printf(" %d \t\t %d \t\t",stfor->prn,stfor->bt); stfor->wt += (temp->tt - stfor->tt); if(qt >= stfor->bt) {   stfor->tt = temp->tt + stfor->bt; stfor->bt = 0; printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt); ttl_wt += stfor->wt; ttl_tt += stfor->tt; temp = np = stfor; del_node(np,1); ++i; }  else {   stfor->bt -= qt; stfor->tt = temp->tt + qt; printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt); np = temp = stfor; del_node(np,2); ins_node(temp); } }while(stfor != NULL); }

void FCFS { getch; printf("\n\n Background FCFS Process Scheduling \n\n"); printf("\n\n Process  Burst Time   Waiting Time   Turnaround Time \n"); printf(" --- \n"); do { printf(" %d \t\t %d \t\t",stbck->prn,stbck->bt); stbck->wt = temp->tt; stbck->tt = temp->tt + stbck->bt; printf(" %d \t\t %d\n\n",stbck->wt,stbck->tt); ttl_wt += stbck->wt; ttl_tt += stbck->tt; temp = np = stbck; del_node(np,1); ++i; }while(stbck != NULL);

} void main { clrscr; printf("\n Enter the Quantum time for Foreground Process : "); scanf("%d",&qt); ins_dat; if(stfor != NULL) Round_Robin; if(stfor == NULL && stbck != NULL) FCFS; avg_wt = (float) ttl_wt/i; avg_tt = (float) ttl_tt/i; printf("\n\n Average Waiting Time : %f",avg_wt); printf("\n Average Turnaround Time : %f",avg_tt); getch; }

Bankers algo :

void main { int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0; clrscr; printf("\n Enter the number of resources : "); scanf("%d", &rz); printf("\n enter the max instances of each resources\n"); for (i=0;i<rz;i++) { avail[i]=0; printf("%c= ",(i+97)); scanf("%d",&ins[i]); }	printf("\n Enter the number of processes : "); scanf("%d", &pno); printf("\n Enter the allocation matrix \n    "); for (i=0;i<rz;i++) printf(" %c",(i+97)); printf("\n"); for (i=0;i <pno;i++) { P[i]=i; printf("P[%d] ",P[i]); for (j=0;j<rz;j++) { scanf("%d",&allocated[i][j]); avail[j]+=allocated[i][j]; }	}	printf("\nEnter the MAX matrix \n    "); for (i=0;i<rz;i++) { printf(" %c",(i+97)); avail[i]=ins[i]-avail[i]; }	printf("\n"); for (i=0;i <pno;i++) { printf("P[%d] ",i); for (j=0;j<rz;j++) scanf("%d", &MAX[i][j]); }	printf("\n"); A: d=-1; for (i=0;i <pno;i++) { count=0; t=P[i]; for (j=0;j<rz;j++) { need[t][j] = MAX[t][j]-allocated[t][j]; if(need[t][j]<=avail[j]) count++; }		if(count==rz) { output[k++]=P[i]; for (j=0;j<rz;j++) avail[j]+=allocated[t][j]; } else P[++d]=P[i]; }	if(d!=-1) { pno=d+1; goto A;	} printf("\t <"); for (i=0;i<k;i++) printf(" P[%d] ",output[i]); printf(">"); getch; }
 * 1) include<stdio.h>
 * 2) include<conio.h>

another bankers algo :

void main { int k=,output[10],d=,t=,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=; clrscr; printf("\n Enter the number of resources : "); scanf("%d", &rz); printf("\n enter the max instances of each resources\n"); for(i=;i<rz;i++) { avail[i]=; printf("%c= ",(i+97)); scanf("%d",&ins[i]); } printf("\n Enter the number of processes : "); scanf("%d", &pno); printf("\n Enter the allocation matrix \n    "); for(i=;i<rz;i++) printf(" %c",(i+97)); printf("\n"); for(i=;i <pno;i++) {          P[i]=i; printf("P[%d] ",P[i]); for(j=;j<rz;j++) { scanf("%d",&allocated[i][j]); avail[j]+=allocated[i][j]; } } printf("\nEnter the MAX matrix \n    "); for(i=;i<rz;i++) {         printf(" %c",(i+97)); avail[i]=ins[i]-avail[i]; } printf("\n"); for(i=;i <pno;i++) { printf("P[%d] ",i); for(j=;j<rz;j++) scanf("%d", &MAX[i][j]); } printf("\n"); A: d=-1; for(i=;i <pno;i++) { count=; t=P[i]; for(j=;j<rz;j++) { need[t][j] = MAX[t][j]-allocated[t][j]; if(need[t][j]<=avail[j]) count++; } if(count==rz) { output[k++]=P[i]; for(j=;j<rz;j++) avail[j]+=allocated[t][j]; } else P[++d]=P[i]; } if(d!=-1) { pno=d+1; goto A; } printf("\t <"); for(i=;i<k;i++) printf(" P[%d] ",output[i]); printf(">"); getch; }
 * 1) include<stdio.h>
 * 2) include<conio.h>

FIFO

int main { int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\n ENTER THE NUMBER OF PAGES:\n"); scanf("%d",&n); printf("\n ENTER THE PAGE NUMBER :\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\n ENTER THE NUMBER OF FRAMES :"); scanf("%d",&no); for(i=0;i<no;i++) frame[i]= -1; j=0; printf("\tref string\t page frames\n"); for(i=1;i<=n;i++) {                                   printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++) if(frame[k]==a[i]) avail=1; if (avail==0) {                                               frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]); }                                   printf("\n"); }                       printf("Page Fault Is %d",count); return 0; }
 * 1) include<stdio.h>

LRU-

main { int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; printf("Enter no of pages:"); scanf("%d",&n); printf("Enter the reference string:"); for(i=0;i<n;i++) scanf("%d",&p[i]); printf("Enter no of frames:"); scanf("%d",&f); q[k]=p[k]; printf("\n\t%d\n",q[k]); c++; k++; for(i=1;i<n;i++) {                       c1=0; for(j=0;j<f;j++) {                                   if(p[i]!=q[j]) c1++; }                       if(c1==f) {                                   c++; if(k<f) {                                               q[k]=p[i]; k++; for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n"); }                                   else {                                               for(r=0;r<f;r++) {                                                           c2[r]=0; for(j=i-1;j<n;j--) {                                                           if(q[r]!=p[j]) c2[r]++; else break; }                                   }                                    for(r=0;r<f;r++) b[r]=c2[r]; for(r=0;r<f;r++) {                                               for(j=r;j<f;j++) {                                                           if(b[r]<b[j]) {                                                                       t=b[r]; b[r]=b[j]; b[j]=t; }                                               }                                    }                                    for(r=0;r<f;r++) {                                               if(c2[r]==b[0]) q[r]=p[i]; printf("\t%d",q[r]); }                                   printf("\n"); }           } } printf("\nThe no of page faults is %d",c); }
 * 1) include<stdio.h>

OPTIMAL PAGE REPLACEMENT

int fr[3]; void main {	 void display; int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3]; int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3; clrscr; for(i=0;i<3;i++) {		 fr[i]=-1; }		 for(j=0;j<12;j++) {	  flag1=0; flag2=0; for(i=0;i<3;i++) {		 if(fr[i]==p[j]) {		   flag1=1; flag2=1; break; }	    }	       if(flag1==0) {		 for(i=0;i<3;i++) {			 if(fr[i]==-1) {				    fr[i]=p[j]; flag2=1; break; }			}	      }		 if(flag2==0) {		      for(i=0;i<3;i++) lg[i]=0; for(i=0;i<frsize;i++) {					 for(k=j+1;k<12;k++) {						 if(fr[i]==p[k]) {							 lg[i]=k-j; break; }				     }			}			  found=0; for(i=0;i<frsize;i++) {				  if(lg[i]==0) {					 index=i; found=1; break; }				 }				    if(found==0) {					 max=lg[0]; index=0; for(i=1;i<frsize;i++) {						    if(max<lg[i]) {							max=lg[i]; index=i; }			     }		 }	       fr[index]=p[j]; pf++; }	  display; }	printf("\n no of page faults:%d",pf); getch; } void display { int i; printf("\n"); for(i=0;i<3;i++) printf("\t%d",fr[i]); }
 * 1) include<stdio.h>
 * 2) include<conio.h>