Guest User

Untitled

a guest
Jan 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. int main(int argc, char *argv[]) {
  8.     pid_t pid;
  9.     int status, input, i;
  10.     input = atoi(argv[1]);
  11.  
  12.     for (i=0; i < input; i++) {
  13.     //while(1) {
  14.         pid = fork();
  15.         switch(pid){
  16.             printf("\t child pid = %d\n", getpid());
  17.             case -1:
  18.                 printf("case -1");
  19.                 fatal("fork failed");
  20.                 break;
  21.             case 0:
  22.                 /*Child calls exec*/
  23.                 execl("sleep.exe", "sleep.exe", (char *)0);
  24.                 printf("\tchild pid = %d\n", getpid());
  25.                 printf("\tchild ppid = %d\n", getppid());
  26.                 break;
  27.             default:
  28.                 /*Parent uses wait to suspend execution until child finishes */
  29.                 printf("\tparent pid = %d\n", getpid());
  30.                 printf("\tparent pid = %d\n", getppid());
  31.                 printf("\tchild exited with %d\n", status);
  32.                 exit(0);
  33.         }
  34.         waitpid(pid, &status, 0);
  35.     }
  36. }
  37.  
  38. int fatal(char *s) {
  39.     perror(s);
  40.     exit(1);
  41. }
Add Comment
Please, Sign In to add comment