A deep understanding of the Linux kernel (2) Process
The program is static, and the process is an instance of the program being executed. A program can be composed of multiple processes. The process is the entity for resource allocation.
After a process is created, the child process is almost the same as the parent process. the child process copies the address space of the parent process and starts to execute the first command after fork (). It has the same executable code as the parent process (except for exec calls ). although the child process has the same Program Execution Code as the parent process, the child process has its own stack and heap. Therefore, the modification of data by the child process is invisible to the parent process.
As mentioned above, the child process copies the address space and data of the parent process. therefore, this replication follows the cow principle. after a child process is created, the kernel marks the data of the parent process as read-only. the child process shares the data of the parent process. Only when the child process needs to modify (write) the data will it copy the data from the parent process to the address space of the child process.
Even so, sub-processes in the user space still inherit many things from the parent process:
- Files opened by the parent process, including file descriptors and offset
- Real user ID, real group ID, valid tive ID, valid tive group ID
- Set-user-ID, set-group-ID Flag
- Current working directory
- Create a file pattern mask
It refers to the most basic permissions given to a file when a file is created. For example, 055.
The sub-process has the same signal processing method as the parent process. For example, if the parent process ignores SIGINT, the sub-process also has a SIGINT signal. Of course, the sub-process can change the processing method.
- Open the close-on-exec flag of the file
The sub-process differs from the parent process:
- The return value of Fork () is different.
Fork () returns the sub-process number in the parent process, and returns 0 in the sub-process;
- Different Process numbers
- Different parent process IDS
- The filelock of the parent process is not inherited by the quilt process.
- The suspended alarm is cleared.
- The signal set is set to null.
In the new Linux (2.6) kernel, multithreading is well supported. In older Linux versions, threads are treated as common processes or delivered to user space processing threads. A lightweight process is proposed for the new Linux version. in the kernel, a lightweight process is used to represent a thread, and the user space thread is associated with the Lightweight Process in the kernel, so that the user space thread can be managed and scheduled. the Lightweight Process shares the data of the parent process. in this way, we can achieve good support for multithreading.
Each process has a task_struct struct in the kernel, which contains all the information required by a process and puts the task_struct into a list of tasks in a two-way linked list.
I will not repeat the task_struct structure here. For more information, seeLinux Kernel Development
Copyright statement:
Reprinted Article please indicate the original source http://blog.csdn.net/feiyinzilgd/archive/2010/09/15/5885640.aspx
Contact tan Haiyan or go to tan Haiyan's personal homepage to leave a message.