man7.org > tlpi > code > online code > procexec/t_vfork.c

procexec/t_vfork.c

This is procexec/t_vfork.c (Listing 24-4, page 524), an example program file from the book The Linux Programming Interface.

The source code file is copyright 2010, Michael Kerrisk, and is licensed under the GNU Affero 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.

Function list (Bold in this list means a function is non-static)

#include "tlpi_hdr.h"
int
main(int argc, char *argv[])
{
    int istack = 222;

    switch (vfork()) {
    case -1:
        errExit("vfork");

    case 0:             /* Child executes first, in parent's memory space */
        sleep(3);                   /* Even if we sleep for a while,
                                       parent still is not scheduled */
        write(STDOUT_FILENO, "Child executing\n", 16);
        istack *= 3;                /* This change will be seen by parent */
        _exit(EXIT_SUCCESS);

    default:            /* Parent is blocked until child exits */
        write(STDOUT_FILENO, "Parent executing\n", 17);
        printf("istack=%d\n", istack);
        exit(EXIT_SUCCESS);
    }
}

 

Download procexec/t_vfork.c


This page copyright (C) 2013, Michael Kerrisk,   mtk AT man7.org