C   72

forkexec

Guest on 1st June 2022 01:34:54 AM

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5.  
  6. int
  7. main(int argc, char *argv[])
  8. {
  9.   int status;
  10.   int rc = fork();
  11.   if (rc < 0) {
  12.     perror("fork failed: ");
  13.     exit(1);
  14.   } else if (rc == 0) {
  15.     argv++;
  16.     execve(argv[0], argv, 0);
  17.   } else {
  18.     waitpid(rc, &status, 0);
  19.     printf("child %d exited with status %d\n", rc,
  20.            WEXITSTATUS(status));
  21.   }
  22. }

Raw Paste


Login or Register to edit or fork this paste. It's free.