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 <unistd. h>
2. Get the parent process ID: pid_t getppid (void)
A) # include <unistd. h>
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 <unistd. h>
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 <unistd. h>
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 <unistd. h>
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 <unistd. h>
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 <unistd. h>
E) int system (const char * string)
1. Example: system ("ls-al/etc/passwd ")
2. # include <stdlib. h>
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 <sys/types. h>
C) # include <sys/wait. h>
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