Chapter III Process Management notes

Source: Internet
Author: User

Chapter III Process Management notes

20135109 Gao Yi Tong

3 , 1 Process

1, the procedure itself is not a process, the process is in the implementation period of the program and related resources collectively.

2. The thread of execution, or thread, is the active object in the process. Each thread has a separate counter, a process stack, and a set of process counters.

3. The kernel scheduler is a thread rather than a process, and for Linux, threading is a special process.

4. The process begins to survive when it is created, and the system calls to create a completely new process by copying an existing process, calling the fork () process as the parent process, and the newly generated process as a child process.

5. The fork () system call is returned from the kernel two times: once back to the parent process, once back to the newly generated child process.

3 , 2 process descriptors and task structures

1. The kernel holds the list of processes as a two-way loop list called the task queue, and each item in the linked list is a structure of type Task_struck, which becomes the process descriptor, defined in the <Linux/sched.h> file.

2. The process descriptor contains all the information of a process, including: Process address space, pending signal, status of the process, etc.

3, Linux through the slab allocator distribution Task_struck structure, this can achieve object reuse and cache coloring purposes. Since the slab allocator dynamically generates task_struck, it is only necessary to create a new structure struck Thread_info on the bottom of the stack (for a downward-growing stack) or on the top of the stack (for an upward-growing stack).

4. The THREAD_INFO structure of each task is allocated at the low end of its kernel stack, and the task field in the structure holds a pointer to the actual task_struck of the job.

5, the kernel through a unique process identity value or PID to identify each process, the PID is actually an int type.

6, in the kernel, access tasks often need to get pointers to their task_struct, in essence, most of the kernel processing process code is directly through the task_struct. Some hardware can come up with a special register to hold the task_struct pointer of the current process, speed up the access speed, and want to x86 such an architecture, only at the end of the stack to create thread_info structure, by calculating the offset indirectly find task_struct structure.

7. The state domain in the process descriptor describes the current status of the process, and each process in the system must be in one of these 5 states:

Task_running (running)

Task_interruptible (can be interrupted)

Task_uninterruptible (non-disruptive)

_task_traced--is tracked by other processes

_task_stopped--(stop) process stops executing

8, the kernel often needs to adjust the state of a process, it is best to use the Set_task (task,state) function, this function sets the specified process to the specified state.

8, executable code is an important part of the process, the code is loaded into the process in the address space of the execution, the general program in the user space execution. We say that the kernel "represents processes" executes and is in the context of the process.

9. All processes are descendants of the init process with PID 1, and the kernel starts the INIT process in the final phase of system startup.

Each process in the system must have a parent process, each of which can have 0 or more child processes, each containing a task_struct that points to its parent process, called the parent pointer, and a children list of word processes. task_struct

3 , 3 Process Creation

1, the process first in the new address space to create a process, read into the executable file, finally began to execute, fork () by copying the current process of a child process, the child process and the parent process is the difference between PID, PPID, etc., the exec () function is responsible for reading the executable file and loading it into the address space to start running.

2. Copy when writing:

The fork () of Linux is implemented using a write-time copy (copy-on-weite) page, where the actual cost of fork () is to copy the page table of the parent process and create a unique process description for the child process.

3. Fork ():

(1) Linux calls Fork () through the Clone () system and then the Clone () to dispatch do_fork ()

(2) Do_fork () created most of the work, defined in the Kernel/fork.c file, called the copy_process () function, and the process began to run.

(3) Workflow of the Copy_process function:

1) Call Dup_tast_struct () to create the kernel stack, thread_info structure, and task_struck for the process, and the parent process is exactly the same as the child process.

2) The child process begins to differentiate between the parent process.

3) The sub-process sets the status to task_uninterruptible and is guaranteed not to run.

4) copy_process () call Copy_flags () to update the flags member of the Task_struck.

5) Call Alloc_pid to assign PID to the new process

6) copy_process () Copy or share open folder, etc. according to the parameter flags passed to clone.

7) copy_process () to do the finishing work and return the child process pointer

8) Return to the Do_fork () function.

4, Vfork ():

(1) The vfork () system call and the fork () system call are essentially the same except for the page table that does not copy the parent process.

(2) The implementation of the Vfork () system call is performed by passing a special flag to the clone () system call:

1) when calling Copy_process (), the Vfor_done member of the Task_struck is set to null;

2) When executing do_fork, Vfor_done will point to a specific address

3) After the child process executes, the parent process does not execute immediately but continues to wait until the vfor_done of the child process sends a signal to it

4) when calling Mm_release, the process exits the memory address space, checks whether Vfor_done is empty, and sends a signal to the parent process if it is not empty.

5) Back to Do_fork (), the parent process continues to work.

3 , 4 Threads in Linux in the implementation

1, threading mechanism is a common abstract concept in modern programming mechanism, Linux has all the threads as a process to implement. Each of these threads has its own task_struck.

2. Thread creation is similar to normal process creation, except that when invoking Clone (), it is necessary to pass some parameter flags to indicate the resource to be shared, and the parameter flags passed to clone () determine how the new process is created and the type of resource shared between the parent and child processes.

3, the kernel thread and the normal process is the difference between the kernel thread does not have a separate address space (the MM pointer to the address space is pointed to null), they only run in the kernel space, never switch to the user state.

4, kernel process and normal process, can be scheduled or can be preempted.

5, Linux will give some tasks to the kernel thread to do, such as flush and KSOFIRQD.

3 , 5 Process End

1. At the end of the process, the kernel must release the resources it occupies and end up relying on do_exit ().

2, the task_struck structure of the child process is freed after the parent process obtains the information of the child process termination.

3. The standard action of Wait () is to suspend the process that invokes it until one of the child processes exits, and the function returns the PID of the child process.

4. Wait () is used to check the child processes and clear all the zombie processes associated with them.

5, if the parent process exits before the child process, there must be a mechanism to ensure that the child process can find a new father, if not, let Init do their parent process. Call Exit_notify () in exit (), the function calls Forget_original_parent (), and then call Find () _new_reaper () to find the parent process.

Chapter III Process Management notes

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.