fork download
  1. #include <pthread.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6.  
  7. #define PID_MIN 0
  8. #define PID_MAX 3
  9.  
  10. #define NUM_THREADS 100
  11. #define ITERATIONS 10
  12. #define SLEEP 5
  13.  
  14.  
  15.  
  16.  
  17. int S;
  18.  
  19. /* mutex lock for accessing pid_map */
  20. pthread_mutex_t mutex;
  21.  
  22. int pid_map[PID_MAX-PID_MIN+1];
  23. int thread_counter =0;
  24.  
  25.  
  26. int allocate_map(void);
  27. int allocate_pid(int thread_id);
  28. void release_pid(int pid);
  29.  
  30.  
  31.  
  32. // Allocates the pid map.
  33.  
  34. int allocate_map(void)
  35. {
  36. /*initilize to zero explicitly just to make sure*/
  37. int i;
  38. for (i =0;i<PID_MAX-PID_MIN+1;i++)
  39. {
  40. pid_map[i] = 0;
  41. S++;
  42.  
  43. }
  44. printf("map allocated!S=%d\n",S);
  45. return 1;
  46. }
  47.  
  48. /**
  49.  * Allocates a pid
  50.  */
  51. int allocate_pid(int t_id)
  52. {
  53. int next_pid;
  54. int i,j;
  55. int err;
  56.  
  57. for (j=0;j<PID_MAX-PID_MIN+1;j++)
  58. {
  59. printf("%d",pid_map[j]);
  60. }
  61. printf("\n");
  62.  
  63.  
  64. printf("Thread %d trying to lock...%d\n",t_id,pthread_mutex_lock(&mutex));
  65. S--;
  66.  
  67. for (i =0;i<PID_MAX-PID_MIN;i++)
  68. {
  69. if (pid_map[i] == 0)
  70. {
  71. pid_map[i] = 1;
  72. next_pid=i;
  73. pthread_mutex_unlock(&mutex);
  74. return next_pid+PID_MIN;
  75.  
  76.  
  77.  
  78. }
  79. }
  80. return -999;
  81.  
  82. }
  83. /**
  84.  * Releases a pid
  85.  */
  86. void release_pid(int pid)
  87. {
  88. pid = pid-PID_MIN;
  89. pid_map[pid] = 0;
  90. S++;
  91. }
  92.  
  93. void *do_something(void *threadid)
  94. {
  95. int my_pid;
  96. long thread_id;
  97. int i;
  98. double r;
  99. thread_id= (long)threadid;
  100. //printf("\nthread %d launched do_something\n", thread_id);
  101. for (i=0;i<1;i++)
  102. {
  103. r = rand()%50; // 0-50
  104. r=r/10;
  105. //r=0.1;
  106. //printf("thread %d trying to allocate PID..., S=%d\n",thread_id,S);
  107. my_pid=allocate_pid(thread_id);
  108.  
  109. printf("Thread number %d got PID %d S=%d\n", thread_id, my_pid,S);
  110. sleep(r);
  111. release_pid(my_pid);
  112.  
  113. printf("Thread number %d released PID %d S=%d \n", thread_id, my_pid,S);
  114.  
  115. }
  116.  
  117. thread_counter--;
  118. return;
  119. }
  120.  
  121.  
  122. int main()
  123. {
  124. int i,err;
  125. time_t t;
  126.  
  127. pthread_t tid[NUM_THREADS];
  128. if (pthread_mutex_init(&mutex, NULL) != 0)
  129. {
  130. printf("\n mutex init failed\n");
  131. return 1;
  132. }
  133.  
  134.  
  135. srand((unsigned) time(&t));
  136. allocate_map();
  137. while(i < 20)
  138. {
  139. err = pthread_create(&(tid[i]), NULL, &do_something, (void*)i);
  140. if (err != 0)
  141. printf("\ncan't create thread :[%s]", strerror(err));
  142. i++;
  143. thread_counter++;
  144. //printf("\nthread %d created\n", i);
  145. }
  146.  
  147. while (thread_counter >0)
  148. {
  149. sleep(1);
  150. }
  151. pthread_mutex_destroy(&mutex);
  152. printf("DONE!\n");
  153. }
  154.  
  155.  
  156.  
  157.  
Runtime error #stdin #stdout 0s 133376KB
stdin
Standard input is empty
stdout
Standard output is empty