Fork system call

Source: Internet
Author: User
/** Simple fork usage */# include <unistd. h> # include <stdio. h> # include <stdlib. h> int main () {pid_t child; if (child = fork () =-1) {perror ("fork"); exit (exit_failure );} else if (child = 0) {puts ("in child"); printf ("\ tchild pid = % d \ n", getpid ()); printf ("\ tchild ppid = % d \ n", getppid (); exit (exit_success);} else {puts ("in parent "); printf ("\ tparent pid = % d \ n", getpid (); printf ("\ tparent ppid = % d \ n", getppid ());} return (exit_success );}

-----------------------------------------------

The first running result is as follows:

In parent

In child
Parent pid = 4836
Parent ppid = 4835.
Child pid = 4837

Child ppid = 4836

----------------------------------------------

The result of the second operation is changed as follows:

In parent
Parent pid = 4855
In child
Parent ppid = 4854.
Child pid = 4856
Child ppid = 4855

When fork is used, the running sequence of the husband process and sub-process is random, that is, its execution is asynchronous.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.