fork download
  1. #include <pthread.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6.  
  7. #define PID_MIN 300
  8. #define PID_MAX 350
  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;
  23. int thread_counter =0;
  24.  
  25.  
  26. int allocate_map(void);
  27. int allocate_PID(int thread_id); //not (void) purely for a nicer output, it shows therad 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. pid_map =(int*)malloc((PID_MAX-PID_MIN+1)*sizeof(int));
  39. if (pid_map == NULL)
  40. return -1;
  41.  
  42. // the structure is a bitmap:
  43. //0 - available,
  44. //1 - taken
  45. for (i =0;i<PID_MAX-PID_MIN+1;i++)
  46. {
  47. pid_map[i] = 0;
  48. S++;
  49.  
  50. }
  51. printf("map allocated!S=%d\n",S);
  52. return 1;
  53.  
  54. }
  55.  
  56. /**
  57.  * Allocates a pid
  58.  */
  59. int allocate_PID(int t_id)
  60. {
  61. int next_pid;
  62. int i,j;
  63. int err;
  64.  
  65. if (S<=0)
  66. return -1; //NOT thread SAFE
  67.  
  68. pthread_mutex_lock(&mutex);
  69. while (S<=0)
  70. {}
  71.  
  72. for (i =0;i<PID_MAX-PID_MIN+1;i++)
  73. {
  74. if (pid_map[i] == 0)
  75. {
  76.  
  77. S--;
  78. printf("Thread number %d got PID %d S=%d\n", t_id, i+PID_MIN,S);
  79. pid_map[i] = 1;
  80. next_pid=i;
  81. pthread_mutex_unlock(&mutex);
  82. return (next_pid+PID_MIN);
  83.  
  84.  
  85.  
  86. }
  87. }
  88. //for debug purposes:
  89. //if a thread will pass mutex and S-semaphore
  90. //but won't find a PID then -999 will be returned
  91. return -999;
  92.  
  93. }
  94. /**
  95.  * Releases a pid
  96.  */
  97. void release_pid(int pid)
  98. {
  99. pid = pid-PID_MIN;
  100. pid_map[pid] = 0;
  101. S++;
  102. }
  103.  
  104. void *do_something(void *threadid)
  105. {
  106. int my_pid;
  107. long thread_id;
  108. int i;
  109. double r;
  110. thread_id= (long)threadid;
  111. //printf("\nthread %d launched do_something\n", thread_id);
  112.  
  113.  
  114.  
  115. //for (i=0;i<10;i++)
  116. while(2>1)
  117. {
  118. r = rand()%50; // 0-50
  119. r=r/10;
  120.  
  121. sleep(r/100);
  122. my_pid=allocate_PID(thread_id); //will return PID or "-1" if unsucessfull
  123. if (my_pid==-1)
  124. {
  125. printf("thread %d was NOT able allocate PID\n",thread_id);
  126.  
  127. }
  128. else
  129. {
  130. sleep(r);
  131. release_pid(my_pid);
  132.  
  133. printf("Thread number %d released PID %d \tPIDs available: %d \n", thread_id, my_pid,S);
  134. thread_counter--;
  135. return;
  136. }
  137. }
  138.  
  139.  
  140. }
  141.  
  142.  
  143. int main()
  144. {
  145. int i,err;
  146. time_t t;
  147.  
  148. pthread_t tid[NUM_THREADS];
  149. if (pthread_mutex_init(&mutex, NULL) != 0)
  150. {
  151. printf("\n mutex init failed\n");
  152. return 1;
  153. }
  154.  
  155.  
  156. srand((unsigned) time(&t));
  157. allocate_map();
  158. while(i < NUM_THREADS)
  159. {
  160. err = pthread_create(&(tid[i]), NULL, &do_something, (void*)i);
  161. if (err != 0)
  162. printf("\ncan't create thread :[%s]", strerror(err));
  163. i++;
  164. thread_counter++;
  165. //printf("\nthread %d created\n", i);
  166. }
  167.  
  168. while (thread_counter >0)
  169. {
  170. sleep(1);
  171. }
  172. pthread_mutex_destroy(&mutex);
  173. printf("***DONE!***\n");
  174. }
  175.  
  176.  
  177.  
  178.  
Time limit exceeded #stdin #stdout 5s 133376KB
stdin
Standard input is empty
stdout
map allocated!S=51

can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily unavailable]
can't create thread :[Resource temporarily u