Debian Codegraph @

listings/chapter-3/fork.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 int main ()
12 {
13 pid_t child_pid;
14
15 printf ("the main program process id is %d\n", (int) getpid ());
16
17 child_pid = fork ();
18 if (child_pid != 0) {
19 printf ("this is the parent process, with id %d\n", (int) getpid ());
20 printf ("the child's process id is %d\n", (int) child_pid);
21 }
22 else
23 printf ("this is the child process, with id %d\n", (int) getpid ());
24
25 return 0;
26 }