Creation of processes under Linux (System (); Fork (); exec* ())

Source: Internet
Author: User

0. System ();

The system () function calls the shell program to execute the command (low efficiency), which is equivalent to fork (), then Execve (); Features: The original process and the sub-process each run, and the original process needs to wait for the child process to run, and then continue;

1. Fork ();

References: Linux fork method to create multiple sub-processes simultaneously (i)

In Linux, you use fork () to create multiple sub-processes with a parent process in the following format:

1 intStatus,idx;2 3  for(idx =0; IDX <Ten; idx++) {4Status =fork ();5   if(Status = =0|| Status = =-1) Break;//each time the loop is discovered, the child process is found to jump directly from the loop that created the child process, not letting you go into the loop, which ensures that only the parent process does the work of looping through the creation of the child process at a time6 }7 8 if(Status = =-1) {9   //ErrorTen}Else if(Status = =0) {//code to be executed by each child process One   //Sub Process A}Else { -   //Parent Process -}

Reference: Process Control: Fork in Linux creating multiple sub-processes at the same time considerations

    /*You can not create a child process here, you want to create-"judge-" use-"return or exit. Not much more like test2.c *childpid1 = fork ();     *childpid2 = fork ();     *CHILDPID3 = fork (); */Childpid1= Fork ();//Create    if(0= = CHILDPID1)//Judging{//Enterprintf"In child1 process\n"); printf ("\tchild pid =%d\n", Getpid ());                                     Exit (exit_success);     //Exit}

2. exec* ();

Reference documents:

Linux system Programming Process (V): EXEC series functions

EXECL Learning under Linux

Errors in the compilation:

Warning:not enough variables to fit a sentinel appears when using the Execl () function

Workaround-Adjust the parameters in the EXECL

Creation of processes under Linux (System (); Fork (); exec* ())

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.