Peterson’s Algorithm for Mutual Exclusion | Set 1 (Basic C implementation)

Problem: Given 2 process i and j, you need to write a program that can guarantee mutual exclusion between the two without any additional hardware support.

Solution: There can be multiple ways to solve this problem, but most of them require additional hardware support. The simplest and the most popular way to do this is by using Peterson Algorithm for mutual Exclusion. It was developed by Peterson in 1981 though the initial work in this direction by done by Theodorus Jozef Dekker who came up with Dekker’s algorithm in 1960, which was later refined by Peterson and came to be known as Peterson’s Algorithm.

Basically, Peterson’s algorithm provides guaranteed mutual exclusion by using only the shared memory. It uses two ideas in the algorithm,

1. Willingness to acquire lock.
2. Turn to acquire lock.

Explanation:

The idea is that first a thread expresses its desire to acquire lock and sets flag[self] = 1 and then gives the other thread a chance to acquire the lock. If the thread desires to acquire the lock, then, it gets the lock and then passes the chance to the 1st thread. If it does not desire to get the lock then the while loop breaks and the 1st thread gets the chance.

Implementation in C language

```// Filename: peterson_spinlock.c
// Use below command to compile:
// gcc -pthread peterson_spinlock.c -o peterson_spinlock

#include <stdio.h>

int flag[2];
int turn;
const int MAX = 1e9;
int ans = 0;

void lock_init()
{
// Initialize lock by reseting the desire of
// both the threads to acquire the locks.
// And, giving turn to one of them.
flag[0] = flag[1] = 0;
turn = 0;
}

// Executed before entering critical section
void lock(int self)
{
// Set flag[self] = 1 saying you want to acquire lock
flag[self] = 1;

// But, first give the other thread the chance to
// acquire lock
turn = 1-self;

// Wait until the other thread looses the desire
// to acquire lock or it is your turn to get the lock.
while (flag[1-self]==1 && turn==1-self) ;
}

// Executed after leaving critical section
void unlock(int self)
{
// You do not desire to acquire lock in future.
// This will allow the other thread to acquire
// the lock.
flag[self] = 0;
}

// A Sample function run by two threads created
// in main()
void* func(void *s)
{
int i = 0;
int self = (int *)s;

lock(self);

// Critical section (Only one thread
// can enter here at a time)
for (i=0; i<MAX; i++)
ans++;

unlock(self);
}

// Driver code
int main()
{
// Initialized the lock then fork 2 threads
lock_init();

// Create two threads (both run func)

// Wait for the threads to end.

printf("Actual Count: %d | Expected Count: %d\n",
ans, MAX*2);

return 0;
}
```
```// mythread.h (A wrapper header file with assert
// statements)

#include <assert.h>
#include <sched.h>

{
assert(rc == 0);
}

{
assert(rc == 0);
}

void *(*start_routine)(void*), void *arg)
{
assert(rc == 0);
}

{
assert(rc == 0);
}

```

Output:

```Thread Entered: 1
Actual Count: 2000000000 | Expected Count: 2000000000
```

The produced output is 2*109 where 109 is incremented by both threads.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.2 Average Difficulty : 3.2/5.0
Based on 13 vote(s)