LINUX study note 11-process control programming

Source: Internet
Author: User
1. obtain the ID of the current process: pid_tgetpid (void). after adding this function to the program, you can obtain the idnumber of the process: printf (pid % d, getpid () when running the program ()); output in integer form a) # include & lt; unistd. h & gt; 2. obtain the parent process ID: pid_tgetppid (void... 1. obtain the ID of the current process: pid_t getpid (void). after adding this function to the program, you can get the ID of the current process: printf ("pid = % d ", getpid (); output in integer form
 
A) # include
 
2. get the parent process ID: pid_t getppid (void)
 
A) # include
 
3. create a sub-process: pid_t fork (void)
 
A) program: the created sub-process runs the code below the fork function in the parent process, shares the code segment, and copies the data segment again.
 
B) # include
 
C) role:
 
D) return value: a value is returned in both the parent and child processes.
 
1. ID: in the parent process, fork returns the ID of the child process.
 
2. negative value: an error occurs,
 
3. 0: In a sub-process, fork returns 0, which does not mean that the ID is 0.
 
4. create a sub-process: pid_t vfork (void)
 
A) # include
 
B) differences:
 
1. shared data segment
 
2. the child process runs first, and the parent process runs later. fork is not sure
 
5. replacement process: exec function family
 
A) role: the process number remains unchanged, but the new program is started to replace the original program.
 
B) Int execl (const char * path, const char * arg1 ,..., NULL)
 
1. Cxecl ("/etc/ls", "ls", "-al", "/etc/passwd", NULL)
 
2. Path: name of the program to be executed, including the complete Path
 
3. # include
 
4. Arg1-argn: the command line parameter required by the program to be executed. The 1st parameter is the program name and ends with a null pointer.
 
C) Int execlp (const char * path, const char * arg1 ,..., NULL)
 
1. Cxeclp ("ls", "ls", "-al", "/etc/passwd", NULL)
 
2. # include
 
3. Path: the name of the program to be executed, but it does not contain the complete PATH. find it from the Path command of LINUX.
 
D) Int execv (const char * path, const char * argv [], NULL)
 
1. Path: name of the program to be executed, including the complete Path
 
2. argv []: string array
 
3. # include
 
E) int system (const char * string)
 
1. example: system ("ls-al/etc/passwd ")
 
2. # include
 
F) function: call fork to generate a sub-process. The sub-process calls/bin/sh-c string to execute the command represented by the string parameter. It is equivalent to using shell to parse commands
 
6. process wait: pid_t wait (int * status)
 
A) Pid_t wait (NULL );
 
B) # include
 
C) # include
 
D) function: blocks the process until a sub-process exits.
 
E) return value: Process Number of the sub-process to exit
 
F) Status: return the process end Status value. If NULL is not required
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.