Sei sulla pagina 1di 6

Aim: To implement Centralized Mutual Exclusion Algorithm Theory: What is Mutual exclusion?

It makes sure that concurrent process access shared resources or data in a serialized way. If a process, says Pi, is executing in its critical section, then no other processes can be executing in their critical sections. Example: updating a DB or sending control signals to an IO device. Centralized Algorithm:

1. Process 1 asks the coordinator for permission to enter a critical region. Permission is granted 2. Process 2 then asks permission to enter the same critical region. The coordinator does not reply. 3. When process 1 exits the critical region, it tells the coordinator, which then replies to 2 [2] Advantages: Fair algorithm, grants in the order of requests The scheme is easy to implement Scheme can be used for general resource allocation

Critical Question: When there is no reply, does this mean that the coordinator is dead or just busy? Shortcomings Single point of failure. No fault tolerance Confusion between No-reply and permission denied Performance bottleneck of single coordinator in a large system

Timestamp Prioritized Schemes:

a. Two processes want to enter the same critical region. b. Process 0 has the lowest timestamp, so it wins. c. When process 0 is done, it sends an OK also, so 2 can now enter the critical region[2] Comparison of the Mutual Exclusion Algorithms:

Conclusion:

Thus we have successfully implemented Centralized Mutual Exclusion Algorithm. #include<iostream.h> #include<iomanip.h> #include<conio.h> int min(int ts[],int use[],int no,int m) { int i=0,u; int min=ts[use[0]]; u=use[0]; for(i=1;i<no;i++) { if((min>ts[use[i]])&&(m!=use[i])) { min=ts[use[i]]; u=use[i]; } } return u; }

void main() { clrscr(); int p=0,n,no,i=0,m=0,ts[10],use[10]; cout<<"Enter the no. of processes"<<endl;

cin>>n; for(i=0;i<n;i++) { cout<<"Enter timestamp of process "<<i+1<<endl; cin>>ts[i]; } cout<<"Enter no. of processes wanting to use the resource"<<endl; cin>>no; for(i=0;i<no;i++) { cout<<"Enter name of process"<<endl; cin>>use[i]; } for(i=0;i<no;i++) { cout<<"Process "<<use[i]<<" sends a request to the co-ordinator"<<endl; if(no==1) { cout<<use[no-1]<<" gets to access the resource"; } else { m=min(ts,use,no,p); p=m; cout<<"Process "<<m<<" gets to use the resource"<<endl;

cout<<"The remaining processes are waiting to use the resource"<<endl; cout<<endl; } getch(); } }

/*OUTPUT: Enter the no. of processes 5 Enter timestamp of process 1 2 Enter timestamp of process 2 4 Enter timestamp of process 3 1 Enter timestamp of process 4 3 Enter timestamp of process 5 6 Enter no. of processes wanting to use the resource 2 Enter name of process 2 Enter name of process

3 Process 2 sends a request to the co-ordinator Process 2 gets to use the resource The remaining processes are waiting to use the resource

Process 3 sends a request to the co-ordinator Process 3 gets to use the resource The remaining processes are waiting to use the resource */

Potrebbero piacerti anche