title: Simple dining philosopher using pthreads
body: <p>I am working on the dining philosophers program.  However I am running into a problem that my program stops before all of the philosophers have eaten, and I don't understand why. This is my code as of now:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;pthread.h&gt;
  
  void *func(int n);
  pthread_t philosopher[5];
  pthread_mutex_t chopstick[5];
  
  int main()
  {
       int i;
       void *msg;
       for(i=1;i&lt;=5;i++)
       {
            pthread_mutex_init(&amp;chopstick[i],NULL);
       }
       for(i=1;i&lt;=5;i++)
       {
            pthread_create(&amp;philosopher[i],NULL,(void *)func,(int *)i);
       }
       for(i=1;i&lt;=5;i++)
       {
            pthread_join(philosopher[i],&amp;msg);
       }
        for(i=1;i&lt;=5;i++)
        {
            pthread_mutex_destroy(&amp;chopstick[i]);
        }
       return 0;
  }
  
  void *func(int n)
  {
       printf ("\nPhilosopher %d is thinking ",n);
       pthread_mutex_lock(&amp;chopstick[n]);//when philosopher 5 is eating he takes fork 1 and fork 5
       pthread_mutex_lock(&amp;chopstick[(n+1)%5]);
       printf ("\nPhilosopher %d is eating ",n);
       sleep(3);
       pthread_mutex_unlock(&amp;chopstick[n]);
       pthread_mutex_unlock(&amp;chopstick[(n+1)%5]);
       printf ("\nPhilosopher %d finished eating ",n);
  }
  </code></pre>