Design and Implementation of Linux kernel (III)-Linux process
Source: Internet
Author: User
Linux kernel design and implementation (3)-the process of linux is the core concept of all operating systems, and is no exception in Linux. Main content: Process and thread process lifecycle process creation process termination 1. process and thread www.2cto.com process and thread...
Linux kernel design and implementation (3)-the process of linux is the core concept of all operating systems, and is no exception in Linux. Main content: Process and thread process lifecycle process creation process termination 1. processes and threads www.2cto.com processes and threads are in the dynamic state when the program is running. management operations of processes and threads (such as creation and destruction) are implemented by the kernel. Processes in Linux are lightweight compared with those in Windows, and do not strictly distinguish between processes and threads. threads are just a special process. So we will only discuss the process below. we will mention the thread only when the thread is different from the process. A process provides two virtual mechanisms: virtual processor and virtual memory. each process has an independent virtual processor and virtual memory, and each thread has an independent virtual processor, threads in the same process may share the virtual memory. The process information in the kernel www.2cto.com is mainly stored in task_struct (include/linux/sched. h) process id PID and thread id tid are equal for the same process or thread. In Linux, you can use the ps command to view the information of all processes: ps-eo pid, tid, ppid, comm 2. the transition between various states of a process constitutes the entire lifecycle of the process.
3. process creation in Linux is different from that in other systems. in Linux, process creation is divided into two steps: fork () and exec (). Fork: create a sub-process exec by copying the current process: Read the executable file and load it into the memory. run the created process: call dup_task_struct () to allocate a kernel stack for the new process, task_struct and so on. The content is the same as that of the parent process. Check the new process (whether the number of processes exceeds the upper limit, etc.) www.2cto.com to clear the information of the new process (such as setting the PID to 0), so that it is different from the parent process. The new process status is set to TASK_UNINTERRUPTIBLE to update the flags member of task_struct. Call alloc_pid () to assign a valid PID for the new process. copy or share the corresponding information based on the clone () parameter flag, perform some scanning and tail work, and return the fork () of the process created by the new process pointer () the function actually calls the clone () function. The process of creating a thread is the same as that of a process, but the parameters passed to the clone () function are different. For example, using a common fork to create a process is equivalent to: clone (SIGCHLD, 0) to create a shared address space with the parent process, file system resources, process of the file descriptor and signal processing program, that is, a thread: clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0) there is also a major difference between the kernel threads created in the kernel and common processes: the kernel threads do not have an independent address space and they can only run in the kernel space. Www.2cto.com, which is related to the Linux kernel. 4. like the process creation process, there are also many steps to terminate a process: the operation (do_exit) on the sub-process sets the identifier member in task_struct to PF_EXITING and calls del_timer_sync () to delete the kernel timer, make sure there is no timer in the queue and run the call exit_mm () to release the mm_struct occupied by the process call sem _ exit (), so that the process leaves the queue waiting for the IPC signal to call exit_files () and exit_fs (), release the file descriptor and file system resources occupied by the process, set the exit_code of task_struct to the return value of the process, and call exit_policy () to send a signal to the parent process, and set its status to EXIT_ZOMBIE to switch to the new process and continue executing the sub-process into EXIT_ZOMBIE. Although the sub-process will never be scheduled, the associated resources will be released, however, the memory occupied by it has not been released, such as the kernel stack allocated during creation and the task_struct structure. These are released by the parent process. After the action (release_task) parent process on the parent process receives the exit_policy () signal sent by the child process, it deletes the process descriptor of the child process and all the resources exclusive to the process. Www.2cto.com we can see from the above steps that every child process must have a parent process. what if the parent process has ended before the child process ends? The sub-process has taken this into account when calling exit_policy. If the parent process of the child process has exited, when the child process exits, the exit_policy () function will first call forget_original_parent () and then call find_new_reaper () to find a new parent process. The find_new_reaper () function first finds a thread in the current thread Group as the parent. if it cannot be found, let init act as the parent process. (The init process always exists when linux is started)
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