I. chicken ribs
1,
Ii. process control programming:
1. Get ID:
(1) obtain the process ID: pid_t getpid (void );
(2) obtain the parent process ID: pid_t getppid (void)
How to use:
(1) You need to introduce the header file: # include <sys/types. h>, # include <unistd. h>
2. Process Creation-fork: pid_t fork (void );
How to use:
(1) After fork (), a new sub-process will be generated and the return value will be sent to both processes (the original process [becomes a parent process] and the new process) at the same time.
(2) return value. If fork is successfully created, the Process Code PID of the child process is returned in the parent process, and 0 is returned in the new process. If the creation fails,-1 is returned.
(3) Note: The created sub-process will "copy" the data, stack space, and other information defined in the parent process, and remind you again: the sub-process is "copying the data of the parent process to its own memory space". Therefore, the data modification of the sub-process does not affect the parent process.
3. Process Creation 2-vfork: pid_t vfork (void );
How to use:
(1) You need to introduce the header file: # include <sys/types. h>, # include <unistd. h>
(2) differences between vfork and fork:
I,
Fork: The child process copies the data segment of the parent process.
Vfork: Data Segment shared by sub-process and parent process
II,
Fork: The execution sequence of parent and child processes is unknown.
Vfork: the sub-process runs first, and the parent process runs later.
4. Exec function family: exec replaces the program that calls it with the program that is executed.
(1) differences with Fork
I. Fork creates a new process and generates a new PID
Ii. Execute to start a new program and replace the original process. Note: here the replacement only replaces the data and code of the original process, and the others remain unchanged. Therefore, the PID of the process will not change.
Common exec Functions
(2) int execl (const char * path, const char * arg1, char * arg2 ,..);
I. header files need to be introduced: # include <unistd. h>
Ii. parameters:
Path: name of the program to be executed (must contain the complete path)
Arg1 ~ Argn: the command line parameter required by the executed program. It must contain the program name. End with a null pointer.
Iii. eg: execl ("/bin/ls", "ls", "-Al", "/etc/passwd", (char *) 0 );
1 2 3 4 5
1 indicates the name of the program to be executed, including the complete path,
2. Write the program name again (excluding the path, only the program name can be written)
3 and 4 are the command line parameters required for Program 2.
5. The end is indicated by a null pointer (char * 0 ).
(3) variants of execl that do not contain paths: int execlp (const char * paht, const char * arg1 ,..);
Parameters:
I. Path: name of the program to be executed (excluding the path, the program will be searched from the $ path system environment variable)
Ii. arg1 ~ Argn: the command line parameter required by the executed program. Arg must contain the program name. End with a null pointer.
(4) convert arg1 ~ The execl variant imported by argn in an array: int execv (const char * path, char * const argv []);
Parameters:
I. Path: name of the program to be executed (the complete path must be included)
Ii. eg:
Char * argv {"ls", "-Al", "/etc/passwd", (char *) 0 };
Execv ("/bin/ls", argv );
5. System (const char * string );
How to use:
(1) Call fork to generate a sub-process (unlike execl, it is done by a sub-process, rather than replacing the code of the parent process with another program like execl ), then, the sub-process calls "/bin/sh-C string" to execute the command represented by the string parameter.
(2) introduce the header file: # include <stdlib. h>
6. Process wait: pid_t wait (int * status );
(1) After fork creates a process, the execution sequence of the Parent and Child processes is uncertain. In addition to using vfork to control the sub-process execution first, there is also a more flexible way to use wait, process wait function.
(2) function: blocks the current process until one of its sub-processes exits. Note that the sub-processes exit here, that is, even if the sub-process is sleep, the parent process will not be executed.
(3) You need to introduce the header file: # include <sys/types. h>, # include <sys/Wait. H>