Demo Code to Fork the Child Process

Child process can be created by POSIX function fork(), and below is the demo code.

pid = fork();

if (pid == 0) {
    /* processing of child process */
} else if (pid < 0) {
    /* parent process: handle errors */
    perror("fork");
    exit(-1);
} else {
   /* parent process: the parameter of pid is the ID of forked child process */
}

And also, parent process could call function wait() to wait for finish of child process, instead of processing in the second return of function fork().

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.