dining philosophers problem


 
Thread Tools Search this Thread
Top Forums Programming dining philosophers problem
# 1  
Old 03-17-2006
dining philosophers problem

To avoid deadlock, I used a possible solution. The odd numbered philosophers grab the right and then the left while even numbered philosophers grap the chopsticks in the reverse order. Implement this solution using pthread mutual exclusion lock.

Please look at my code, although it works,but the print seems wrong.
FOR instance:

philosopher 4 is thinking for 7 seconds
philosopher 4 is eating for 2 seconds
philosopher 3 is no longer eating

but the third sentence should be
"philosopher 4 is no longer eating"
Code:
#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define N 5

  pthread_mutex_t  *mymutex;

void think (void *arg) {
        int i,*id;
        int j;
        id = (int *) arg;
        i = *id;
        j = 2+rand()%N; 
      /*  sleep(j);*/
	printf("\nPhilosopher  %d is thinking for %d seconds\n", i,j);
               
}

void eat (void *arg) {
        int i,*id;
        int j;
        id = (int *) arg;
        i = *id;
        j = 3+rand()%N;
        
	printf ("\nPhilosopher  %d is eating for %d seconds\n", i,j);
	sleep(j);
}

void *philosopher (void * arg)
{
   while(1) {    
      int i,j;
      i = *(int *) arg;
      j = rand()%N+3;
              think( arg);  
                if (i%2 == 1) {
              pthread_mutex_unlock(&mymutex[(i+1)%N]);    
              pthread_mutex_unlock(&mymutex[(i)%N]);
              printf("philosopher %d is eating for %d seconds\n",i,j);
              fflush(stdout);
              sleep(j); 
/*              printf("philosopher %d is no longer eating\n",i);
*/              pthread_mutex_lock(&mymutex[(i)%N]);   
              pthread_mutex_lock(&mymutex[(i+1)%N]);
            
           }
              else
                         {
              pthread_mutex_unlock(&mymutex[(i)%N]);     
              pthread_mutex_unlock(&mymutex[(i+1)%N]); 
              printf("philosopher %d is eating for %d seconds\n",i,j);
              fflush(stdout);
              sleep(j);
 /*             printf("philosopher %d is no longer eating\n",i);
*/              pthread_mutex_lock(&mymutex[(i+1)%N]);  
              pthread_mutex_lock(&mymutex[(i)%N]);   
             
            
           }
          /*   pthread_exit(NULL);   */
   }
}
   
void main (int argc, char*argv[]) {
        int rc, i, *ids;
        int status;
        
        pthread_t threads[N];
        pthread_attr_t attr;
        mymutex = (pthread_mutex_t*)malloc(sizeof(mymutex) * N);
        ids = (int *) malloc(sizeof(int)*N); 
        
        for (i = 0; i<N; i++) {
             	pthread_mutex_init(&mymutex[i], NULL);
             }
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        for (i = 0; i<N; i++) {
        ids[i] = i; 
  rc = pthread_create(&threads[i], NULL, philosopher,(void*)&ids[i]);
        if (rc) {
           printf("ERROR;return code from pthread_create() is %d\n",rc);
           exit(-1);
          }
	}
        pthread_attr_destroy(&attr);
        for (i = 0; i<N; i++) {
           pthread_mutex_destroy(&mymutex[i]);
        } 
        pthread_exit(NULL);
}

# 2  
Old 03-17-2006
Why? It's commented out.
# 3  
Old 03-17-2006
sorry I forgot remove the comment.
The original code should be:

Code:
.......
pthread_mutex_unlock(&mymutex[(i+1)%N]);    
              pthread_mutex_unlock(&mymutex[(i)%N]);
              printf("philosopher %d is eating for %d seconds\n",i,j);
              fflush(stdout);
              sleep(j); 
             printf("philosopher %d is no longer eating\n",i);
             pthread_mutex_lock(&mymutex[(i)%N]);   
              pthread_mutex_lock(&mymutex[(i+1)%N]);
.......

# 4  
Old 03-18-2006
You may have also missed the RULES about posting homework/classwork.
# 5  
Old 03-18-2006
RTM is right about the rules. But this is a tough problem and you have done a lot of work. So I will drop one hint. You are invoking fflush() only some of the time. Invoke it after every printf() or drop it altogether and use "setvbuf(stdout, NULL, _IONBF, 0);" at the beginning of the program.

I love the switch to chopsticks! I have always heard it with forks. It makes sense with chopsticks. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Router problem or ISP problem ?

Hi everyone, I am experiencing discontinuity of Internet service, this started 1 month ago. Everything worked very well for 1 year of intensive use, but now, I have problems reaching my gateway. The gateway is not my router but a node belonging to my ISP and I share the same public IP with... (3 Replies)
Discussion started by: remic
3 Replies

2. Homework & Coursework Questions

About dining philosopher problem

----- (1 Reply)
Discussion started by: mkchantony
1 Replies

3. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

4. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

5. Programming

Dining philosophers

Hi. I`m writng script solving problem of dining philosophers but it does not work. PHILOSOPHER SRIPT: #!/bin/bash ./start.conf cd stol a=$1 #echo a$a #echo ilosc$ilosc #echo dziel$(($%$ilosc)) if ; then #next=$%$ilosc] next=1 else next=$ fi (0 Replies)
Discussion started by: Aryman1983
0 Replies

6. AIX

AIX OS problem? network problem?

Dear ALL. I installed AIX OS on customer sites. but Only one site is too slow when I connected telnet, ftp.. Ping is too fast. but telnet and FTP is not connected.. of course i check the configuration file on aix but it's normal. Do any Idea?? thanks in advance. - Jun - (3 Replies)
Discussion started by: Jeon Jun Seok
3 Replies

7. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

8. Solaris

problem in finding a hardware problem

Hi I am right now facing a strange hardware problem. System get booted with the following error: Fatal Error Reset CPU 0000.0000.0000.0003 AFSR 0100.0000.0000.0000 SCE AFAR 0000.07c6.0000.1000 SC Alert: Host System has Reset It happen 4 or 5 times and get the same error every time.I... (8 Replies)
Discussion started by: girish.batra
8 Replies

9. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies

10. UNIX for Advanced & Expert Users

SSH Problem auth problem

Hi, Just recently we seem to be getting the following error message relating to SSH when we run the UNIX script in background mode: warning: You have no controlling tty. Cannot read confirmation.^M warning: Authentication failed.^M Disconnected; key exchange or algorithm negotiation... (1 Reply)
Discussion started by: budrito
1 Replies
Login or Register to Ask a Question