fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #define RAND_DIVISOR
  6. 100000000
  7. #define TRUE 1
  8. /* The mutex lock */
  9. pthread_mutex_t mutex;
  10. /* the semaphores */
  11. sem_t full, empty;
  12. /* the buffer */
  13. buffer_item buffer[BUFFER
  14. _SIZE];
  15. /* buffer counter */
  16. int counter;
  17. pthread_t tid; //
  18. Thread ID
  19. pthread_attr_t attr; //
  20. Set of thread attributes
  21. void *producer(void *
  22. param); /* the producer
  23. thread */
  24. void *consumer(void *
  25. param); /* the consumer
  26. thread */
  27. void initializeData() {
  28. /* Create the mutex
  29. lock */
  30. pthread_mutex_init(&
  31. mutex, NULL);
  32. /* Create the full
  33. semaphore and initialize
  34. to 0 */
  35. sem_init(&full, 0, 0
  36. );
  37. /* Create the empty
  38. semaphore and initialize
  39. to BUFFER_SIZE */
  40. sem_init(&empty, 0,
  41. BUFFER_SIZE);
  42. /* Get the default
  43. attributes */
  44. pthread_attr_init(&
  45. attr);
  46. /* init buffer */
  47. counter = 0;
  48. }
  49. /* Producer Thread */
  50. void *producer(void *
  51. param) {
  52. buffer_item item;
  53. while(TRUE) {
  54. /* sleep for a
  55. random period of time */
  56. int rNum = rand() /
  57. RAND_DIVISOR;
  58. sleep(rNum);
  59. /* generate a
  60. random number */
  61. item = rand();
  62. /* acquire the
  63. empty lock */
  64. sem_wait(&empty);
  65. /* acquire the
  66. mutex lock */
  67. pthread_mutex_lock
  68. (&mutex);
  69. if(insert_item(item
  70. )) {
  71. fprintf(stderr,
  72. " Producer report error
  73. condition\n");
  74. }
  75. else {
  76. printf("producer
  77. produced %d\n", item);
  78. }
  79. /* release the
  80. mutex lock */
  81. pthread_mutex_
  82. unlock(&mutex);
  83. /* signal full */
  84. sem_post(&full);
  85. }
  86. }
  87. /* Consumer Thread */
  88. void *consumer(void *
  89. param) {
  90. buffer_item item;
  91. while(TRUE) {
  92. /* sleep for a
  93. random period of time */
  94. int rNum = rand() /
  95. RAND_DIVISOR;
  96. sleep(rNum);
  97. /* aquire the full
  98. lock */
  99. sem_wait(&full);
  100. /* aquire the mutex
  101.  lock */
  102. pthread_mutex_lock
  103. (&mutex);
  104. if(remove_item(&
  105. item)) {
  106. fprintf(stderr,
  107. "Consumer report error
  108. condition\n");
  109. }
  110. else {
  111. printf("consumer
  112. consumed %d\n", item);
  113. }
  114. /* release the
  115. mutex lock */
  116. pthread_mutex_
  117. unlock(&mutex);
  118. /* signal empty */
  119. sem_post(&empty);
  120. }
  121. }
  122. /* Add an item to the
  123. buffer */
  124. int insert_item(buffer_
  125. item item) {
  126. /* When the buffer is
  127.  not full add the item
  128.   and increment the
  129. counter*/
  130. if(counter < BUFFER_
  131. SIZE) {
  132. buffer[counter] =
  133. item;
  134. counter++;
  135. return 0;
  136. }
  137. else { /* Error the
  138. buffer is full */
  139. return -1;
  140. }
  141. }
  142. /* Remove an item from
  143. the buffer */
  144. int remove_item(buffer_
  145. item *item) {
  146. /* When the buffer is
  147.  not empty remove the
  148. item
  149.   and decrement the
  150. counter */
  151. if(counter > 0) {
  152. *item = buffer[(
  153. counter-1)];
  154. counter--;
  155. return 0;
  156. }
  157. else { /* Error buffer
  158.  empty */
  159. return -1;
  160. }
  161. }
  162. int main(int argc, char
  163. *argv[]) {
  164. /* Loop counter */
  165. int i;
  166. /* Verify the correct
  167. number of arguments were
  168. passed in */
  169. if(argc != 4) {
  170. fprintf(stderr, "
  171. USAGE:./main.out <INT> <
  172. INT> <INT>\n");
  173. }
  174. int mainSleepTime =
  175. atoi(argv[1]); /* Time
  176. in seconds for main to
  177. sleep */
  178. int numProd = atoi(
  179. argv[2]); /* Number of
  180. producer threads */
  181. int numCons = atoi(
  182. argv[3]); /* Number of
  183. consumer threads */
  184. /* Initialize the app
  185. */
  186. initializeData();
  187. /* Create the producer
  188.  threads */
  189. for(i = 0; i <
  190. numProd; i++) {
  191. /* Create the
  192. thread */
  193. pthread_create(&tid
  194. ,&attr,producer,NULL);
  195. }
  196. /* Create the consumer
  197.  threads */
  198. for(i = 0; i <
  199. numCons; i++) {
  200. /* Create the
  201. thread */
  202. pthread_create(&tid
  203. ,&attr,consumer,NULL);
  204. }
  205. /* Sleep for the
  206. specified amount of time
  207. in milliseconds */
  208. sleep(mainSleepTime);
  209. /* Exit the program *
  210. /
  211.   printf("Exit the
  212. program\n");
  213.   exit(0);
  214. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
10 
10
10
compilation info
prog.c:6:1: error: expected identifier or '(' before numeric constant
 100000000
 ^
prog.c:13:1: error: unknown type name 'buffer_item'
 buffer_item buffer[BUFFER
 ^
prog.c:13:20: error: 'BUFFER' undeclared here (not in a function)
 buffer_item buffer[BUFFER
                    ^
prog.c:14:1: error: expected ']' before '_SIZE'
 _SIZE];
 ^
prog.c:18:1: error: unknown type name 'Thread'
 Thread ID
 ^
prog.c:19:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'pthread_attr_t'
 pthread_attr_t attr; //
 ^
prog.c:20:1: error: unknown type name 'Set'
 Set of thread attributes
 ^
prog.c:20:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'thread'
 Set of thread attributes
        ^
prog.c:20:8: error: unknown type name 'thread'
prog.c: In function 'initializeData':
prog.c:31:1: error: 'mutex' undeclared (first use in this function)
 mutex, NULL);
 ^
prog.c:31:1: note: each undeclared identifier is reported only once for each function it appears in
prog.c:41:1: error: 'BUFFER_SIZE' undeclared (first use in this function)
 BUFFER_SIZE);
 ^
prog.c:45:1: error: 'attr' undeclared (first use in this function)
 attr);
 ^
prog.c: In function 'producer':
prog.c:52:4: error: unknown type name 'buffer_item'
    buffer_item item;
    ^
prog.c:57:14: error: expected expression before ';' token
  RAND_DIVISOR;
              ^
prog.c:58:7: warning: implicit declaration of function 'sleep' [-Wimplicit-function-declaration]
       sleep(rNum);
       ^
prog.c:68:3: error: 'mutex' undeclared (first use in this function)
 (&mutex);
   ^
prog.c:69:10: warning: implicit declaration of function 'insert_item' [-Wimplicit-function-declaration]
       if(insert_item(item
          ^
prog.c:72:1: warning: missing terminating " character
 " Producer report error
 ^
prog.c:72:1: error: missing terminating " character
prog.c:73:1: error: stray '\' in program
 condition\n");
 ^
prog.c:73:1: error: 'condition' undeclared (first use in this function)
prog.c:73:11: error: expected ')' before 'n'
 condition\n");
           ^
prog.c:73:12: warning: missing terminating " character
 condition\n");
            ^
prog.c:73:11: error: missing terminating " character
 condition\n");
           ^
prog.c:74:7: error: expected ';' before '}' token
       }
       ^
prog.c:76:17: warning: missing terminating " character
          printf("producer
                 ^
prog.c:76:10: error: missing terminating " character
          printf("producer
          ^
prog.c:77:2: error: 'produced' undeclared (first use in this function)
  produced %d\n", item);
  ^
prog.c:77:2: error: stray '\' in program
prog.c:77:12: error: 'd' undeclared (first use in this function)
  produced %d\n", item);
            ^
prog.c:77:14: error: expected ')' before 'n'
  produced %d\n", item);
              ^
prog.c:77:15: warning: missing terminating " character
  produced %d\n", item);
               ^
prog.c:77:14: error: missing terminating " character
  produced %d\n", item);
              ^
prog.c:78:7: error: expected ';' before '}' token
       }
       ^
prog.c:81:7: error: unknown type name 'pthread_mutex_'
       pthread_mutex_
       ^
prog.c:82:8: error: expected declaration specifiers or '...' before '&' token
 unlock(&mutex);
        ^
prog.c: In function 'consumer':
prog.c:90:4: error: unknown type name 'buffer_item'
    buffer_item item;
    ^
prog.c:95:14: error: expected expression before ';' token
  RAND_DIVISOR;
              ^
prog.c:103:3: error: 'mutex' undeclared (first use in this function)
 (&mutex);
   ^
prog.c:104:10: warning: implicit declaration of function 'remove_item' [-Wimplicit-function-declaration]
       if(remove_item(&
          ^
prog.c:107:1: warning: missing terminating " character
 "Consumer report error
 ^
prog.c:107:1: error: missing terminating " character
prog.c:108:1: error: stray '\' in program
 condition\n");
 ^
prog.c:108:1: error: 'condition' undeclared (first use in this function)
prog.c:108:11: error: expected ')' before 'n'
 condition\n");
           ^
prog.c:108:12: warning: missing terminating " character
 condition\n");
            ^
prog.c:108:11: error: missing terminating " character
 condition\n");
           ^
prog.c:109:7: error: expected ';' before '}' token
       }
       ^
prog.c:111:17: warning: missing terminating " character
          printf("consumer
                 ^
prog.c:111:10: error: missing terminating " character
          printf("consumer
          ^
prog.c:112:2: error: 'consumed' undeclared (first use in this function)
  consumed %d\n", item);
  ^
prog.c:112:2: error: stray '\' in program
prog.c:112:12: error: 'd' undeclared (first use in this function)
  consumed %d\n", item);
            ^
prog.c:112:14: error: expected ')' before 'n'
  consumed %d\n", item);
              ^
prog.c:112:15: warning: missing terminating " character
  consumed %d\n", item);
               ^
prog.c:112:14: error: missing terminating " character
  consumed %d\n", item);
              ^
prog.c:113:7: error: expected ';' before '}' token
       }
       ^
prog.c:116:7: error: unknown type name 'pthread_mutex_'
       pthread_mutex_
       ^
prog.c:117:8: error: expected declaration specifiers or '...' before '&' token
 unlock(&mutex);
        ^
prog.c: At top level:
prog.c:124:17: error: unknown type name 'buffer_'
 int insert_item(buffer_
                 ^
prog.c:144:17: error: unknown type name 'buffer_'
 int remove_item(buffer_
                 ^
prog.c: In function 'main':
prog.c:170:23: warning: missing terminating " character
       fprintf(stderr, "
                       ^
prog.c:170:7: error: missing terminating " character
       fprintf(stderr, "
       ^
prog.c:171:1: error: 'USAGE' undeclared (first use in this function)
 USAGE:./main.out <INT> <
 ^
prog.c:171:6: error: expected ')' before ':' token
 USAGE:./main.out <INT> <
      ^
prog.c:172:1: error: stray '\' in program
 INT> <INT>\n");
 ^
prog.c:172:13: warning: missing terminating " character
 INT> <INT>\n");
             ^
prog.c:172:1: error: missing terminating " character
 INT> <INT>\n");
 ^
prog.c:173:4: error: expected ';' before '}' token
    }
    ^
prog.c:194:3: error: 'attr' undeclared (first use in this function)
 ,&attr,producer,NULL);
   ^
prog.c:209:4: error: unterminated comment
    /* Exit the program *
    ^
prog.c:208:4: error: expected declaration or statement at end of input
    sleep(mainSleepTime);
    ^
stdout
Standard output is empty