Vous êtes sur la page 1sur 32

OPERATING SYSTEM LAB PRACTICAL FILE IT-218

AMAN DEEP 706/IT/08

PROGRAM 1 CPU SCHEDULING


CPU scheduling is the process where CPU is scheduled to do a particular process for a given amount of time. Since we use single processor system, so only one process can take over CPU, the time of CPU is so configured to make sure that it doesnt sit idle. For this multiple processes are kept in memory at the same time and whenever suitable the CPU time is given to the appropriate process. CPU scheduling decisions may take place under the following four circumstances: Under following conditions, CPU scheduling is done: 1. When a process switches from the running state to the waiting state. 2. When a process switches from the running state to the ready state. 3. When a process switches from the waiting state to the ready state 4. When a process terminates

First Come, First Serve Scheduling (FCFS)


Under this algorithm the process that requests the CPU first is given the CPU time first. In this algorithm, the new process enters the ready queue at its end. When processes that came before it have been terminated, this process executes.

Shortest Job First Scheduling (SJF)


This is another algorithm of CPU scheduling. In this algorithm the shortest process in the ready queue gets the CPU time first followed by the next shortest process in the queue The SJF algorithm more advantageous because using this algorithm we get minimum average waiting time processes. Moving a short process before long one decreases the waiting time of the short process more than it increases the waiting time of the long process. Consequently, the average waiting time decreases. It is further divided into 2 parts- preemptive and non preemptive. In case of preemptive SJF, in case a process arrives at some time with burst time less than the remaining time of ongoing process, then this process takes over the CPU and the other process is put on hold. In case of non-preemptive SJF, the new process doesnt take over the CPU immediately, rather it waits until the current process ends and then takes over the CPU.

Round robin(RR)
This algorithm uses circular queue. All the processes that are ready are kept in a circular queue. A time slice called quantum is initially, at the max this much amount of time is given to any process in one go. If the process doesnt terminate during this time, then CPU time is given to the next process in the circular queue. So on the process continues until all processes terminate.

Priority scheduling

Here a number is associated with each process. This number is called the priority of the process. Generally lower number means higher priority. The CPU time is given to the process with higher priority. As in case of SJF, this scheduling can also be divided into 2 parts- preemptive and non preemptive. Here preemption is based on priority of the process. #include<iostream.h> #include<iomanip.h> #include<stdlib.h> #include<conio.h> struct proc{ proc(){ cpub=pri=at=wt=tt=tr=0; } char name[20]; int pri; int cpub; //CPU Burst Time int at; //Arrival Time int wt; //Waiting Time int tt; //Turnaround Time int tr; //Time Remaining virtual void input() { cout<<"\n\tEnter Name, CPU burst, Arrival Time : "; cin>>name>>cpub>>at; tr = cpub; } void inputwithpri(){ input(); cout<<"\n\tEnter priority : "; cin>>pri; } void calcvalue(int endtime) { tt = endtime - at; wt = tt- cpub; } void display(){ cout<<name<<cpub<<at<<'\n'; } }; void printavgs(proc ps[], int n) { float avcpubt, avtt, avwt; avtt = avwt = avcpubt = 0.0; for(int i=0;i<n;i++) { avtt += ps[i].tt; avwt += ps[i].wt; avcpubt += ps[i].cpub; } cout<<"\n\tAverage Turnaround Time : "<<(avtt/n); cout<<"\n\tAverage Waiting Time : "<<(avwt/n); cout<<"\n\tAverage CPU Time : "<<(avcpubt/n)<<endl; } void printchart(proc ps[], int n) { cout<<" Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Time \n";

for(int i=0;i<n;i++) { cout<<setw(4)<<ps[i].name<<setw(15)<<ps[i].at<<setw(12)<<ps[i].cpub<<setw(15)< <ps[i].wt<<setw(18)<<ps[i].tt<<'\n'; } } void sortbyat(proc ps[], int n) { int i,j; proc c; for(i=0;i<n;i++) { for(j=0;j<i;j++) if(ps[i].at < ps[j].at) { c = ps[i]; ps[i] = ps[j]; ps[j] = c; } }

void sortbypri(proc ps[], int n) { int i,j; proc c; for(i=0;i<n;i++) { for(j=0;j<i;j++) if(ps[i].pri > ps[j].pri) { c = ps[i]; ps[i] = ps[j]; ps[j] = c; } } } void dispdispatch() { cout<<" Start Time End Time Name TimeRemaining\n"; } void dispatch(proc &ps, int time, int & timeline,int &terminated) { int burst; if(time==-1 || time>ps.tr) burst = ps.tr; else burst = time; ps.tr -= burst; cout<<setw(10)<<timeline<<setw(11)<<timeline+burst<<setw(7)<<ps.name<<set w(17)<<ps.tr<<'\n'; timeline += burst; if (ps.tr == 0) {terminated ++;ps.calcvalue(timeline);}

void fcfs(proc ps[], int n) { int i=0; int terminated=0 ;//processes finished int todispatch; int timeline = ps[0].at;// time line variable dispdispatch(); todispatch = -1; i=0; while(i<n) { if(ps[i].at <= timeline) {todispatch = i;} else todispatch = -1; if (todispatch!=-1) {dispatch(ps[todispatch], -1, timeline, terminated); i++;} else timeline++; } } void roundrobin(proc ps[], int n, int tq)//tq is timequantum { int i=0; int terminated=0 ;//processes finished int timeline = ps[0].at;// time line variable dispdispatch(); i=0; while(terminated != n) { int dispatched; //counts no. dispatched dispatched = 0; while(i<n) { if(ps[i].at <= timeline && ps[i].tr > 0) { dispatch(ps[i], tq, timeline, terminated); dispatched++; } i++; } i = 0; if (dispatched == 0)timeline++; dispatched = 0;

} } void prischedule(proc ps[], int n) {

int i=0, maxpri=32767; //maximum priority int terminated=0 ;//processes finished int todispatch; int timeline = ps[0].at;// time line variable dispdispatch(); while (terminated != n) { todispatch = -1; maxpri=32767; i=0;

while(i<n) { if((ps[i].tr > 0 )&& (ps[i].at <= timeline) && (ps[i].pri < maxpri)) {todispatch = i; maxpri = ps[todispatch].pri;} i++; } if (todispatch!=-1)dispatch(ps[todispatch], -1, timeline, terminated); else timeline++; } } void sjf(proc ps[], int n) { int i=0, mincpub=32767; //min burst int terminated=0 ;//processes finished int todispatch; int timeline = ps[0].at;// time line variable dispdispatch(); while (terminated != n) { todispatch = -1; mincpub=32767; i=0; while(i<n) { if((ps[i].tr > 0 )&& (ps[i].at <= timeline) && (ps[i].cpub < mincpub)) {todispatch = i; mincpub = ps[todispatch].cpub;} i++; } if (todispatch!=-1)dispatch(ps[todispatch], -1, timeline, terminated); else timeline++; } }

void main() { clrscr(); int num, algo, timeslice; proc ps[100]; cout<<"\n\tList of algos : \n\t1. FCFS \ \n\t2. SJF \ \n\t3. ROUND ROBIN \ \n\t4. PRIORITY\n"; cout<<"\n\tWhich algo to implement ? "; cin>>algo; cout<<"\n\tNo. of processes : "; cin>>num; if(algo==3) {cout<<"\n\tEnter timeslice : "; cin>>timeslice;} for(int i =0;i<num;i++) { cout<<"\tFor proc "<<i+1<<'\n'; if (algo == 4) ps[i].inputwithpri();

else ps[i].input(); } sortbyat(ps,num); switch(algo) { case 1: fcfs(ps,num); break; case 2: sjf(ps,num); break; case 3: roundrobin(ps,num,timeslice); break; case 4: prischedule(ps,num); break; } printchart(ps,num); printavgs(ps, num); getch(); }

OUTPUT
List of algos : 1. FCFS 2. SJF

3. ROUND ROBIN 4. PRIORITY Which algo to implement ? 1 No. of processes : 3 For proc 1 Enter Name, CPU burst, Arrival Time : p1 12 0 For proc 2 Enter Name, CPU burst, Arrival Time : p2 10 4 For proc 3 Enter Name, CPU burst, Arrival Time : p3 6 12 Start Time End Time Name TimeRemaining 0 12 p1 0 12 22 p2 0 22 28 p3 0 Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Time p1 0 12 0 12 p2 4 10 8 18 p3 12 6 10 16 Average Turnaround Time : 15.333333 Average Waiting Time : 6 Average CPU Time : 9.333333 List of algos : 1. FCFS 2. SJF 3. ROUND ROBIN 4. PRIORITY Which algo to implement ? 2 No. of processes : 3 For proc 1 Enter Name, CPU burst, Arrival Time : P1 12 0 For proc 2 Enter Name, CPU burst, Arrival Time : P2 10 4 For proc 3 Enter Name, CPU burst, Arrival Time : P3 6 12

Start Time End Time Name TimeRemaining 0 12 P1 0 12 18 P3 0 18 28 P2 0 Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Time P1 0 12 0 12 P2 4 10 14 24 P3 12 6 0 6 Average Turnaround Time : 14 Average Waiting Time : 4.666667 Average CPU Time : 9.333333 List of algos : 1. FCFS 2. SJF 3. ROUND ROBIN 4. PRIORITY Which algo to implement ? 3 No. of processes : 3 Enter timeslice : 5 For proc 1 Enter Name, CPU burst, Arrival Time : P1 12 0 For proc 2 Enter Name, CPU burst, Arrival Time : P2 10 4 For proc 3 Enter Name, CPU burst, Arrival Time : P3 6 12 Start Time End Time Name TimeRemaining 0 5 P1 7 5 10 P2 5 10 15 P1 2 15 20 P2 0 20 25 P3 1 25 27 P1 0 27 28 P3 0 Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Time P1 0 12 15 27 P2 4 10 6 16 P3 12 6 10 16 Average Turnaround Time : 19.666666 Average Waiting Time : 10.333333 Average CPU Time : 9.333333 List of algos :

1. FCFS 2. SJF 3. ROUND ROBIN 4. PRIORITY Which algo to implement ? 4 No. of processes : 3 For proc 1 Enter Name, CPU burst, Arrival Time : P1 12 0 Enter priority : 3 For proc 2 Enter Name, CPU burst, Arrival Time : P2 10 4 Enter priority : 1 For proc 3 Enter Name, CPU burst, Arrival Time : P3 6 12 Enter priority : 2 Start Time End Time Name TimeRemaining 0 12 P1 0 12 22 P2 0 22 28 P3 0 Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Time P1 0 12 0 12 P2 4 10 8 18 P3 12 6 10 16 Average Turnaround Time : 15.333333 Average Waiting Time : 6 Average CPU Time : 9.333333

PAGE REPLACEMENT ALGORITHMS PROGRAM 2 FIFO PAGE REPLACEMENT ALGORITHM


#include<iostream.h> #include<stdio.h> #include<conio.h> void main() { int num,frames,x,page[50],frame[20],y,z,fl=0,flag=0,k=0,total_pag_faults; clrscr();

cout<<"\n\n\nEnter the number of Pages:: "; cin>>num; cout<<"\n\n\nEnter the number of Frames:: "; cin>>frames; cout<<"\n\n\nEnter the reference string:: "; for(x=0;x<num;x++) { cin>>page[x]; } for(x=0;x<frames;x++) //the first frames(a number) page faults where frames = number of frames { frame[x]=page[x]; } for(x=0;x<frames;x++) { cout<<"\n\n\nFrame "<<x+1<<":: "; cout<<frame[x]; } for(x=frames;x<num;x++) { fl=0; for(y=0;y<frames;y++) if(page[x]==frame[y]) { fl=1; //so as to have no page fault for this reference } if(fl!=1) { frame[flag++]=page[x]; if(flag==frames) flag=0; //reset flag to 1st frame cout<<"\n\nPAGE FAULT "<<(++k)+frames<<":\n\n"; for(z=0;z<frames;z++) { cout<<"Frame "<<z+1<<":: "; cout<<frame[z]; cout<<"\n"; } getch(); }} total_pag_faults=k+frames; cout<<"\n\nTotal number of page faults = "<<total_pag_faults; getch(); }

OUTPUT
Enter the number of Pages:: 20 Enter the number of Frames:: 3 Enter the reference string:: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Frame 1:: 7 Frame 2:: 0 Frame 3:: 1

PAGE FAULT 4: Frame 1:: 2 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 5: Frame 1:: 2 Frame 2:: 3 Frame 3:: 1 PAGE FAULT 6: Frame 1:: 2 Frame 2:: 3 Frame 3:: 0 PAGE FAULT 7: Frame 1:: 4 Frame 2:: 3 Frame 3:: 0 PAGE FAULT 8: Frame 1:: 4 Frame 2:: 2 Frame 3:: 0 PAGE FAULT 9: Frame 1:: 4 Frame 2:: 2 Frame 3:: 3 PAGE FAULT 10: Frame 1:: 0 Frame 2:: 2 Frame 3:: 3 PAGE FAULT 11: Frame 1:: 0 Frame 2:: 1 Frame 3:: 3 PAGE FAULT 12: Frame 1:: 0 Frame 2:: 1 Frame 3:: 2 PAGE FAULT 13: Frame 1:: 7 Frame 2:: 1 Frame 3:: 2

PAGE FAULT 14: Frame 1:: 7 Frame 2:: 0 Frame 3:: 2 PAGE FAULT 15: Frame 1:: 7 Frame 2:: 0 Frame 3:: 1 Total number of page faults = 15

PROGRAM 3 LRU PAGE REPLACEMENT ALGO


#include<iostream.h> #include<stdio.h> #include<conio.h> struct fr { int value; int index; }frame[20]; void main() { int num,frames,x,page[50],fl=0,y,z,k=0,flag=0,minimum,apnt,m=0,total_pag_faults; clrscr(); cout<<"\n\n\nEnter the number of Pages:: "; cin>>num;

cout<<"\n\n\nEnter the number of Frames:: "; cin>>frames; cout<<"\n\n\nEnter the reference string:: "; for(x=0;x<num;x++) { cin>>page[x]; } for(x=0;x<frames;x++) //the first frames(a number) page faults where frames = number of frames { frame[x].value=page[x]; } for(x=0;x<frames;x++) { cout<<"\n\n\nFrame "<<x+1<<":: "; cout<<frame[x].value; } for(x=frames;x<num;x++) { fl=0; flag=0; for(y=0;y<frames;y++) if(page[x]==frame[y].value) { fl=1; //so as to have no page fault for this reference } if(fl!=1) { for(z=0;z<frames;z++) { k=x-1; while((frame[z].value!=page[k])&&(k>=0)) k--; if(k>=0) { flag=1; frame[z].index=k; } if(flag!=1) { frame[z].index=-20;

} } minimum=9999; for(y=0;y<frames;y++) if(minimum>frame[y].index) { minimum=frame[y].index; apnt=y; } frame[apnt].value=page[x];

// To select the page with least index

} } total_pag_faults=m+frames; cout<<"\n\nTotal number of page faults = "<<total_pag_faults; getch(); }

cout<<"\n\nPAGE FAULT "<<(++m)+frames<<":\n\n"; for(z=0;z<frames;z++) { cout<<"Frame "<<z+1<<":: "; cout<<frame[z].value; cout<<"\n"; } getch();

OUTPUT
Enter the number of Pages:: 20 Enter the number of Frames:: 3 Enter the reference string:: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Frame 1:: 7 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 4: Frame 1:: 2 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 5: Frame 1:: 2 Frame 2:: 0 Frame 3:: 3 PAGE FAULT 6:

Frame 1:: 4 Frame 2:: 0 Frame 3:: 3 PAGE FAULT 7: Frame 1:: 4 Frame 2:: 0 Frame 3:: 2 PAGE FAULT 8: Frame 1:: 4 Frame 2:: 3 Frame 3:: 2 PAGE FAULT 9: Frame 1:: 0 Frame 2:: 3 Frame 3:: 2 PAGE FAULT 10: Frame 1:: 1 Frame 2:: 3 Frame 3:: 2

PAGE FAULT 11: Frame 1:: 1 Frame 2:: 0 Frame 3:: 2 PAGE FAULT 12: Frame 1:: 1 Frame 2:: 0 Frame 3:: 7 Total number of page faults = 12

PROGRAM 4 OPTIMAL PAGE REPLACEMENT ALGORITHM


#include<iostream.h> #include<stdio.h> #include<conio.h> struct fr { int value; int index; }frame[20]; void main() { int num,frames,x,page[50],fl=0,y,z,k=0,flag=0,maximum,apnt,m=0,total_pag_faults; clrscr(); cout<<"\n\n\nEnter the number of Pages:: "; cin>>num; cout<<"\n\n\nEnter the number of Frames:: "; cin>>frames; cout<<"\n\n\nEnter the reference string:: "; for(x=0;x<num;x++) { cin>>page[x]; } for(x=0;x<frames;x++) //the first frames(a number) page faults where frames = number of frames

{ } frame[x].value=page[x];

for(x=0;x<frames;x++) { cout<<"\n\n\nFrame "<<x+1<<":: "; cout<<frame[x].value; } for(x=frames;x<num;x++) { fl=0; flag=0; for(y=0;y<frames;y++) if(page[x]==frame[y].value) { fl=1; //so as to have no page fault for this reference } if(fl!=1) { for(z=0;z<frames;z++) { k=x; while((frame[z].value!=page[k])&&(k<=num)) k++; if(k<=num) { flag=1; frame[z].index=k; } if(flag!=1) { frame[z].index=9999; } maximum=-10; for(y=0;y<frames;y++) if(maximum<frame[y].index) { maximum=frame[y].index; index } frame[apnt].value=page[x]; cout<<"\n\nPAGE FAULT "<<(++m)+frames<<":\n\n"; for(z=0;z<frames;z++) { cout<<"Frame "<<z+1<<":: "; cout<<frame[z].value; cout<<"\n"; } getch(); apnt=y;

//To select the page with highest

} total_pag_faults=m+frames; cout<<"\n\nTotal number of page faults = "<<total_pag_faults; getch();

OUTPUT
Enter the number of Pages:: 20 Enter the number of Frames:: 3 Enter the reference string:: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Frame 1:: 7 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 4: Frame 1:: 2 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 5: Frame 1:: 2 Frame 2:: 0 Frame 3:: 3 PAGE FAULT 6: Frame 1:: 2 Frame 2:: 4 Frame 3:: 3 PAGE FAULT 7: Frame 1:: 2 Frame 2:: 0 Frame 3:: 3

PAGE FAULT 8: Frame 1:: 2 Frame 2:: 0 Frame 3:: 1 PAGE FAULT 9: Frame 1:: 7 Frame 2:: 0 Frame 3:: 1 Total number of page faults = 9

PROGRAM 5 BANKERS ALGORITHM


#include<iostream.h> #include<conio.h> #include<process.h> #define P 2 #define R 3 int ava[R] , max[P][R] ,alc[P][R], need[P][R]; void disp(int pid,int a[R]) { int i=0,j=0; clrscr(); cout<<"\n\t ALLOCATED MATRIX \n"; for(;i<P;i++) { cout<<"\n\tProcess No :" <<i+1; for(;j<R;j++) cout<<alc[i][j]; } cout<<"\n\t NEED MATRIX \n"; for(i=0;i<P;i++) { cout<<"\n\tProcess No :" <<i+1; for(j=0;j<R;j++) cout<<"\t"<<need[i][j]; } cout<<"\n\t AVAILABLE MATRIX \n"; for(i=0;i<R;i++) cout<<ava[i]; cout<<"\n\t REQUEST MATRIX \n"; for(i=0;i<R;i++) cout<<a[i]; cout<<"\n REQUESTING PROCESS \n"; cout<<pid; } void input() { int i=0,j=0; clrscr(); cout<<"\n\tEnter the available resource matrix :";

for(;i<R;i++) { cout<<"\n\tResource No.: "<<i+1<<" :"; cin>>ava[i]; } cout<<"\n\tEnter the Maximum Matrix"; for(i=0;i<P;i++) { cout<<"\n\tFor Process No.: "<<i+1<<" :"; for(j=0;j<R;j++) { cout<<"\n\tFor Resource No.: "<<j+1<<" 's number:= "; cin>>max[i][j]; } } cout<<"\n\rEnter the current Allocation Matrix :"; for(i=0;i<P;i++) { cout<<"\n\tFor Process No. "<<i+1<<" :"; for(j=0;j<R;j++) { cout<<"\n\tFor Resource No. "<<j+1<<" 's number allocated = "; cin>>alc[i][j]; } } getch(); } int makerequest(int a[R]) { int ctr=0,p; cout<<"\n\tENter the process no : "; cin>>p; for(;ctr<R;ctr++) { cout<<"\n\tEnter the number of resources type "<<ctr+1<<" :"; cin>>a[ctr]; } return p; } int simulate(int pid,int a[R]) { int i=0,flag=1; for(;i<R;i++) { if((a[i]<need[pid-1][i])&&(a[i]<ava[i])) flag++; else flag--; } if(flag==3) // condition 1 n 2 are true { for(i=0;i<R;i++) { ava[i]-=a[i]; alc[pid-1][i]+=a[i]; need[pid-1][i]-=a[i]; } return 0; } if(flag<=0) return 1; } void main() { int i=0,j=0,pno,result; int req[R]; input(); for(;i<P;i++) for(;j<R;j++) need[i][j]=max[i][j]- alc[i][j]; pno=makerequest(req);

getch(); result=simulate(pno,req); if(result==0) // No dead lock { cout<<"\n\tRequest is legal now heading towards Safety Algo"; getch(); disp(pno,req); getch(); exit(0); } else { cout<<"\n\tError Occurred ! Request is illegal !"; getch(); disp(pno,req); getch(); } }

OUTPUT
Enter the available resource matrix : Resource No.: 1 :3 Resource No.: 2 :2 Resource No.: 3 :1 Enter the Maximum Matrix For Process No.: 1 : For Resource No.: 1 's number:= 2 For Resource No.: 2 's number:= 1 For Resource No.: 3 's number:= 1 For Process No.: 2 : For Resource No.: 1 's number:= 2 For Resource No.: 2 's number:= 2 For Resource No.: 3 's number:= 0 Enter the current Allocation Matrix : For Process No. 1 : For Resource No. 1 's number allocated = 1 For Resource No. 2 's number allocated = 0 For Resource No. 3 's number allocated = 1 For Process No. 2 : For Resource No. 1 's number allocated = 1 For Resource No. 2 's number allocated = 1 For Resource No. 3 's number allocated = 0 ENter the process no : 2 Enter the number of resources type 1 :1 Enter the number of resources type 2 :1 Enter the number of resources type 3 :0 Error Occurred ! Request is illegal ! ALLOCATED MATRIX Process No :1101 Process No :2 NEED MATRIX

Process No :1 1 1 Process No :2 0 0 AVAILABLE MATRIX 321 REQUEST MATRIX 110 REQUESTING PROCESS 2

0 0

PROGRAM 6 MEMORY ALLOCATION ALGORITHMS


#include<conio.h> #include<iostream.h> class mem_alloc { int siz[25],n; int req; public : void getdata(); void firstfit(); void bestfit(); void worstfit(); }; void mem_alloc :: getdata() { cout<<"\n\tEnter the total number of available blocks: "; cin>>n; cout<<"\n\t"; for(int i=0;i<n;++i) { cout<<"\n\tEnter the size of block "<<i+1<<": "; cin>>siz[i]; } cout<<"\n\t Enter the memory space required for the process :"; cin>>req; } void mem_alloc :: firstfit() { int k=0; for(int j=0;j<n;++j) { if(siz[j]>req) { k++; break; } } if(k==0) cout<<"\n\t No block can store this process."; else cout<<"\n\t block no "<<j+1<<" is allocated of size "<<siz[j];

void mem_alloc :: worstfit() { int maxsize = siz[0];

int k; for(int j=1;j<n;++j) { if(maxsize<=siz[j]) { maxsize=siz[j]; k=j; } } if(maxsize>=req) cout<<"\n\t block no "<<k+1<<" is allocated of size "<<siz[k]; else cout<<"\n\t No block can store this process. "; } void mem_alloc :: bestfit() { int size,k=0; size = siz[0]; for(int l=1;l<n;++l) { if(size<=siz[l]) size=siz[l]; } for(int j=1;j<n;++j) { if(size>=siz[j] && siz[j]>=req) { size=siz[j]; k=j; } } if(size>=req) cout<<"\n\t The best possible fit is at block "<<k+1<<" of size "<<siz[k]; else cout<<"\n\t No block can store this process."; } void main() { clrscr(); int ch; mem_alloc obj1; cout<<"\n\n\t\t MEMORY ALLOCATION"; obj1.getdata(); while(1) { cout<<"\n\n\t\t Memory Allocation Method"; cout<<"\n\n 1. First Fit"; cout<<"\n 2. Best Fit"; cout<<"\n 3. Worst Fit"; cout<<"\n 4. Exit"; cout<<"\n\n Enter your choice :"; cin>>ch; if(ch == 4) break; switch(ch) { case 1:obj1.firstfit(); break; case 2: obj1.bestfit(); break; case 3: obj1.worstfit(); break; } getch(); } }

OUTPUT
Enter the total number of available blocks: 10 Enter the size of block 1: 1 Enter the size of block 2: 2 Enter the size of block 3: 3 Enter the size of block 4: 4 Enter the size of block 5: 5 Enter the size of block 6: 6 Enter the size of block 7: 7 Enter the size of block 8: 8 Enter the size of block 9: 9 Enter the size of block 10: 10 Enter the memory space required for the process :5 Memory Allocation Method 1. First Fit 2. Best Fit 3. Worst Fit 4. Exit Enter your choice :1 block no 6 is allocated of size 6 Memory Allocation Method 1. First Fit 2. Best Fit 3. Worst Fit 4. Exit Enter your choice :2 The best possible fit is at block 5 of size 5 Memory Allocation Method 1. First Fit 2. Best Fit 3. Worst Fit 4. Exit

Enter your choice :3 block no 10 is allocated of size 10

PROGRAM 7 PRODUCER COMSUMER PROBLEM


#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #include <signal.h> #define BUFFER_SIZE 10 /* Number of elements in shared memory buffer */ #define SEM_MUTEX 0 #define SEM_EMPTY 1 #define SEM_FULL 2 int rc, semID, shmID, i; char elem; union semun { int val; struct semid_ds *buf; unsigned short *array; } seminfo; struct sembuf WaitMutex={SEM_MUTEX, -1, 0}; struct sembuf SignalMutex={SEM_MUTEX, 1, 0}; struct sembuf WaitEmpty={SEM_EMPTY, -1, 0}; struct sembuf SignalEmpty={SEM_EMPTY, 1, 0}; struct sembuf WaitFull={SEM_FULL, -1, 0}; struct sembuf SignalFull={SEM_FULL, 1, 0}; struct shmid_ds shminfo; char *shmPtr; void initialize(); void producer(); void consumer(); void myhandler(int signo) { if(signo==SIGINT) {printf("\n Caught Ctrl-C\n Aborting after cleaning up shared memory and semaphores...\n"); sleep(2); /* Remove shared memory */ shmctl(shmID, IPC_RMID, &shminfo); /* Remove semaphores */ semctl(semID, SEM_MUTEX, IPC_RMID, seminfo); exit(1); } } main() { /* Initialize shared memory and semaphores */

initialize(); /* Start a child process and proceed accordingly*/ if (fork()==0) {signal(SIGINT,myhandler); /* Child becomes the consumer */ consumer(); /* Child quits after consuming 26 characters */ exit(0); } else { /* Parent becomes the producer */ producer(); /* Returns after producing 26 characters */ /* Wait for child to finish */ wait(NULL); //printf("\n child(consumer) finished.....now sleeping for 10 seconds\n"); //sleep(10); /* Remove shared memory */ shmctl(shmID, IPC_RMID, &shminfo); /* Remove semaphores */ semctl(semID, SEM_MUTEX, IPC_RMID, seminfo); /* Parent is done cleaning up, so now quits */ exit(0); } } void initialize() { /* Init semaphores */ /* Three semaphores (Empty, Full, Mutex) are created in one set */ if((semID=semget(IPC_PRIVATE, 3, 0666 | IPC_CREAT))==-1) {perror("semget"); exit(1); } /* printing the contents of struct semid_ds printf("\n sem_perm.mode= %d\n",obsemid_ds.sem_perm.mode);*/ /* Init Mutex to one, allowing access to critical section */ seminfo.val=1; semctl(semID, SEM_MUTEX, SETVAL, seminfo); /* Init Empty to number of elements in shared memory (circular buffer) */ seminfo.val=BUFFER_SIZE; semctl(semID, SEM_EMPTY, SETVAL, seminfo); /* Init Full to zero, no elements are produced yet */ seminfo.val=0; semctl(semID, SEM_FULL, SETVAL, seminfo); /* Init Shared memory */ shmID=shmget(IPC_PRIVATE, BUFFER_SIZE, 0666 | IPC_CREAT); } void producer() {

/* attach shared memory to process */ if((shmPtr=(char*)shmat(shmID, 0, SHM_W))==(void *)-1) {perror("shmat"); exit(1); } for(i=0; i < 26; i++) {printf(" (Inside parent)\n"); /* Wait(Empty) - pause if no empty spots in circular buffer (i.e. all filled) */ semop(semID, &WaitEmpty, 1); /* Produce element. Example element are chars 'a'-'z' */ elem='a'+i; printf("Produced elem '%c'\n", elem); /* Wait(Mutex) - don't touch shared memory while consumer is using it */ semop(semID, &WaitMutex, 1); /* Put element into shared memory buffer (circular buffer) */ *(shmPtr + (i%BUFFER_SIZE))=elem; /* Signal(Mutex) - allow consumer to access shared memory now */ semop(semID, &SignalMutex, 1); /* Signal(Full) - record one more filled spot in circular buffer */ semop(semID, &SignalFull, 1); sleep(1); } } void consumer() { /* attach shared memory to process*/ if((shmPtr=(char*)shmat(shmID, 0, SHM_R))==(void *)-1) {perror("shmat"); exit(1); } for(i=0; i < 26; i++) {printf("\t\t (Inside child)\n"); /* Wait(Full) - pause if no filled spots in circular buffer (i.e. all empty) */ semop(semID, &WaitFull, 1); /* Wait(Mutex) - don't touch shared memory while producer is using it */ semop(semID, &WaitMutex, 1); /* Get element from the shared memory buffer (circular buffer) */ elem=*(shmPtr + (i%BUFFER_SIZE)); /* Signal(Mutex) - allow producer to access shared memory now */ semop(semID, &SignalMutex, 1); /* Display character */ printf("\t\t Consumed elem '%c'\n", elem); //sleep(1); /* Signal(Empty) - record one more empty spot in circular buffer */ semop(semID, &SignalEmpty, 1); } }

OUTPUT

(Inside child) (Inside parent) Produced elem 'a' Consumed elem 'a' (Inside child) (Inside parent) Produced elem 'b' Consumed elem 'b' (Inside child) (Inside parent) Produced elem 'c' Consumed elem 'c' (Inside child) (Inside parent) Produced elem 'd' Consumed elem 'd' (Inside child) (Inside parent) Produced elem 'e' Consumed elem 'e' (Inside child) (Inside parent) Produced elem 'f' Consumed elem 'f' (Inside child) (Inside parent) Produced elem 'g' Consumed elem 'g' (Inside child)

(Inside parent) Produced elem 'h' Consumed elem 'h' (Inside child) (Inside parent) Produced elem 'i' Consumed elem 'i' (Inside child) (Inside parent) Produced elem 'j' Consumed elem 'j' (Inside child) (Inside parent) Produced elem 'k' Consumed elem 'k' (Inside child) (Inside parent) Produced elem 'l' Consumed elem 'l' (Inside child) (Inside parent) Produced elem 'm' Consumed elem 'm' (Inside child) (Inside parent) Produced elem 'n' Consumed elem 'n' (Inside child) (Inside parent) Produced elem 'o'

Consumed elem 'o' (Inside child) (Inside parent) Produced elem 'p' Consumed elem 'p' (Inside child) (Inside parent) Produced elem 'q' Consumed elem 'q' (Inside child) (Inside parent) Produced elem 'r' Consumed elem 'r' (Inside child) (Inside parent) Produced elem 's' Consumed elem 's' (Inside child) (Inside parent) Produced elem 't' Consumed elem 't' (Inside child) (Inside parent) Produced elem 'u' Consumed elem 'u' (Inside child) (Inside parent) Produced elem 'v' Consumed elem 'v'

(Inside child) (Inside parent) Produced elem 'w' Consumed elem 'w' (Inside child) (Inside parent) Produced elem 'x' Consumed elem 'x' (Inside child) (Inside parent) Produced elem 'y' Consumed elem 'y' (Inside child) (Inside parent) Produced elem 'z' Consumed elem 'z'

Vous aimerez peut-être aussi