Textbook Study Note 4: Chapter III 20135115 Xuan Wenjun

Source: Internet
Author: User

Section three Chapters Process Management

Note: Xuan Wenjun, original works reproduced please indicate the source.

First, the process

1, process management is the heart of all operating systems.

2, Process: is in the execution period of the program and the associated resources collectively, in fact, the process is executing the real-time results of the program code.

3. Thread of execution: Thread threads, is the object that is active in the process.

4. The kernel dispatches the object to be a thread, not a process.

5, for Linux, threading is a special process.

6, the process provides two kinds of virtual mechanism: virtual processor and virtual memory.

Virtual memory can be shared between threads in the same process, but each has its own virtual processor.

7. In a Linux system, call the fork () system call to create a completely new process by copying an existing process.

The process calling fork () is called the parent process, and the newly generated process is called a child process.

The fork () system call returns two times from the kernel: one time back to the parent process, and another to the newly generated child process.

Call the EXEC () function to create a new address space and load the new program into it.

Fork () is actually implemented by the clone () system call.

The program exits execution through the exit () system call.

Ii. process descriptors and task structures

1. The kernel stores the list of processes in a two-way circular chain table called the task queue.

2. Each item in the list is a structure of type task_struct, called the process descriptor, which contains all the information for a specific process.

The process descriptor is relatively large:

3. Assigning process descriptors

Linux allocates task_struct structures through the slab allocator to achieve object reuse and cache coloring.

4. Process Descriptor Storage

(1) The kernel identifies each process by a unique process identity value or PID.

(2) The maximum default setting for PID is 32768, which is actually the maximum number of simultaneous processes allowed in the system, which can be increased by the system administrator by modifying the/proc/sys/kernel/pid_max.

(3) The kernel stores the PID of each process in their respective process descriptors, looking through the current macro to the process descriptor of the currently running process.

5. Process Status: The State field in the process descriptor.

6. The kernel adjusts the state of a process using the set_task_state (task,state) function.

7, executable code is an important part of the process.

System calls and exception handlers are explicitly defined interfaces to the kernel.

8, the process has an inheritance relationship, so the process is PID 1 of the Init process descendants.

Third, process creation

1, the UNIX process is created by fork () and exec ().

Fork () Creates a child process by copying the current process, and the EXEC () function is responsible for reading the executable and loading it into the address space to start running.

2. The fork () of Linux is implemented using the write-time copy (Copy-on-write) page.

The actual cost of fork () is to copy the page table of the parent process and create a unique process descriptor for the child process.

3. The fork (), vfork (), and _clone () library functions call Clone () according to their respective parameter flags, and then the Clone () calls Do_fork (), Do_fork () calls the Copy_process () function, and then lets the process start running.

4. Vfork () has the same functionality as fork () Except for page table entries that do not copy the parent process.

Iv. implementation of threads in Linux

1. The threading mechanism provides a set of threads that run in shared memory address space within the same program.

The threading mechanism supports concurrent programming techniques, and it also guarantees true parallel processing on multiprocessor systems.

2. Linux implements all threads as processes, and threads are only considered a process that shares certain resources with other processes.

3. Thread creation is similar to the process, but it is necessary to pass some parameter flags when invoking clone () to indicate the resources that need to be shared.

The parent-child process shares the address space, file system resources, file descriptors, and signal handlers.

4. Kernel thread

(1) The kernel often needs to perform some operations in the background (example: FLUSH,KSOFIRQD), which are done through kernel threads.

(2) The kernel thread differs from the normal process: the kernel thread does not have a separate address space, only runs in kernel space and does not switch to user space.

Same: Can be scheduled and can be preempted.

(3) Kernel threads can only be created by other kernel threads, and the kernel automatically handles this by deriving all the new kernel thread from the Kthreadd kernel process.

(4) The newly created process is in a non-operational state and will not be actively run if it is not explicitly awakened by calling Wake_up_process ().

Create a process and let it run, which can be done by calling Kthread_run ().

V. The end of the process

1. When a process is terminated, the kernel must release the resources it occupies and inform the parent process.

2, most of the process end depends on Do_exit ().

3. The task_struct structure of the child process is freed after the parent process obtains information about the child process that has been terminated, or notifies the kernel that it is not concerned with the information.

Release_task () is called when the process descriptor is eventually required to be freed.

4, 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, otherwise these orphaned processes will always be in a zombie state at the exit, wasting memory.

Workaround: For the child process to find a thread within the current thread group as the father, if not, let Init as their parent process.

5. The init process routinely calls wait () to check its child processes, clearing all of its associated zombie processes.

Textbook Study Note 4: Chapter III 20135115 Xuan Wenjun

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.