Program to implement Peterson’s Algorithm for Mutual Exclusion in C | Wave the world

Program to implement Peterson’s Algorithm for Mutual Exclusion in C

/*
    Peterson’s Algorithm for Mutual Exclusion
    Created By: Pirate
*/


#include <stdio.h>
#include <pthread.h>

void* process(void *s);
int flag[2];
int turn,count=0,mode=0;
const int MAX = 1e9;

void main()
{
    printf("*** Peterson's Algorithm ***\n");
    pthread_t p1,p2,p3,p4;

    printf("\nFirst without Lock\n");

    pthread_create(&p1, NULL, process, (void*)0);
    pthread_create(&p2, NULL, process, (void*)1);

    pthread_join(p1, NULL);
    pthread_join(p2, NULL);

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

    printf("\n\nNow with Lock\n");
    count = 0;
    mode = 1;
    lock_init();

    pthread_create(&p3, NULL, process, (void*)0);
    pthread_create(&p4, NULL, process, (void*)1);

    pthread_join(p3, NULL);
    pthread_join(p4, NULL);

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

}

void lock_init()
{
    flag[0] = flag[1] = 0;
    turn = 0;
}

void lock(int current)
{
    flag[current] = 1;

    turn = 1-current;
    while (flag[1-current]==1 && turn==1-current) ;
}

void unlock(int current)
{
    flag[current] = 0;
}

void* process(void *s)
{
    int i = 0;
    int current = (int *)s;
    printf("Process : %d\n", current);

    if(mode == 1)
        lock(current);
    for (i=0; i<MAX; i++)
        count++;

    if(mode == 1)
        unlock(current);
}





Output :




9 comments:

  1. what does this line mean?
    const int MAX = 1e9;

    ReplyDelete
    Replies
    1. 1e-9 means "one times ten to the negative ninth power" i.e 1 × 10-9

      Delete
  2. thanks it is working but coild you make me understand what is the variable mode for

    ReplyDelete
    Replies
    1. mode variable for switching between two mode, i.e without lock and with lock.

      Delete
  3. its not working please help me out

    ReplyDelete
  4. can you send a program to implement petersons algo to synchronise two processes
    which are trying to write on same file

    ReplyDelete
    Replies
    1. I'll try. do you write any code for this?

      Delete

 

Pro

About