Advertisement
EBobkunov

hotfix ex1

Oct 3rd, 2023 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <time.h>
  6.  
  7. int main() {
  8.     pid_t pid1, pid2;
  9.     clock_t start_time, end_time;
  10.  
  11.     start_time = clock(); // Start measuring main process execution time
  12.  
  13.     printf("Main process: PID=%d, Parent PID=%d\n", getpid(), getppid());
  14.  
  15.     // Create the first child process
  16.     pid1 = fork();
  17.     if (pid1 < 0) {
  18.         perror("Fork failed");
  19.         return 1;
  20.     } else if (pid1 == 0) {
  21.         printf("Child 1: PID=%d, Parent PID=%d\n", getpid(), getppid());
  22.         return 0;
  23.     }
  24.  
  25.     // Create the second child process
  26.     pid2 = fork();
  27.     if (pid2 < 0) {
  28.         perror("Fork failed");
  29.         return 1;
  30.     } else if (pid2 == 0) {
  31.         printf("Child 2: PID=%d, Parent PID=%d\n", getpid(), getppid());
  32.         return 0;
  33.     }
  34.    
  35.     // Wait for both child processes to complete
  36.     waitpid(pid1, NULL, 0);
  37.     waitpid(pid2, NULL, 0);
  38.  
  39.     end_time = clock(); // Stop measuring main process execution time
  40.     double execution_time_ms = (double)(end_time - start_time) * 1000 / CLOCKS_PER_SEC;
  41.     printf("Main process execution time: %.2f ms\n", execution_time_ms);
  42.  
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement