fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "pthread.h"
  4. #include "semaphore.h"
  5.  
  6. sem_t mutex;
  7. int s = 0;
  8.  
  9. void* job (void* id) {
  10. printf("Start thread ID #%d\n", (int) id);
  11. while(1) {
  12. //sleep(1); //(1)
  13. sem_wait(&mutex);
  14. //sleep(1); //(2)
  15. printf("Thread #%d is doing math. %d + 1 = %d.\n", (int) id, s, s+1);
  16. s++;
  17. //sleep(1); //(3)
  18. sem_post(&mutex);
  19. sleep(1); //(4)
  20. }
  21. }
  22.  
  23. int main() {
  24. pthread_t thread[10];
  25.  
  26. int i;
  27. sem_init(&mutex, 0, 1);
  28. for (i = 0; i<10; ++i)
  29. pthread_create(&(thread[i]), NULL, job, (void*) i);
  30.  
  31. sleep(100);
  32. }
  33.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘job’:
prog.c:19: warning: implicit declaration of function ‘sleep’
prog.c: In function ‘main’:
prog.c:32: warning: control reaches end of non-void function
/home/outB8N/ccWyCt1j.o: In function `main':
prog.c:(.text+0x2e): undefined reference to `sem_init'
prog.c:(.text+0x56): undefined reference to `pthread_create'
/home/outB8N/ccWyCt1j.o: In function `job':
prog.c:(.text+0xa8): undefined reference to `sem_wait'
prog.c:(.text+0xe4): undefined reference to `sem_post'
collect2: ld returned 1 exit status
stdout

Standard output is empty