Advertisement
EBobkunov

ex3

Oct 1st, 2023 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.     if (argc != 2) {
  7.         fprintf(stderr, "Usage: %s <n>\n", argv[0]);
  8.         return 1;
  9.     }
  10.  
  11.     int n = atoi(argv[1]);
  12.  
  13.     for (int i = 0; i < n; i++) {
  14.         pid_t child_pid = fork();
  15.  
  16.         if (child_pid == -1) {
  17.             perror("fork");
  18.             exit(1);
  19.         } else if (child_pid == 0) {
  20.             // Child process
  21.             printf("Child process %d created\n", getpid());
  22.             sleep(5); // Sleep for 5 seconds
  23.             exit(0);
  24.         }
  25.     }
  26.  
  27.     // Parent process
  28.     sleep(5 * n); // Sleep for 5 seconds per fork
  29.     return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement