Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/time.h>
- int fib(int x) { /* slow/recursive implementation of Fib */
- int i, rint = (rand()%30); double dummy;
- for (i=0; i<rint*100; i++) {dummy=2.345*i*8.765/1.234;}
- if (x==0) return(0); else if (x==1) return(1); else return(fib(x-1)+fib(x-2));
- }
- struct timeval stop, start;
- /*gettimeofday(&start, NULL);
- //do stuff
- gettimeofday(&stop, NULL);
- printf("took %lu\n", stop.tv_usec - start.tv_usec);*/
- int main()
- {
- int pidp = 0;
- int pidc1 = 0;
- int pidc2 = 0;
- pidp = getpid();
- gettimeofday(&start, NULL);
- if((pidc1 = fork()) == 0)
- {
- pidc1 = getpid();
- printf("from C1: own PID=%d, parent's PID=%d\n", pidc1, pidp);
- fib(20);
- }
- else
- {
- wait();
- if((pidc2 = fork()) == 0)
- {
- pidc2 = getpid();
- printf("from C2: own PID=%d, parent's PID=%d\n", pidc2, pidp);
- fib(20);
- }
- else
- {
- wait();
- gettimeofday(&stop, NULL);
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement