#include#include#include#include#define } if(bchopstick[(id+4)%5] == 1) { pthread_cond_signal(&chop_cond[(id+4)%5]); } printf(” “);

#include#include#include#include#define num_thread 5pthread_mutex_t chopstick5;int bchopstick5;pthread_mutex_t m1;pthread_cond_t chop_cond5;void* check(void* tid){    int id = (int)tid;        int k = 0;    while(1){    pthread_mutex_lock(&m1);        if(bchopstickid == 1 && bchopstick(id+1)%5 == 1)        {                         bchopstickid = 0 ;             bchopstick(id+1)%5 = 0 ;            pthread_mutex_unlock(&m1);            pthread_mutex_lock(&chopstickid);            pthread_mutex_lock(&chopstick(id+1)%5);            //printf(“philospher pickup chopstick %d , %d
“,(id%5),(id+1)%5);                printf(“philospher %d is eating
“,id);            //sleep(rand()%5);            //printf(“philospher putdown chopstick %d , %d
“,(id%5),(id+1)%5);            pthread_mutex_unlock(&chopstick(id+1)%5);            pthread_mutex_unlock(&chopstickid);            printf(“philospher %d eating complete
“,id);            bchopstickid = 1 ;             bchopstick(id+1)%5 = 1 ;                        if(bchopstick(id+2)%5 == 1 )            {                pthread_cond_signal(&chop_cond(id+1)%5);            }            if(bchopstick(id+4)%5 == 1)            {                pthread_cond_signal(&chop_cond(id+4)%5);            }            printf(”
“);        }        else        {            pthread_cond_wait(&chop_condid,&m1);            pthread_mutex_unlock(&m1);    //        bchopstickid = 0 ;     //        bchopstick(id+1)%5 = 0 ;    //            pthread_mutex_unlock(&m1);    //        pthread_mutex_lock(&chopstickid);    //        pthread_mutex_lock(&chopstick(id+1)%5);            //printf(“philospher pickup chopstick %d , %d
“,(id%5),(id+1)%5);        //        printf(“philospher %d is eating
“,isd);            //sleep(rand()%5);            //printf(“philospher putdown chopstick %d , %d
“,(id%5),(id+1)%5);    //        pthread_mutex_unlock(&chopstick(id+1)%5);    //        pthread_mutex_unlock(&chopstickid);        //    printf(“philospher %d eating complete
“,id);        //    bchopstickid = 1 ;         //    bchopstick(id+1)%5 = 1 ;    }}    pthread_exit(NULL);}int main(){    pthread_t threadnum_thread;    int t = 0;    bchopstick0 = 1;    bchopstick1 = 1;    bchopstick2 = 1;    bchopstick3 = 1;    bchopstick4 = 1;    for(t=0;t<5;t++)    {        printf("philospher %d created ",t);        pthread_create(&threadt, NULL, check,(void *)t);        sleep(3);    }    pthread_exit(NULL);}