You are on page 1of 4

PROGRAM 4

Mutual Exclusion
Mutual exclusion, in computer science, refers to the problem of ensuring that no two processes
or threads (henceforth referred to only as processes) can be in their critical section at the same
time. Here, a critical section refers to a period of time when the process accesses a shared
resource, such as shared memory. Systems involving multiple processes are often most easily
programmed using critical regions. When a process has to read or update certain shared data
structures, it first enters a critical region to achieve mutual exclusion and ensure that no other
process will use the shared data structures at the same time.
Uses of Mutual Exclusion:-

• Prevention from deadlock.


• Prevention from starvation.
• Fairness
• Optimization

Types of Mutual Exclusion:-

1. Non-Token based:-
In this every process shares its time stamp(execution time) & process ID with all the
other processes. All process take decision which process has to be executed in its critical
section. Example: - Lamport algorithm.
2. Token based:-
In this process a token is rotated among every process and which process has this token
will entered in critical section and no other process is executed if it should not have
token. Example: - Suzuki-Kasami algorithm.

Performance Metrics of Mutual Exclusion:-

• Response Time
• Synchronization Delay
• System Throughput
• No. of messages necessary per critical section invocation.
Program:-
#include<stdio.h>

#include<dos.h>

#include<time.h>

void main()

int cs=0,pro=0;

double run=5;

char key='a';

time_t t1,t2;

clrscr();

printf("Press a key(except q) to enter a process into crirical section.\n");

printf("\nPress q at any time to exit.\n");

t1=time(NULL)-5;

while(key!='q')

while(!kbhit())

if(cs!=0)

t2=time(NULL);

if(t2-t1>run)

printf("Process %d\n",pro-1);

printf("\nExits critical section\n");

cs=0;
}

key=getch();

if(key!='q')

if(cs!=0)

printf("\nError:Another process is currently executing critical section Please wait till its
execution is over\n");

else

printf("Process %d",pro);

printf("Entered critical section\n");

cs=1;

pro++;

t1=time(NULL);

}
OUTPUT

You might also like