Child Process Creation through fork() in C
Solution 1
Upon successful completion, fork()
(source):
- shall return 0 to the child process
- and shall return the process ID of the child process to the parent process.
The example you gave is well explained. However, I would like to precise that Both processes (parent and child) shall continue to execute from the fork()
function.
if you would like to know the PID of the child (from the code of the child), use getpid
API.
Solution 2
After executing the fork() function, you have two processes, which both continue executing after the fork call. The only difference between the two processes is the return value of fork(). In the original process, the "parent", the return value is the process id (pid) of the child. In the new cloned process, the "child", the return value is 0.
If you wouldn't test the return value of fork(), both processes would be doing exactly the same.
NB: to understand why the fork() function is useful, you need to read what the exec() function is doing. This function loads a new process from disk, and replaces the caller process with the new process. The combination of fork() and exec() is actually the way to start a different process.
Solution 3
fork
is a function that returns twice - once for the parent, once for the child.
For the child, it returns 0
, for the parent the pid of the child, any positive number; for both processes, the execution continues after the fork.
The child process will run through the else if (pid == 0)
block, while the parent will run the else
block.
mino
Updated on April 06, 2020Comments
-
mino about 4 years
I'm completely new to C and learning about processes. I'm a little confused as to what the code below is actually doing, it's taken from Wikipedia but I've seen it in several books and am unsure as to why, for example, we do
pid_t pid;
thenpid = fork();
. My reading seem to suggest the child process returns apid
of0
, however, I thought the very original parent process will maintain thepid
of0
after seeing a tree with the root aspid 0
.What does, for example,
pid = fork();
do to the parent? As doesn't it do the same thing for the child? And doesn'tpid = fork();
put it into a loop as it will do this for each child?Basically, could someone explain each step to me as if I were, say, five? Maybe younger? Thanks!
#include <stdio.h> /* printf, stderr, fprintf */ #include <sys/types.h> /* pid_t */ #include <unistd.h> /* _exit, fork */ #include <stdlib.h> /* exit */ #include <errno.h> /* errno */ int main(void) { pid_t pid; /* Output from both the child and the parent process * will be written to the standard output, * as they both run at the same time. */ pid = fork(); if (pid == -1) { /* Error: * When fork() returns -1, an error happened * (for example, number of processes reached the limit). */ fprintf(stderr, "can't fork, error %d\n", errno); exit(EXIT_FAILURE); } else if (pid == 0) { /* Child process: * When fork() returns 0, we are in * the child process. */ int j; for (j = 0; j < 10; j++) { printf("child: %d\n", j); sleep(1); } _exit(0); /* Note that we do not use exit() */ } else { /* When fork() returns a positive number, we are in the parent process * (the fork return value is the PID of the newly created child process) * Again we count up to ten. */ int i; for (i = 0; i < 10; i++) { printf("parent: %d\n", i); sleep(1); } exit(0); } return 0; }
-
Some programmer dude about 11 yearsOn POSIX systems, the very first process it
init
, and it always have process id 1. No process ever have process id 0. -
mino about 11 years@JoachimPileborg If this is the case, then why is the pid value of the child process, according, to what I've read zero? Shouldn't it be a larger number than the parent?
-
unwind about 11 years@mino: The point of
fork()
returning 0 is to make it trivial to detect "aha, I'm in the child" since you typically want to do something different in that case (and your own PID is rarely interesting, and can be quickly found usinggetpid()
anyway). -
Some programmer dude about 11 yearsLike unwind said. The return value of
fork
in the child is not the childs process id, it's just a value that tells you that you are in the child. To get the childs process id usegetpid
.
-
-
mino about 11 yearsWhy would you need to use getpid f it will return the pid OF the child process to the parent process?
-
Bechir about 11 yearsThe parent process will get the child's PID as a return of the
fork()
function. If the child would like to get its own PID, you shall use thegetpid()
call.