Objectives of this section:
- Copy process image
- Fork system call
- Orphan and botnets
- Copy at write time
1. Process replication (or generation)
The child process obtained by using the fork function inherits the address space of the entire process from the parent process, including: process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, etc.
The difference between a child process and a parent process is:
1. The sub-process does not inherit the lock set by the parent process (because the sub-process is inherited due to exclusive locks)
2. The process IDs and parent process IDs are different.
3. Pending alarms of sub-processes are cleared;
4. the pending Signal Set of the sub-process is set to an empty set.
Ii. fork system call
Contains the header file <sys/types. h> and <unistd. h>
Function: create a sub-process
Function prototype
Pid_t fork (void );
Parameter: No parameter.
Return Value:
- If a child process is successfully created, the child process ID is returned for the parent process.
- If a child process is successfully created, the return value is 0 for the child process.
- -1 indicates creation failed.
Flowchart:
<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (= (pid =-(pid = (pid> printf (
Running result:
<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (= (pid =-(pid = (pid>
The above program is basically the same as the previous one, that is, to let the parent process sleep for 100 seconds so that the child process exits first.
Running result:
<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (val = pid = (pid =-(pid = ++ printf (pid> printf (
When fork is called:
Running result:
<Unistd. h> <stdlib. h> <fcntl. h> ERR_EXIT (m) \ (main (= open (fd =-= (pid =-(pid =, (pid> write (fd ,,
Running result:
It can be seen that the parent-child process shares the file offset pointer. After the parent process writes the file offset to the parent, the child process starts to write.