title: Simulating deadlock of threads in C
body: <p>This is just the beginning of a bigger simulation. The idea is to have 3 threads and 2 files and a deadlock would appear with the usual conditions and I have hard coded just that specifically. </p>
  
  <p>It happens that the threads run and then the process aborts on the console with Abort (core dumped). I have the output below... Is this a deadlock?? My apologies for not getting the output in the preferred format but the backtraces to the memory are not being easy to format. </p>
  
  <pre><code>Time Interval 4000250
  *** glibc detected *** ./a.out: double free or corruption (out): 0x00007fa2dc000b00 *** 
   ======= Backtrace: =========
  /lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7fa2e351d626]
  /lib/x86_64-linux-gnu/libc.so.6(fclose+0x155)[0x7fa2e350d2a5]
  ./a.out[0x400a05]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x7e9a)[0x7fa2e3863e9a]
  /lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fa2e35914bd]
  ======= Memory map: ========
  00400000-00401000 r-xp 00000000 07:00 163351                              /home/kartheek    /Desktop/a.out
  00601000-00602000 r--p 00001000 07:00 163351                             /home/kartheek    /Desktop/a.out
  </code></pre>
  
  <p>00602000-00603000 rw-p 00002000 07:00 163351                             /home/kartheek/Desktop/a.out
      00923000-00944000 rw-p 00000000 00:00 0                                  [heap]
      7fa2d4000000-7fa2d4021000 rw-p 00000000 00:00 0 
      7fa2d4021000-7fa2d8000000 ---p 00000000 00:00 0 
      7fa2dc000000-7fa2dc021000 rw-p 00000000 00:00 0 
      7fa2dc021000-7fa2e0000000 ---p 00000000 00:00 0 
      7fa2e2287000-7fa2e229c000 r-xp 00000000 07:00 4805                       /lib/x86_64-    linux-gnu/libgcc_s.so.1
      7fa2e229c000-7fa2e249b000 ---p 00015000 07:00 4805                       /lib/x86_64-    linux-gnu/libgcc_s.so.1
      7fa2e249b000-7fa2e249c000 r--p 00014000 07:00 4805                       /lib/x86_64-    linux-gnu/libgcc_s.so.1
  7fa2e249c000-7fa2e249d000 rw-p 00015000 07:00 4805                       /lib/x86_64-linux-gnu/libgcc_s.so.1
  7fa2e249d000-7fa2e249e000 ---p 00000000 00:00 0 
  7fa2e249e000-7fa2e2c9e000 rw-p 00000000 00:00 0 
  7fa2e2c9e000-7fa2e2c9f000 ---p 00000000 00:00 0 
  7fa2e2c9f000-7fa2e349f000 rw-p 00000000 00:00 0 
  7fa2e349f000-7fa2e3652000 r-xp 00000000 07:00 4784                       /lib/x86_64-linux-gnu/libc-2.15.so
  7fa2e3652000-7fa2e3851000 ---p 001b3000 07:00 4784                       /lib/x86_64-linux-gnu/libc-2.15.so
  7fa2e3851000-7fa2e3855000 r--p 001b2000 07:00 4784                       /lib/x86_64-linux-gnu/libc-2.15.so
  7fa2e3855000-7fa2e3857000 rw-p 001b6000 07:00 4784                       /lib/x86_64-linux-gnu/libc-2.15.so
  7fa2e3857000-7fa2e385c000 rw-p 00000000 00:00 0 
  7fa2e385c000-7fa2e3874000 r-xp 00000000 07:00 4864                       /lib/x86_64-linux-gnu/libpthread-2.15.so
  7fa2e3874000-7fa2e3a73000 ---p 00018000 07:00 4864                       /lib/x86_64-linux-gnu/libpthread-2.15.so
  7fa2e3a73000-7fa2e3a74000 r--p 00017000 07:00 4864                       /lib/x86_64-linux-gnu/libpthread-2.15.so
  7fa2e3a74000-7fa2e3a75000 rw-p 00018000 07:00 4864                       /lib/x86_64-linux-gnu/libpthread-2.15.so
  7fa2e3a75000-7fa2e3a79000 rw-p 00000000 00:00 0 
  7fa2e3a79000-7fa2e3a9b000 r-xp 00000000 07:00 4764                       /lib/x86_64-linux-gnu/ld-2.15.so
  7fa2e3c80000-7fa2e3c83000 rw-p 00000000 00:00 0 
  7fa2e3c97000-7fa2e3c9b000 rw-p 00000000 00:00 0 
  7fa2e3c9b000-7fa2e3c9c000 r--p 00022000 07:00 4764                       /lib/x86_64-linux-gnu/ld-2.15.so
  7fa2e3c9c000-7fa2e3c9e000 rw-p 00023000 07:00 4764                       /lib/x86_64-linux-gnu/ld-2.15.so
  7fffa77ef000-7fffa7810000 rw-p 00000000 00:00 0                          [stack]
  7fffa784f000-7fffa7850000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  Aborted (core dumped)</p>
  
  <p>I don't know what that means in the output but the fact that it says Aborted puts me to think that I might be close. </p>
  
  <p>The code works as:</p>
  
  <p>Create the threads
  Thread1 accesses file1 and waits in written mode
  Thread2 also accesses file1 and tries to make changes to it and then this is what happens</p>
  
  <p>Now, the next step is to create a monitor thread that will run at a higher priority. Is there anyway to allow the deadlock to occur and then free it? I mean, I have in mind one solution where with the help of mutex conditions, we can check for a clashing situation, but that would be deadlock avoidance, something that I do not want. Is there anyway to allow the deadlock to occur and then prevent the abortion of the program so that the monitor thread can free the resources accordingly???</p>
  
  <p>Here is the code please. Again, apologies for the formatting.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;pthread.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sched.h&gt;
  
  FILE *fp1;
  FILE *fp2;
  struct sched_param schedparam1,schedparam2,schedparam3;
  int policy=SCHED_RR;
  int mutex1[3]= {0,0,0},mutex2[3]= {0,0,0};
  
  void *routine1(void *arg)
  {
  int a;
  printf("before");
  fp1= fopen("file1.txt", "w");
  usleep(250000);
  printf("woke up");
  fp2= fopen("file2.txt", "w");
  a=fclose(fp1);
  printf("%d\n",a);
  fclose(fp2);
  }
  
  void *routine2(void *arg)
  {
  printf("asdasd");
  fp2=fopen("file2.txt",  "w");
  fp1=fopen("file1.txt",  "w");
  fclose(fp1);
  fclose(fp2);
  }
  </code></pre>
  
  <p>//void *monitor_function(void *arg)
  //{</p>
  
  <pre><code>void main()
  {
  pthread_t thread1, thread2, thread3,thread4;
  pthread_attr_t at1,at2,at3;
  int iret1, iret2,iret3,iret4;
  struct timespec tp;
  schedparam1.sched_priority = 10;
  sched_setscheduler(getpid(),policy,&amp;schedparam1);
  sched_rr_get_interval(getpid(),&amp;tp);
  
  printf("\nTime Interval %ld\n",tp.tv_nsec);
  \
  printf("test");
  
  schedparam1.sched_priority = 10; //Set Nice value of Thread
  schedparam2.sched_priority = 10;
  schedparam3.sched_priority = 10;
  
  /* Set attributes*/
  
  
  pthread_attr_init(&amp;at1);
  pthread_attr_setinheritsched(&amp;at1, PTHREAD_INHERIT_SCHED); // To inherit parent thread     properties use PTHREAD_INHERIT_SCHED
  pthread_attr_setschedpolicy(&amp;at1, policy);
  pthread_attr_setschedparam(&amp;at1, &amp;schedparam1);
  
  
  pthread_attr_init(&amp;at2);
  pthread_attr_setinheritsched(&amp;at2, PTHREAD_INHERIT_SCHED);
  pthread_attr_setschedpolicy(&amp;at2,policy);
  pthread_attr_setschedparam(&amp;at2, &amp;schedparam2);
  
  pthread_attr_init(&amp;at3);
  pthread_attr_setinheritsched(&amp;at3, PTHREAD_INHERIT_SCHED);
  pthread_attr_setschedpolicy(&amp;at3, policy);
  pthread_attr_setschedparam(&amp;at3, &amp;schedparam3);
  
  //iret1 = pthread_create( &amp;thread1, &amp;at1, monitor_function,NULL);
  
  iret2 = pthread_create( &amp;thread2,&amp;at1, routine1,NULL);
  iret3 = pthread_create( &amp;thread3,&amp;at2, routine2,NULL);
  //iret4 = pthread_create( &amp;thread4,&amp;at3, NULL,NULL);
  
  
  pthread_join( thread2, NULL);
  pthread_join( thread3, NULL);
  //pthread_join( thread1, NULL);
  
  printf("\nThread create 1 returns: %d\n",iret1);
  
  printf("Thread create 2 returns: %d\n",iret2);
  printf("Thread create 3 returns: %d\n",iret3);
  exit(0);
  
  
  
  //fp1= fopen("file1.txt", "w");
  //fp2= fopen("file2.txt", "w");
  
  }
  </code></pre>