Sunday, 20 September 2015

Dining philosopher problem source code, operating system,

Dining philosopher problem source code, operating system,

 /*****************************************************************************
ASSIGNMENT TITLE:DINNING PHILOSOPHER PROBLEM
**************************************************************************** */

#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
using namespace std;
void *func(int n);
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];

    void *func(int n)
    {
cout<<"Philospher is thinking:"<<n<<endl;
    pthread_mutex_lock(&chopstick[n]);
    pthread_mutex_lock(&chopstick[(n+1)%5]);
cout<<"Philosopher is eating:";
    sleep(3);
    pthread_mutex_unlock(&chopstick[n]);
    pthread_mutex_unlock(&chopstick[(n+1)%5]);

    }

int main(int argc, char* argv[])
{
int i,j;
    void *msg;
    for(i=1;i<=5;i++)
    {
    j=pthread_mutex_init(&chopstick[i],NULL);
    if(j==-1)
    {
cout<<"Mutex initialization failed:";
        exit(1);
    }
    }
    for(i=1;i<=5;i++)
    {
    j=pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);
    if(j!=0)
    {
cout<<"Thread creation error:";
        exit(1);
    }
    }
    for(i=1;i<=5;i++)
    {
j=pthread_join(philosopher[i],&msg);
        if(j!=0)
{
cout<<"Thraed join failed:";
            exit(1);
}
}
for(i=1;i<=5;i++)
    {
j=pthread_mutex_destroy(&chopstick[i]);
if(j!=0)
{

cout<<"Mutex Destroyed:";
exit(1);
}
    }
return 0;
}


Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com

useful tutorial keep it up...
EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)

LinkWithin