fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <pthread.h>
  4. #include <time.h>
  5.  
  6. struct snail_thread{
  7. int move;
  8. char snail_name[10];
  9. char owner[10];
  10. };
  11.  
  12. int sum = 0;
  13.  
  14. void printval(void *ptr) {
  15. struct snail_thread *data;
  16. data = (struct snail_thread *) ptr;
  17.  
  18. while(sum < 100) {
  19. sum += data->move;
  20. printf("%s moves %d mm, total: %d\n",data->snail_name, data->move, sum);
  21. }
  22.  
  23. pthread_exit(0);
  24. }
  25.  
  26. int main(void) {
  27. pthread_t t[6];
  28. struct snail_thread s[6];
  29. int i;
  30.  
  31. srand(time(NULL));
  32.  
  33. for(i = 0; i < 6; i++)
  34. s[i].move = rand() % ((5 + 1) - 1) + 1;
  35.  
  36. strcpy(s[0].snail_name, "Snail A");
  37. strcpy(s[0].owner, "Jon");
  38.  
  39. strcpy(s[1].snail_name, "Snail B");
  40. strcpy(s[1].owner, "Ben");
  41.  
  42. strcpy(s[2].snail_name, "Snail C");
  43. strcpy(s[2].owner, "Mark");
  44.  
  45. strcpy(s[3].snail_name, "Snail D");
  46. strcpy(s[3].owner, "Jon");
  47.  
  48. strcpy(s[4].snail_name, "Snail E");
  49. strcpy(s[4].owner, "Mark");
  50.  
  51. strcpy(s[5].snail_name, "Snail F");
  52. strcpy(s[5].owner, "Ben");
  53.  
  54.  
  55. for(i = 0; i < 6; i++)
  56. pthread_create(&t[i],NULL,(void *) &printval, (void *) &s[i]);
  57.  
  58. for(i = 0; i < 6; i++)
  59. pthread_join(t[i], NULL);
  60.  
  61. return (0);
  62. }
  63.  
  64.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:31:2: warning: implicit declaration of function ‘srand’ [-Wimplicit-function-declaration]
  srand(time(NULL));
  ^
prog.c:34:3: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration]
   s[i].move = rand() % ((5 + 1) - 1) + 1;
   ^
/home/wPRHgp/ccA4vDOr.o: In function `main':
prog.c:(.text.startup+0x136): undefined reference to `pthread_create'
prog.c:(.text.startup+0x159): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty