0% found this document useful (0 votes)
168 views4 pages

C Programming for Distributed Systems

The document discusses implementing the dining philosophers problem in C using IPC and fork() functions by modeling each philosopher's state as either thinking, hungry, or eating and controlling access to shared resources (chopsticks) using a monitor to ensure mutual exclusion and avoid deadlock between philosophers. It provides a solution with a data structure to track philosopher states and monitor methods for pickup and putdown of chopsticks that only allow eating when a philosopher's neighbors are not also eating.

Uploaded by

Utkarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views4 pages

C Programming for Distributed Systems

The document discusses implementing the dining philosophers problem in C using IPC and fork() functions by modeling each philosopher's state as either thinking, hungry, or eating and controlling access to shared resources (chopsticks) using a monitor to ensure mutual exclusion and avoid deadlock between philosophers. It provides a solution with a data structure to track philosopher states and monitor methods for pickup and putdown of chopsticks that only allow eating when a philosopher's neighbors are not also eating.

Uploaded by

Utkarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DISTRIBUTED SYSTEMS (RCS-701)

1. WRITE A PROGRAM IN C (using IPC / fork () function) to simulate the


DINING PHILOSOPHER problem. The output of the program must display
status of each philosopher and status of each shared resources(chopsticks).

Solution: To code this solution, we need to distinguish among three states in which
we may find a philosopher. For this purpose, we introduce the following data
structure:

THINKING – When philosopher doesn’t want to gain access to either fork.

HUNGRY – When philosopher wants to enter the critical section.

EATING – When philosopher has got both the forks, i.e., he has entered
the section.

Philosopher i can set the variable state[i] = EATING only if her two
neighbors are not eating
(state[(i+4) % 5] != EATING) and (state[(i+1) % 5] != EATING).

filter_none

brightness_4

// Dining-Philosophers Solution Using Monitors

monitor DP

    status state[5];

    condition self[5];

  

    // Pickup chopsticks

    Pickup(int i)
    {

        // indicate that I’m hungry

        state[i] = hungry;

  

        // set state to eating in test()

        // only if my left and right neighbors 

        // are not eating

        test(i);

  

        // if unable to eat, wait to be signaled

        if (state[i] != eating)

            self[i].wait;

    }

  

    // Put down chopsticks

    Putdown(int i)

    {

  

        // indicate that I’m thinking

        state[i] = thinking;

  

        // if right neighbor R=(i+1)%5 is hungry and

        // both of R’s neighbors are not eating,


        // set R’s state to eating and wake it up by 

        // signaling R’s CV

        test((i + 1) % 5);

        test((i + 4) % 5);

    }

  

    test(int i)

    {

  

        if (state[(i + 1) % 5] != eating

            && state[(i + 4) % 5] != eating

            && state[i] == hungry) {

  

            // indicate that I’m eating

            state[i] = eating;

  

            // signal() has no effect during Pickup(),

            // but is important to wake up waiting

            // hungry philosophers during Putdown()

            self[i].signal();

        }

    }

  
    init()

    {

  

        // Execution of Pickup(), Putdown() and test()

        // are all mutually exclusive,

        // i.e. only one at a time can be executing

for

    i = 0 to 4

  

        // Verify that this monitor-based solution is

        // deadlock free and mutually exclusive in that

        // no 2 neighbors can eat simultaneously

        state[i] = thinking;

    }

} // end of monitor

You might also like