Guest User

Untitled

a guest
Jan 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 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.     i = 0;
  12.  
  13.     while (i < input) {
  14.     //while(1) {
  15.         pid = fork();
  16.         switch(pid){
  17.             printf("\t child pid = %d\n", getpid());
  18.             case -1:
  19.                 printf("case -1");
  20.                 fatal("fork failed");
  21.                 break;
  22.             case 0:
  23.                 /*Child calls exec*/
  24.                 execl("sleep.exe", "sleep.exe", (char *)0);
  25.                 printf("\tchild pid = %d\n", getpid());
  26.                 printf("\tchild ppid = %d\n", getppid());
  27.                 break;
  28.             default:
  29.                 /*Parent uses wait to suspend execution until child finishes */
  30.                 printf("\tparent pid = %d\n", getpid());
  31.                 printf("\tparent pid = %d\n", getppid());
  32.                 printf("\tchild exited with %d\n", status);
  33.                 exit(0);
  34.         }
  35.     waitpid(pid, &status, 0);
  36.     i++;
  37.     }
  38. }
  39.  
  40. int fatal(char *s) {
  41.     perror(s);
  42.     exit(1);
  43. }
Add Comment
Please, Sign In to add comment