Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4.  
  5.  
  6.  
  7. int fib(int x) { /* slow/recursive implementation of Fib */
  8. int i, rint = (rand()%30); double dummy;
  9. for (i=0; i<rint*100; i++) {dummy=2.345*i*8.765/1.234;}
  10. if (x==0) return(0); else if (x==1) return(1); else return(fib(x-1)+fib(x-2));
  11. }
  12.  
  13. struct timeval stop, start;
  14. /*gettimeofday(&start, NULL);
  15. //do stuff
  16. gettimeofday(&stop, NULL);
  17. printf("took %lu\n", stop.tv_usec - start.tv_usec);*/
  18.  
  19. int main()
  20. {
  21. int pidp = 0;
  22. int pidc1 = 0;
  23. int pidc2 = 0;
  24.  
  25. pidp = getpid();
  26.  
  27. gettimeofday(&start, NULL);
  28. if((pidc1 = fork()) == 0)
  29. {
  30. pidc1 = getpid();
  31. printf("from C1: own PID=%d, parent's PID=%d\n", pidc1, pidp);
  32. fib(20);
  33. }
  34. else
  35. {
  36. wait();
  37. if((pidc2 = fork()) == 0)
  38. {
  39. pidc2 = getpid();
  40. printf("from C2: own PID=%d, parent's PID=%d\n", pidc2, pidp);
  41. fib(20);
  42. }
  43. else
  44. {
  45. wait();
  46. gettimeofday(&stop, NULL);
  47. printf("from P0: own PID=%d, PID of C1=%d, PID of C2=%d, total elapsed time in milliseconds=%lu\n", pidp, pidc1, pidc2, stop.tv_usec - start.tv_usec);
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement