Profile photo for Andrew Weimholt

Here’s help on your homework assignment…

  1. #include <pthread.h> 
  2. #include <unistd.h> 
  3. #include <stdlib.h> 
  4. #include <stdio.h> 
  5. #include <time.h> 
  6.  
  7. int val=0; 
  8.  
  9. void *foo(void *arg) 
  10. { 
  11. sleep(*(int *)arg); 
  12. val = *(int *)arg; 
  13. return NULL; 
  14. } 
  15.  
  16. int main() 
  17. { 
  18. // select two random numbers from 1 to 10 
  19. srand(time(NULL)); 
  20. int a=1 + rand() % 10; 
  21. int b=1 + rand() % 10; 
  22. printf("inputs are %d %d\n\n", a, b); 
  23. fflush(stdout); 
  24. // print the larger of the two without using any conditional statements 
  25. pthread_t t[2]; 
  26. pthread_create(&t[0],NULL,foo,(void *)&a); 
  27. pthread_create(&t[1],NULL,foo,(void *)&b); 
  28. pthread_join(t[0],NULL); 
  29. pthread_join(t[1],NULL);  
  30. printf("the larger number is %d\n",val); 
  31. return 0; 
  32. } 

[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

View 17 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025