You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

35 lines
933 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. int main() {
  8. clock_t pt = clock(), t1, t2;
  9. int status;
  10. printf("Main. PID: %d\nParent PID: %d\n\n", getpid(), getppid());
  11. pid_t child1 = fork(), child2;
  12. t1 = clock();
  13. if (child1 != 0) {
  14. child2 = fork();
  15. t2 = clock();
  16. }
  17. if (child1 == 0) {
  18. printf("Child1. PID: %d\nParent PID: %d\n", getpid(), getppid());
  19. t1 = clock() - t1;
  20. printf("Child1. Time took: %fms\n\n", ((double)t1)/CLOCKS_PER_SEC*1000);
  21. } else if (child2 == 0) {
  22. printf("Child2. PID: %d\nParent PID: %d\n", getpid(), getppid());
  23. t2 = clock() - t2;
  24. printf("Child2. Time took: %fms\n\n", ((double)t2)/CLOCKS_PER_SEC*1000);
  25. } else {
  26. pt = clock() - pt;
  27. printf("Main. Time took: %fms\n\n", ((double)pt)/CLOCKS_PER_SEC*1000);
  28. }
  29. }