procexec/t_clone.cThis is procexec/t_clone.c (Listing 28-3, page 601), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
+/* t_clone.c + + Demonstrate the use of the Linux-specific clone() system call. +*/ #define _GNU_SOURCE #include <signal.h> #include <sys/wait.h> #include <fcntl.h> #include <sched.h> #include "tlpi_hdr.h" #ifndef CHILD_SIG #define CHILD_SIG SIGUSR1 /* Signal to be generated on termination of cloned child */ #endif static int /* Startup function for cloned child */ childFunc(void *arg) { if (close(*((int *) arg)) == -1) errExit("close"); return 0; /* Child terminates now */ } int main(int argc, char *argv[]) { const int STACK_SIZE = 65536; /* Stack size for cloned child */ char *stack; /* Start of stack buffer */ char *stackTop; /* End of stack buffer */ int s, fd, flags; fd = open("/dev/null", O_RDWR); /* Child will close this fd */ if (fd == -1) errExit("open"); /* If argc > 1, child shares file descriptor table with parent */ flags = (argc > 1) ? CLONE_FILES : 0; /* Allocate stack for child */ stack = malloc(STACK_SIZE); if (stack == NULL) errExit("malloc"); stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ /* Ignore CHILD_SIG, in case it is a signal whose default is to terminate the process; but don't ignore SIGCHLD (which is ignored by default), since that would prevent the creation of a zombie. */ if (CHILD_SIG != 0 && CHILD_SIG != SIGCHLD) if (signal(CHILD_SIG, SIG_IGN) == SIG_ERR) errExit("signal"); /* Create child; child commences execution in childFunc() */ if (clone(childFunc, stackTop, flags | CHILD_SIG, (void *) &fd) == -1) errExit("clone"); /* Parent falls through to here. Wait for child; __WCLONE is needed for child notifying with signal other than SIGCHLD. */ if (waitpid(-1, NULL, (CHILD_SIG != SIGCHLD) ? __WCLONE : 0) == -1) errExit("waitpid"); printf("child has terminated\n"); /* Did close() of file descriptor in child affect parent? */ s = write(fd, "x", 1); if (s == -1 && errno == EBADF) printf("file descriptor %d has been closed\n", fd); else if (s == -1) printf("write() on file descriptor %d failed " "unexpectedly (%s)\n", fd, strerror(errno)); else printf("write() on file descriptor %d succeeded\n", fd); exit(EXIT_SUCCESS); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.