title: simple deadlock example using pthread
body: <p>I am trying to understand deadlock with simple example using two resources rs1 and rs2, both has their own mutex locks, so proc1 locks resource1 and trying to get resource2, at the same time proc2 locks resource2 and trying to get resource1, so both are in deadlock. Following program shows deadlock scenario, but the problem is why both "p1 trying to get rs2" and "p2 tying to get rs1" are not printing by proc1 and proc2 respectively...</p>
  
  <pre><code>#include&lt;pthread.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  pthread_mutex_t resource1,resource2;
  int test=0;
  void *proc1()
  {
      printf("\nThis is proc1 using rs1");
      pthread_mutex_lock(&amp;resource1);
          usleep(200);
          printf("\np1 trying to get rs2..."); 
          pthread_mutex_lock(&amp;resource2);
              test++;
          printf("\nproc1 got rs2!!");    
          pthread_mutex_unlock(&amp;resource2);   
       pthread_mutex_unlock(&amp;resource1);  
  return 0;
  }
  
  void *proc2()
  {
      printf("\nThis is proc2 using rs2");
      pthread_mutex_lock(&amp;resource2);
          usleep(200);
          printf("\np2 trying to get rs1..."); 
          pthread_mutex_lock(&amp;resource1);
              test--;
          printf("\nproc2 got rs1!!");    
          pthread_mutex_unlock(&amp;resource1);   
       pthread_mutex_unlock(&amp;resource2);  
  return 0;
  }
  
  int main(){
      pthread_t t1,t2;
      pthread_mutex_init(&amp;resource1, NULL);
      pthread_mutex_init(&amp;resource2, NULL);
  
      pthread_create(&amp;t1,NULL, proc1 , NULL);
      pthread_create(&amp;t2,NULL, proc2 , NULL);
  
      pthread_join(t1,NULL);  
      pthread_join(t2,NULL);
  // will never arrive here
      pthread_mutex_destroy(&amp;resource1);
      pthread_mutex_destroy(&amp;resource2);
  }
  </code></pre>