Linux kernel-process management
1. Process
#内核调度的对象是线程, not a process
#对Linux而言, a thread is just a special process
#进程提供两种虚拟机制: Virtual processor, virtual memory
#创建进程通过fork () to copy the creation process from the parent process
2. Process Descriptor
#任务队列: Doubly linked list (each item is task_struct---> process descriptor)
#Linux通过slab分配器分配task_struct
#内核通过唯一的进程标识值 (PID) to identify the process
#五种进程状态: 1.task_running Process Executable
2.task_interruptible can be interrupted
3.task_uninterruptible Non-disruptive
4._task_traced is tracked by other processes
5._task_stopped Process Stop Execution
#设置进程状态: Set_task_state (task, state) function and set_surrent_state (state) function
#所有进程都是PID为1的init进程的后代
#init进程描述符 Init_task
3. Process Creation
#fork () and exec ()
Fork () Copy current process Create child process
EXEC () reads the executable file and loads the space address to begin execution
#写时拷贝: Fork () uses write-time copy (Copy-on-write) to postpone the page copy of the address space until the actual write occurs
(The fork () executes exec immediately () without copying)
The actual cost of fork () is to copy the page table of the parent process and assign the process descriptor
#fork () execution process: Call fork () by system call Clone ()
Clone () calls Do_fork (), Do_fork () call Copy_process ()
Copy_process () Process:
1. Call Dum_task_struct () to create the kernel stack, thread_info, task_struct for the new process
2. Ensure that the current number of processes does not exceed the limit after the child process is created
3. The child process begins to differentiate from the parent process, and many members in the process descriptor initialize or clear 0
4. The status of the child process is set to task_uninterruptible
5. Call the Copy_flags () update to update the flags members of the TASK_STRUCT
6. Call Alloc_pid () to assign PID to the new process
7.copy_process () Copy shared open files, file system information, signal processing functions, process address space, and namespaces
8.copy_process () do the finishing work and return a pointer to the child process
#vfork (): Pass a special flag to the clone () system call for:
? ? 1. When Copy_process () is called, the Vfork_done member of TASK_STRUCT is set to null
? ? 2. If a given address is executed at Do_fork (), Vfork_done points to a specific address
? ? 3. After the child process starts executing, the parent process does not start execution immediately until the Vfork_done pointer signals it
? ? 4. Call the Mm_release () process to exit the address space and check if the Vfork_done is empty and wait for the parent process to send a signal if it is not empty
? ? 5. Back to Do_fork (), the parent process wakes up and returns
4. Implementation of threads in Linux
#线程机制提供了在同一程序中共享内存地址的空间运行的一组线程
#Linux将所有的线程当做进程来实现
#创建线程:
? Clone (CLONE_VM | Clone_fs | Clone_files | Clone_sighand, 0)
5. Process termination
#发生在进程调用exit () system call:
Most tasks are done by Do_exit ():
? ? 1. Set the flag member of the task_struct to pf_exiting
? ? 2. Call Del_timer_sync () to remove any kernel timers
? 3. If the BSD process is open, do_exit () calls Acct_update_integrals () to output the accounting information
? ? 4. Call EXIT_MM () to release the mm_struct that the process consumes
? ? 5. Call Sem_exit () and leave the queue if the process waits for the IPC signal
? ? 6. Call Exit_files () and Exit_fs ()
? ? 7. Set the task exit code stored in the Exit_code member of Task_struct to the exit code provided by exit ()
? ? 8. Call Exit_notify () to send a signal to the parent process, seek the adoptive father for the child process, the adoptive father is another thread in the thread group or INIT, and the process state is set to Exit_zombie
? ? 9.do_exit () call schedule () to switch to the new process
#删除进程描述符: Called Do_exit (), the process is dead, but the system also retains the process descriptor
When you release the process descriptor, Release_task () is called:
? 1. Call _exit_signal (), which calls _unhash_process (), which in turn calls Detach_pid () to remove the process from the Pidhash, and also to remove the process from the task list
? 2._exit_signal () Release the resources of the currently zombie process and make statistical records
? ? 3. If the process is the last process of the process group and the leader is dead, Release_task () notifies the lead process of the parent process
? 4.release_task () call Put_task_struct () frees the process kernel stack and the page occupied by Thread_info
#孤儿进程造成的进退维谷: If the parent process exits before the child process, there must be a mechanism to ensure that the child process can find the new father
Linux kernel-Process management