0% found this document useful (0 votes)
33 views34 pages

S.No. List of Programs Done On Sign

The document describes an algorithm for deadlock avoidance using the Banker's Algorithm. It involves inputting the number of processes and resources, maximum and allocated amounts for each process, and available resources. It then calculates the need (maximum - allocated) and checks if the requested resources can be allocated without causing a deadlock.

Uploaded by

Saurabh Yadav
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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views34 pages

S.No. List of Programs Done On Sign

The document describes an algorithm for deadlock avoidance using the Banker's Algorithm. It involves inputting the number of processes and resources, maximum and allocated amounts for each process, and available resources. It then calculates the need (maximum - allocated) and checks if the requested resources can be allocated without causing a deadlock.

Uploaded by

Saurabh Yadav
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 PDF, TXT or read online on Scribd
You are on page 1/ 34

S.NO.

LIST OF PROGRAMS

Done on

Sign

SHORTEST REMAINING TIME FIRST (SJF-Preemptive) #include<stdio.h> struct node { int arr; int bur; int nam; }; void sort(); void sort2(); struct node list[4],temp,temp3; int name[50],n,game[50]; int main() {int i,count=0,gum=0,e,wait=0,sumarr=0,errremov; int k=0,j=0,tt=0,t=0,tus,red,ticktock; int temp2,treasure=-1,temp4; float waiting=0,thru,turn; printf("Enter Total no. of processes: "); scanf("%d",&n); e=n; for(i=0;i<n;i++) { printf("Enter process no., arrival time , burst time:"); scanf(" %d %d %d", &list[i].nam,&list[i].arr,&list[i].bur); } game[gum]=0; gum++; for(i=0;i<n;i++) { tt=tt+list[i].bur; sumarr=sumarr+list[i].arr; if(list[i].arr>t) t=list[i].arr; } temp2=list[0].bur; while(k<t) {sort(); name[j]=list[0].nam; j++; list[0].bur--; temp2=list[0].bur; //Reducing list size if(list[0].bur==0) {for(red=0;red<n-1;red++) {list[red]=list[red+1];} n--; } for(i=0;i<n;i++) {if(list[i].arr==k) { list[i].arr++;}} k++; for(i=0;i<n;i++) {if(list[i].arr==k && temp2>list[i].bur) {temp2=list[i].bur;

tus=i; count++;}} if(count>0) {wait=wait+list[tus].arr; game[gum]=wait; gum++; temp3=list[0]; list[0]=list[tus]; list[tus]=temp3; } count=0; } printf("\n Gantt Chart \n"); for(i=0;i<j;i++) {if(treasure!=name[i]) {printf("| %d ", name[i]); treasure=name[i];}} sort2(); ticktock=gum; if(list[0].nam!=treasure && list[0].bur!=0) {wait=wait+list[0].bur; game[gum]=wait; gum++; printf("| %d ",list[0].nam);} else { errremov=gum-1; temp4=t-game[errremov]; wait=wait+list[0].bur+temp4; game[gum]=wait; gum++; } for(i=1;i<n;i++) {if(list[i].bur!=0) {wait=wait+list[i].bur; game[gum]=wait; gum++; printf("| %d ",list[i].nam); }} printf("|\n"); for(i=0;i<gum;i++) {printf(" %d ",game[i]);} for(i=ticktock;i<gum-1;i++) {waiting=waiting+game[i]; } waiting=waiting-sumarr; turn=waiting+game[gum-1]; waiting=waiting/e; turn=turn/e; printf("\n Avg Waiting time =%f",waiting); printf("\n Avg turn around time = %f",turn); return 0; } void sort() {

int h,l; for(h=0;h<n-1;h++) {for(l=h+1;l<n;l++) {if (list[h].arr>list[l].arr) {temp=list[h]; list[h]=list[l]; list[l]=temp; } } }} void sort2() { int h,l; for(h=0;h<n-1;h++) {for(l=h+1;l<n;l++) {if (list[h].bur>list[l].bur) {temp=list[h]; list[h]=list[l]; list[l]=temp; } } }}

OUTPUT Enter Enter Enter Enter Enter Enter Total no. of process no., process no., process no., process no., process no., processes:5 arrival time arrival time arrival time arrival time arrival time

, , , , ,

burst burst burst burst burst

time: time: time: time: time:

0 1 2 3 4

0 2 3 4 4

8 6 12 2 3

| 0

| 4

Gantt Chart | 4 | 0 | 1 6 9 13

| 2 19

| 31

Avg Waiting time =6.8 Avg turn around time =13

Shortest Job First(Non-Preemptive)-with arrival time #include<stdio.h> struct node { int a,b,s,wtt; char process; }; struct node list[5],ready[5],execution[5],temp; int main() { int i,tt=0,j=0,h=0,n,wt=0,t; float ttt=0,twt=0; printf(Enter no. of processes); scanf(%d,&n); for(i=0;i<n;i++) {printf(" Enter name,arrival time, burst time"); scanf(" %c %d %d", &list[i].process,&list[i].a,&list[i].b); } for(i=n-2;i>=1;i--) { for(j=1;j<=i;j++) { if(list[j].b>list[j+1].b) { temp=list[j]; list[j]=list[j+1]; list[j+1]=temp; } } } tt=tt+list[0].b; for(i=1;i<n;i++) {tt=tt+list[i].b; list[i].s=1; } execution[0]=list[0]; wt=wt+list[0].b; t=wt; j=1; while(wt<tt) {i=1; while(i<n) {if(list[i].s==1 && list[i].a <= t) {execution[j]=list[i]; j++; list[i].s=0; wt=wt+list[i].b; t=wt; i=1; } else {i++;

} } } for(i=1;i<j;i++) {h=h+execution[i-1].b; execution[i].wtt=h-execution[i].a; } for(i=1;i<j;i++) {twt=execution[i].wtt+twt; } for(i=0;i<j;i++) {printf(" %c ", execution[i].process); } ttt=(twt+tt)/n; twt=twt/n; printf("\n Avg Wait time = %f",twt); printf("\n Avg turnaround time = %f",ttt); }

OUTPUT Enter no. of processes 4 Enter name, arrival time, burst Enter name, arrival time, burst Enter name, arrival time, burst Enter name, arrival time, burst 1 4 2 3 Avg Wait time = 4 Avg turnaround time = 10.25

time time time time

1 2 3 4

0 2 3 4

5 6 12 2

Shortest Job First(Nonpremptive)-all arrive at same time #include<stdio.h> int main() {int i,j,n,a[10],temp;float wt=0,tr=0,h=0; printf("Enter no. of processes"); scanf("%d",&n); for(i=0;i<n;i++) {printf("Enter burst time"); scanf("%d",&a[i]); } for(i=0;i<n-1;i++) {for(j=i+1;j<n;j++) {if(a[i]>a[j]) {temp=a[i]; a[i]=a[j]; a[j]=temp; } }} for(i=0;i<n;i++) { printf(" %d \n",a[i]); } for(i=0;i<n;i++) {wt =wt +h; h=h+a[i]; tr=tr+h;} printf(" Avg waiting time %f",wt/n); printf("Avg turnaround time %f",tr/n); } OUTPUT : Enter the no. of processes 4 Enter burst time 5 Enter burst time 6 Enter burst time 12 Enter burst time 2 2 5 6 12 Avg. waiting time 5.5 Avg turnaround time 11.75

First Come First Serve- all arrive at same time #include<stdio.h> int main() { int p,i,a[10],time[10], q[10]; float temp=0, avg=0,turn=0,thru; { printf("Enter the no. of processes"); scanf("%d",&p); for(i=0;i<p;i++) { printf("Enter the burst time of process %d ",i); scanf("%d",&a[i]); } for(i=0;i<p;i++) {printf("Waiting time for process %d is %f \n",i,temp); avg=avg+temp; temp=temp+a[i]; turn=turn+temp; } avg=avg/p; turn=turn/p; thru=p/temp; printf("\nAverage Waiting time is %f",avg); printf("\nAverage Turnaround time is %f",turn); printf("\nThrouhput= %f",thru); }} OUTPUT Enter the no. of processes 4 Enter the burst time of process 0 Enter the burst time of process 1 Enter the burst time of process 2 Enter the burst time of process 3 Waiting time for process0 is 0 Waiting time for process1 is 8 Waiting time for process2 is 14 Waiting time for process3 is 26 Average waiting time is 12 Average Turnaround time is 19 Throughput = 0.142857 8 6 12 2

First come first served- with arrival time #include<stdio.h> int main() { float ar[10],wt=0,tr=0,bur[10],h=0; int n,i,k=0; printf("Enter no. of processes"); scanf("%d",&n); for(i=0;i<n;i++) {printf("Enter arr time of process %d",i); scanf("%f",&ar[i]); printf("Enter burst time of process %d",i); scanf("%f",&bur[i]); } for(i=0;i<n-1;i++) {printf(" | P%d", i); h=h+bur[i]; wt=wt+h-ar[i+1]; } printf(" | P%d |\n", i); h=0; for(i=0;i<n;i++) {printf("%d ",k); k=k+bur[i]; h=h+bur[i]; tr=tr+h-ar[i]; }printf("%d ",k); printf("\nAvg waiting time = %f \n", wt/5); printf("\nAvg turnaround time = %f \n",tr/5); printf("\nThroughput time = %f\n",n/h); }

OUTPUT Enter Enter Enter Enter Enter Total no. of process no., process no., process no., process no., processes:4 arrival time arrival time arrival time arrival time

, , , ,

burst burst burst burst

time: time: time: time:

1 2 3 4

0 5 2 6 3 12 4 2

| 0

| 5

Gantt Chart | 3 | 4 | 11 23 25

Avg Waiting time =6 Avg turn around time = 11 Throughput time = 0.16

PRIORITY Scheduling(Non-premptive)- all arrive at same time #include<stdio.h> int main() {int i,n,j,a[10], b[10], p[10],h=0; float temp=0, avg=0,turn=0,thru; printf("Enter no. of processes"); scanf("%d",&n); for(i=0;i<n;i++) {printf(" Enter burst time of process %d ",i); scanf("%d", &b[i]); printf("Enter priority of process %d", i); scanf("%d", &p[i]); } for(i=0;i<n;i++) {for(j=i+1;j<n;j++) {if(p[i]>p[j]) {temp=p[i]; p[i]=p[j]; p[j]=temp; temp=b[i]; b[i]=b[j]; b[j]=temp; } } } for(i=0;i<n;i++) {printf("%d %d \n",b[i], p[i]); } temp=0; for(i=0;i<n;i++) {printf("Waiting time for process %d = %f \n",i,temp); avg=avg+temp; temp=temp+b[i]; turn=turn+temp; } avg=avg/n; turn=turn/n; thru=n/temp; printf("\nAverage Waiting time = %f",avg); printf("\nAverage Turnaround time = %f",turn); printf("\nThrouhput = %f",thru); } OUTPUT : Enter no. of processes 4 Enter burst time of process 0 : 5 Enter priority of process 0 : 3 Enter burst time of process 1 : 12 Enter priority of process 1 : 4 Enter burst time of process 2 : 3 Enter priority of process 2 : 1 Enter burst time of process 3 : 7 Enter priority of process 3 : 2 3 1 7 2

5 3 12 4 Waiting Waiting Waiting Waiting

time time time time

for for for for

process process process process

0 1 2 3

= = = =

0 3 10 15

Average Waiting time = 7 Average Turnaround time = 13.75 Throughput = 0.148148

Round Robin CPU scheduling #include<stdio.h> struct node { char name; int bur,last,k; }; struct node p[10],order[100]; int main() {int i,n,tslice,tt=0,start=0,j=0; float twt=0,t=0; printf("Enter no. of processes"); scanf("%d",&n); printf("Enter time slice"); scanf("%d",&tslice); for(i=0;i<n;i++) {printf("Enter name, burst time of process %d ",i+1); scanf(" %c %d", &p[i].name,&p[i].bur); p[i].last=t; p[i].k=0; tt=tt+p[i].bur; } while(start<tt) { i=0; while(i<n) { if(p[i].bur!=0) {p[i].k=p[i].k+start-p[i].last; p[i].bur=p[i].bur-tslice; start=start+tslice; if(p[i].bur<0) {start=start+p[i].bur; p[i].bur=0; } p[i].last=start; order[j]=p[i]; j++; i++; } else {i++; }}} for(i=0;i<j;i++) {printf("%c",order[i].name); } for(i=0;i<n;i++) {twt=p[i].k+twt;} twt=(twt)/n; printf("Average waiting time is %f",twt); return 0;}

Output Enter no. of processes Enter time slice 4 1 : 1 5 2 : 2 6 4

Enter name, burst time of process Enter name, burst time of process

Enter name, burst time of process 3 : 3 12 Enter name, burst time of process 4 : 4 2 1 2 3 4 1 2 3 3 Average waiting time is 11.5

DEADLOCK AVOIDANCE BANKERS ALGORITHM

#include<stdio.h> #include<conio.h> int n,p,m,i,j,s,k=0; int work[10],finish[10],max[10][10],all[10][10],need[10][10],avail[10]; int check(); int main() { int m1,n1; int req[10]; printf("Enter no. of processes:\n"); scanf("%d",&n); printf("Enter no. of resources:\n"); scanf("%d",&m); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=m;j++) { printf("For process %d enter maximum and allocation criteria of resource %d",i,j); scanf("%d%d",&max[i][j],&all[i][j]); } } for(i=1;i<=m;i++) { printf("Enter no. of instances of resource type %d available:\t",i); scanf("%d",&avail[i]); } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { need[i][j]= max[i][j]-all[i][j]; } } printf("Which process wants to request resources:\n"); scanf("%d",&m1); for(i=1;i<=m;i++) { printf("How many instances does it request of type %d:",i); scanf("%d",&req[i]); } n1=0; for(i=1;i<=m;i++) { if(req[i]>avail[i]) n1++; } if(n1!=0) printf("\nIt will have to wait\n");

else { for(i=1;i<=m;i++) { avail[i]=avail[i]-req[i]; all[m1][i]=all[m1][i]+req[i]; need[m1][i]=need[m1][i]-req[i]; }

for(i=1;i<=m;i++) work[i]=avail[i]; for(i=1;i<=n;i++) finish[i]=0; for(;;) { k=0; for(i=1;i<=n;i++) { if(finish[i]==0 && s==0) { k++; for(j=1;j<=m;j++) work[j]=work[j]+all[i][j]; finish[i]=1; break; } } if(k==0) break; } p=0; for(i=1;i<=n;i++) { if(finish[i]==0) p++; } if(p!=0) printf("\n---System is not in safe state (IT MUST WAIT)--"); else printf("\n---System is in safe state (Request can be granted)--"); } getch(); return 0; } int check() { s=0; int l;

for(l=1;l<=m;l++) { if(need[i][l]<=work[l]) s=s; else s=1; } return s; }

OUTPUT: Enter no. of processes: 5 Enter no. of resources: 3 For process 1 enter maximum and allocation criteria of resource 17 0 For process 1 enter maximum and allocation criteria of resource 24 1 For process 1 enter maximum and allocation criteria of resource 33 0 For process 2 enter maximum and allocation criteria of resource 13 2 For process 2 enter maximum and allocation criteria of resource 22 0 For process 2 enter maximum and allocation criteria of resource 32 0 For process 3 enter maximum and allocation criteria of resource 19 3 For process 3 enter maximum and allocation criteria of resource 20 0 For process 3 enter maximum and allocation criteria of resource 32 2 For process 4 enter maximum and allocation criteria of resource 12 2 For process 4 enter maximum and allocation criteria of resource 22 1 For process 4 enter maximum and allocation criteria of resource 32 1 For process 5 enter maximum and allocation criteria of resource 14 0 For process 5 enter maximum and allocation criteria of resource 23 0 For process 5 enter maximum and allocation criteria of resource 33 2 Enter no. of instances of resource type 1 available: 10 Enter no. of instances of resource type 2 available: 5 Enter no. of instances of resource type 3 available: 7 Which process wants to request resources: 1 How many instances does it request of type 1:7 How many instances does it request of type 2:2 How many instances does it request of type 3:1 ---System is in safe state (Request can be granted)--

FIFO PAGE REPLACEMENT #include<stdio.h> #include<conio.h> int fr[3]; void main() { void display(); int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2}; int flag1=0,flag2=0,pf=0,frsize=3,top=0; clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<12;i++) { if(fr[i]==page[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<frsize;i++) { if(fr[i]==-1) { fr[i]=page[j]; flag2=1; break; } } } if(flag2==0) { fr[top]=page[j]; top++; pf++; if(top>=frsize) top=0; } display(); } printf("Number of page faults : %d ",pf); getch(); } void display() {

int i; printf("\n"); for(i=0;i<3;i++) printf("%d\t",fr[i]); }

OUTPUT: 2 -1 -1 2 2 2 5 5 5 5 3 3 3 3 3 -1 3 -1 3 3 2 2 2 2 2 5 5 1 1 1 4 4 4 4 4 2 : 6

Number of page faults

LRU PAGE REPLACEMENT #include<stdio.h> #include<conio.h> 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 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++) fs[i]=0; for(k=j-1,l=1;l<=frsize-1;l++,k--) { for(i=0;i<3;i++) { if(fr[i]==p[k]) fs[i]=1; } } for(i=0;i<3;i++) { if(fs[i]==0) 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]); }

OUTPUT: 2 -1 -1 2 2 2 2 2 2 2 3 3 3 3 3 -1 3 -1 3 5 5 5 5 5 5 5 5 1 1 1 4 4 4 2 2 2

no of page faults : 4

OPTIMAL PAGE REPLACEMENT

#include<stdio.h> #include<conio.h> 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]); }

OUTPUT: 2 -1 -1 2 3 -1 2 3 -1 2 3 1 2 3 5 2 3 5 4 3 5 4 3 5 4 3 5 2 3 5 2 3 5 2 3 5 no of page faults : 3

MEMORY MANAGEMENT- BEST FIT #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> using namespace std; int list[20],n_blocks,p_list[2][20]; void allocate(int x); void get_blocks(); void addProcess(); void display(); void deleteProcess(int i); int top = 0; int main () { int choice,id; get_blocks(); do{ cout<<"\n\n Press 1 : TO ADD A NEW PROCESS.\n Press 2 : TO DELETE A PROCESS.\n PRESS 3 : TO DISPLAY ALL THE PROCESS INFO.\n Press 4 : TO EXIT."; cin >> choice; switch(choice) { case 1 : addProcess(); break; case 2 : cout<< "\nEnter the process id."; cin >> id; deleteProcess(id-1); break; case 3 : display(); break; case 4 : exit(0); } }while(choice!=4); getch(); return 0; } void get_blocks() { cout<<"Enter the no. of memory blocks"; cin >> n_blocks; cout<<"\nEnter the free size of each block\n"; for(int i=0;i<n_blocks;i++) cin>>list[i]; } void addProcess() { if(top<20) { cout<<"\nEnter the size of the process"; cin >> p_list[0][top]; top++; allocate(top-1);

} else cout<<"Cannot enter a new process. Delete some first."; } void deleteProcess(int i) { if(top==0) cout<<"No processes exist! Add some."; else { list[p_list[1][i]-1]+=p_list[0][i]; for(int x=i;x<top-1;x++) { p_list[0][x]=p_list[0][x+1]; p_list[1][x]=p_list[1][x+1]; } top--; } } void allocate(int i) { int size,small,pos,num,x; x=p_list[0][i]; num=i; small = 9999; for(i=0;i<n_blocks;i++) { if(list[i]<small&&list[i]>=x) { small = list[i]; pos = i; } } if(small == 9999) { cout<<"Cannot allocate process"; deleteProcess(num); return; } list[pos]-=x; cout<<"\nProcess "<<num+1<<" allocated to "<<"memory block "<<pos+1<<endl; p_list[1][num]=pos+1; } void display() { cout<<"Process ID\tMemory needed\tMemory Block.\n"; for(int i=0;i<top;i++) cout<<i+1<<"\t\t"<<p_list[0][i]<<"\t\t"<<p_list[1][i]<<endl; }

OUTPUT: Enter the no. of memory blocks3 Enter the free size of each block 300 100 200 Press Press PRESS Press 1 2 3 4 : : : : TO TO TO TO ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.1

Enter the size of the process90 Process 1 allocated to memory block 2 Press Press PRESS Press 1 2 3 4 : : : : TO TO TO TO ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.3 Memory needed 90 TO TO TO TO Memory Block. 2

Process ID 1 Press Press PRESS Press 1 2 3 4 : : : :

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.2

Enter the process id.1 Press Press PRESS Press 1 2 3 4 : : : : TO TO TO TO ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.4

FIRST FIT #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> using namespace std; int list[20],n_blocks,p_list[2][20]; void allocate(int x); void get_blocks(); void addProcess(); void display(); void deleteProcess(int i); int top = 0; int main () { clrscr(); int choice,id; get_blocks(); do{ cout<<"\n\n Press 1 : TO ADD A NEW PROCESS.\n Press 2 : TO DELETE A PROCESS.\n PRESS 3 : TO DISPLAY ALL THE PROCESS INFO.\n Press 4 : TO EXIT."; cin >> choice; switch(choice) { case 1 : addProcess(); break; case 2 : cout<< "\nEnter the process id."; cin >> id; deleteProcess(id-1); break; case 3 : display(); break; case 4 : exit(0); } }while(choice!=4); getch(); return 0; } void get_blocks() { cout<<"Enter the no. of memory blocks"; cin >> n_blocks; cout<<"\nEnter the free size of each block\n"; for(int i=0;i<n_blocks;i++)

cin>>list[i]; } void addProcess() { if(top<20) { cout<<"\nEnter the size of the process"; cin >> p_list[0][top]; top++; allocate(top-1); } else cout<<"Cannot enter a new process. Delete some first."; } void deleteProcess(int i) { if(top==0) cout<<"No processes exist! Add some."; else { list[p_list[1][i]-1]+=p_list[0][i]; for(int x=i;x<top-1;x++) { p_list[0][x]=p_list[0][x+1]; p_list[1][x]=p_list[1][x+1]; } top--; } } void allocate(int i) { int size,pos,num,x,flag; x=p_list[0][i]; flag=0; num=i; for(i=0;i<n_blocks;i++) { if(list[i]>=x) { pos = i; flag=1; break; } } if(flag == 0) { cout<<"Cannot allocate process";

deleteProcess(num); return; } list[pos]-=x; cout<<"\nProcess "<<num+1<<" allocated to "<<"memory block "<<pos+1<<endl; p_list[1][num]=pos+1; } void display() { cout<<"Process ID\tMemory needed\tMemory Block.\n"; for(int i=0;i<top;i++) cout<<i+1<<"\t\t"<<p_list[0][i]<<"\t\t"<<p_list[1][i]<<endl; }

OUTPUT: Enter the no. of memory blocks3 Enter the free size of each block 300 100 200

Press Press PRESS Press

1 2 3 4

: : : :

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.1

Enter the size of the process100 Process 1 allocated to memory block 1

Press 1 : Press 2 : PRESS 3 : Press 4 : Process ID 1

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.3 Memory needed Memory Block. 100 1

Press Press PRESS Press

1 2 3 4

: : : :

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.2

Enter the process id.1

Press Press PRESS Press

1 2 3 4

: : : :

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.4

WORST FIT #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> //using namespace std; int list[20],n_blocks,p_list[2][20]; void allocate(int x); void get_blocks(); void addProcess(); void display(); void deleteProcess(int i); int top = 0; int main () { int choice,id; get_blocks(); do{ cout<<"\n\n Press 1 : TO ADD A NEW PROCESS.\n Press 2 : TO DELETE A PROCESS.\n PRESS 3 : TO DISPLAY ALL THE PROCESS INFO.\n Press 4 : TO EXIT."; cin >> choice; switch(choice) { case 1 : addProcess(); break; case 2 : cout<< "\nEnter the process id."; cin >> id; deleteProcess(id-1); break; case 3 : display(); break; case 4 : exit(0); } }while(choice!=4); getch(); return 0; } void get_blocks() { cout<<"Enter the no. of memory blocks"; cin >> n_blocks; cout<<"\nEnter the free size of each block\n"; for(int i=0;i<n_blocks;i++) cin>>list[i]; } void addProcess()

{ if(top<20) { cout<<"\nEnter the size of the process"; cin >> p_list[0][top]; top++; allocate(top-1); } else cout<<"Cannot enter a new process. Delete some first."; } void deleteProcess(int i) { if(top==0) cout<<"No processes exist! Add some."; else { list[p_list[1][i]-1]+=p_list[0][i]; for(int x=i;x<top-1;x++) { p_list[0][x]=p_list[0][x+1]; p_list[1][x]=p_list[1][x+1]; } top--; } } void allocate(int i) { int size,large,pos,num,x; x=p_list[0][i]; num=i; large = -1; for(i=0;i<n_blocks;i++) { if(list[i]>large&&list[i]>=x) { large = list[i]; pos = i; } } if(large == -1) { cout<<"Cannot allocate process"; deleteProcess(num); return; } list[pos]-=x;

cout<<"\nProcess "<<num+1<<" allocated to "<<"memory block "<<pos+1<<endl; p_list[1][num]=pos+1; } void display() { cout<<"Process ID\tMemory needed\tMemory Block.\n"; for(int i=0;i<top;i++) cout<<i+1<<"\t\t"<<p_list[0][i]<<"\t\t"<<p_list[1][i]<<endl; }

OUTPUT: Enter the no. of memory blocks3 Enter the free size of each block 300 100 200 Press Press PRESS Press 1 2 3 4 : : : : TO TO TO TO ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.1

Enter the size of the process50 Process 1 allocated to memory block 1 Press 1 : Press 2 : PRESS 3 : Press 4 : Process ID 1 TO TO TO TO ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.3 Memory needed Memory Block. 50 1

Press Press PRESS Press

1 2 3 4

: : : :

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.2

Enter the process id.1

Press 1 : TO ADD A NEW PROCESS. Press 2 : TO DELETE A PROCESS. PRESS 3 : TO DISPLAY ALL THE PROCESS INFO.

Press 4 : TO EXIT.3 Process ID Memory needed

Memory Block.

Press Press PRESS Press

1 2 3 4

: : : :

TO TO TO TO

ADD A NEW PROCESS. DELETE A PROCESS. DISPLAY ALL THE PROCESS INFO. EXIT.

You might also like