daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5.  
  6.  
  7. int main(int argc, char *argv[]) {
  8.  
  9.     if (argv[1] == NULL) {
  10.         printf("Desired amount of child processes must be passed as argument! \n");
  11.         exit(-1);
  12.     }
  13.  
  14.     char *endp;
  15.     int childQuantity = (int) strtol(argv[1], &endp, 0);
  16.     if (*endp != 0) {
  17.         printf("Conversion from value: %s of type string to value of type long failed! \n", argv[1]);
  18.     }
  19.  
  20.     int returnStatus;
  21.     pid_t pid;
  22.  
  23.     for (int i = 0; i < childQuantity; i++) {
  24.         pid = fork();
  25.         if (pid < 0) {
  26.             printf("Fork failed its work! \n");
  27.         }
  28.         if (pid == 0) {
  29.             sleep(1);
  30.         }
  31.  
  32.         if (pid > 0) {
  33.             waitpid(pid, &returnStatus, 0);
  34.             if (returnStatus == 0) {
  35.                 printf("Child process: %d | Parent process: %d \n", getpid(), getppid());
  36.                 kill(pid, 2563);
  37.                 printf("The child process terminated normally. \n");
  38.                 exit(0);
  39.             }
  40.             if (returnStatus == 1) {
  41.                 printf("The child process terminated with an error!. \n");
  42.             }
  43.         }
  44.     }
  45.  
  46.     return 0;
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top