1 #include <errno.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <stdio.h> 5 #include <unistd.h> 6 #include <sys/types.h> 7 #include <sys/wait.h> 8 9 #include <sys/procdesc.h> 10 11 int main() { 12 int procfd; 13 int rc = pdfork(&procfd, 0); 14 if (rc < 0) { 15 fprintf(stderr, "pdfork() failed rc=%d errno=%d %s\n", rc, errno, strerror(errno)); 16 exit(1); 17 } 18 if (rc == 0) { // Child process 19 sleep(1); 20 exit(123); 21 } 22 fprintf(stderr, "pdfork()ed child pid=%ld procfd=%d\n", (long)rc, procfd); 23 sleep(2); // Allow child to complete 24 pid_t child = waitpid(-1, &rc, WNOHANG); 25 if (child == 0) { 26 fprintf(stderr, "waitpid(): no completed child found\n"); 27 } else if (child < 0) { 28 fprintf(stderr, "waitpid(): failed errno=%d %s\n", errno, strerror(errno)); 29 } else { 30 fprintf(stderr, "waitpid(): found completed child %ld\n", (long)child); 31 } 32 return 0; 33 } 34