Windows, Linux Create child processes

Source: Internet
Author: User

Creating a child process under Windows more commonly used API is CreateProcess, you can start a new process in the following ways:

Startupinfo si = {0};
process_information pi = {0};
SI.CB = sizeof (STARTUPINFO); Structure size
Getstartupinfo (&SI); Initialize structure
Si.wshowwindow = Sw_hide; New process does not show window
Si.dwflags = Startf_useshowwindow; Make Si.wshowwindow effective
Key steps, CreateProcess function parameter meaning please refer to MSDN
if (! CreateProcess (NULL,//executable file name, NULL when supplied by szCmdLine
szCmdLine,//command-line arguments
NULL,//points to a security_attributes struct, which determines whether the returned handle can be inherited by the quilt process.
If the lpprocessattributes parameter is empty (null), then the handle cannot be inherited.
NULL,//points to a security_attributes struct, which determines whether the returned handle can be inherited by the quilt process.
If the lpthreadattributes parameter is empty (null), then the handle cannot be inherited.
FALSE,//Indicates whether the new process inherited a handle from the calling process
NULL,//Specify additional flags to control the creation of priority classes and processes
NULL,//the environment block that points to a new process. If this parameter is empty, the new process uses the environment of the calling process
NULL,//The working path of the child process, which must be filled when the process name is a relative path
&si,//points to a STARTUPINFO structure that determines how the main form of a new process is displayed
&PI))//point to a process_information structure that receives information about the new process
{
return false;
}

WaitForSingleObject (Pi.hprocess,infinite); Wait for the child process to end
CloseHandle (Pi.hthread); Close the main thread of the new process
CloseHandle (pi.hprocess); Close New Process

To create a child process under Linux:

pid_t pid;
if (PID = fork ()) = =-1)//Create child process
{
Exit (0); Failed to create process, exit program
} else if (PID = = 0) {//child process space

Char *args[]={filemonitor, "-FP", Lmonitorlist[num].lm_strdirectory, NULL};
if (Execve (FileMonitor, args, NULL) < 0) {//Call the FileMonitor process in a child process

           printf ("Execve./WISFILEMONITOR-FP%s failed in the" process! /n ", lmonitorlist[num].lm_strdirectory);
            create_thread (Startmonitorthread, (thread_param) num);  
       
   } else {                                                                            //Parent process Space
        lmonitorlist[num].lm_pid = pid;
        Lmonitorlist[num].lm_bstop=false;
   }

If no error occurs while creating the child process, the fork function returns two times, one time in the parent process, and another in the child process. The fork function returns the newly created child process ID to the parent process and returns 0 to the child process.

Windows, Linux Create child processes

Related Article

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.