Search This Blog

Loading...

Dining Philosophers Problem C Program

What is Dining Philosophers problem in C?

Suppose there are N philosophers around a table together, eating spaghetti and talking philosophy. Now we want to discuss the problem. Only N forks, so that only a fork between each of the philosopher. As there are only 5 philosophers and each requires two forks to eat, we need to formulate an algorithm, how philosophers make sure to eat spaghetti at once.
 
The next question is why we show the problems that way? Sometimes, when it comes to computers, often similar situations require solutions in a creative way. This is something like an abstract problem in a new dimension.
Below are the 2 ways of solving it.

1. PROBLEM DEFINITION
To implement Dining Philosophers Problem using Threads and Semaphores

ALGORITHM STEPS:
    1. Set the number of philosophers
    2. Declare a thread by the philosopher\
    3. Declare one semaphore (represent chopsticks) per philosopher
    4. When a philosopher is hungry
        1. Let's See if chopsticks on both sides are free
        2. Acquire both chopsticks or
        3. eat
        4. restore chopsticks
        5. If the sticks are not free
    5. Wait till they are available
# include<stdio.h>
# include<pthread.h>
# include<stdlib.h>
# include<unistd.h>
# include<ctype.h>
# include<sys/types.h>
# include<sys/wait.h>
# include<semaphore.h>
# include<sys/sem.h>

sem_t chopstick[100];
int n;
void *thread_func(int no)
{

int i,iteration=5;

for(i=0;i<iteration;++i)
{

    sem_wait(&chopstick[no]);
    sem_wait(&chopstick[(no+1)%n]);
    printf(“\nPhilosopher %d –> Begin eating”,no);

    sleep(1);

    printf(“\nPhilosopher %d –> Finish eating\n”,no);
    sem_post(&chopstick[no]);
    sem_post(&chopstick[(no+1)%n]);
}

pthread_exit(NULL);
}

int main()
{

int i,res;
pthread_t a_thread[100];
printf(“\nEnter the number of philosopher :”);
scanf(“%d”,&n);

for(i=0;i<n;++i)
{
    res=sem_init(&chopstick[i],0,0);

    if(res==-1)
    {
        perror(“semaphore initialization failed”);
        exit(1);
    }
}

for(i=0;i<n;++i)
{
    res=pthread_create(&a_thread[i],NULL,thread_func,(int*) i);

    if(res!=0)
    {
        perror(“semaphore creation failed”);
        exit(1);
    }
sem_post(&chopstick[i]);
}

for(i=0;i<n;++i)
{
    res=pthread_join(a_thread[i],NULL);
    if(res!=0)
    {
        perror(“semaphore join failed”);
        exit(1);
    }
}

printf(“\n \n thread join succesfull\n”);

for(i=0;i<n;++i)
{

res=sem_destroy(&chopstick[i]);
    if(res==-1)
    {
        perror(“semaphore destruction failed”);
        exit(1);
    }
}
exit(0);
}

2. PROBLEM DEFINITION
To implement Dining Philosophers Problem using Threads and mutex

ALGORITHM STEPS:

    1. Set the number of philosophers
    2. Declare a thread by the philosopher
    3. Declare one mutex(represent chopsticks) per philosopher
    4. When a philosopher is hungry
        1. Let's See if chopsticks on both sides are free
        2. acquire chopsticks
        3. eat
        4. restore chopsticks
        5. If the sticks are not free
    5. wait until it becomes available
# include<stdio.h>
# include<pthread.h>
# include<stdlib.h>
# include<unistd.h>
# include<ctype.h>
# include<sys/types.h>
# include<sys/wait.h>
# include<semaphore.h>
# define philosopher 5

pthread_mutex_t chopstick[philosopher]

int main()
{

int i,res;
pthread_t a_thread[philosopher];
void *thread_func(int n)

for(i=0;i<philosopher;++i)
{
    res=pthread_mutex_init(&chopstick[i],NULL);

    if(res==-1)
    {
        perror(“mutex initialization failed”);
        exit(1);
    }
}

for(i=0;i<philosopher;++i)
{
    res=pthread_create(&a_thread[i],NULL,thread_func,(int)i);

    if(res!=0)
    {
        perror(“mutex creation failed”)
        exit(1);
    }
}

for(i=0;i<philosopher;++i)
{
    res=pthread_join(a_thread[i],NULL);

    if(res!=0)
    {
        perror(“mutex join failed”);
        exit(1);
    }
}

printf(“thread join successful\n”);

for(i=0;i<philosopher;++i)
{
    res=pthread_mutex_destroy(&chopstick[i]);

    if(res==-1)
    {
        perror(“mutex destruction failed”)
        exit(1);
    }
}

exit(0);
}

void *thread_func(int n)
{

int i,iteration=5;

    for(i=0;i<iteration;++i)
    {

        sleep(1);
        pthread_mutex_lock(&chopstick[n]);
        pthread_mutex_lock(&chopstick[n+1)%philosopher]);
        printf(“\nBegin eating :%d”,N);

        sleep(1);

        printf(“\nFinish eating”%d”,n);
        pthread_mutex_unlock(&chopstick[n]);
        pthread_mutex_unlock(&chopstick[n+1)%philosopher]);

    }

pthread_exit(NULL);

}

1 comment:

  1. #include

    #include

    pthread_mutex_t mut[5];

    void *fun(void *k)

    {

    long j=(long)k;

    pthread_mutex_lock(&mut[j]);

    pthread_mutex_lock(&mut[(j+1)%5]);

    printf("\nPhilosopher %ld eating using forks %ld and %ld",j,j,(j+1)%5);

    sleep(2);

    printf("\nPhilosopher %ld thinking releasing forks %ld and %ld",j,j,(j+1)%5);

    pthread_mutex_unlock(&mut[j]);

    pthread_mutex_unlock(&mut[(j+1)%5]);

    pthread_exit(NULL);

    }

    void main()

    {

    long i;

    pthread_t th[5];

    for(i=0;i<5;i++)

    pthread_mutex_init(&mut[i],0);





    for(i=0;i<5;i++)

    pthread_create(&th[i],NULL,fun,(void*)i);

    pthread_exit(NULL);

    }

    OUTPUT
    Produce : 1

    Produce : 2

    Produce : 3

    Produce : 4

    Produce : 5

    Consume : 5

    Consume : 4

    Consume : 3

    Consume : 2

    Consume : 1

    ReplyDelete