"Linux kernel design and implementation" Chapter 3 reading notes

Source: Internet
Author: User

"Linux kernel design and implementation" Chapter 3 reading notes

Process management is the heart of all operating systems.

First, the process

1. A process is a general term for the procedure in which it is implemented and the resources it contains.

2. A thread is an object that is active in a process.

3. The process provides two virtual mechanisms: virtual processors and virtual memory.

4. The kernel dispatches an object that is a thread, not a process.

Ii. process descriptors and task structures

The kernel stores the list of processes in a two- way circular chain table called the task queue. Each item in the list is a process descriptor structure of type task_struct , which is defined in the <linux/sched.h> file.

1. Assigning process descriptors

Linux allocates task_struct structures through the slab allocator , which can achieve object reuse and cache coloring .

Dynamically generate task_struct with the slab allocator, simply create a new struct struct Thread_infoat the bottom or top of the stack .

2. Storage of process descriptors

The kernel identifies each process through a unique process identity value PID . The PID is a number, expressed as a pid_t implicit type, which is actually an int type, and the maximum value is set to 32768 by default.

3. Process status

The State field in the process descriptor is used to describe the current status of the process.

    • Task_running (Run): The process is executable, is executing, or waits for execution in the run queue
    • Task_interruptible (interruptible): The process is sleeping, which is blocked
    • Task_uninterruptible (non-interruptible): Same as interruptible status, received signal will not be awakened or ready to be put into operation
    • Task_traced: Processes that are tracked by other processes
    • Task_stopped (STOP): The process stops executing and the process is not operational or operational.

4. Set the current process state

It is best to use the Set_task_state (task,state) function to adjust the state of a process.

Function: This function sets the specified process to the specified state.

Equivalent to

task -> state = state

5. Process context

fall into kernel space : Executes from an executable file loaded into the address space of the process.

The process only passes through the interface to get into kernel execution.

6. Process Family Tree

All processes are descendants of the init process with PID 1.

Each process in the system must have a parent process that can have 0 or more child processes, and a process with the same parent process is called a sibling.

get the process descriptor of the parent process    : struct task_struct *my_parent = current->parent; Access child process    :struct task_struct *  task;     struct list_head *list;     &current->children) {            struct  task_struct, sibling);             /* task points to one of the current child processes */ }            

The process descriptor of the INIT process is statically allocated as Init_task.

get the next process in the list: List_entry (Task->tasks.next, struct task_struct, Tasks) Gets the previous process in the list: List_ The entry (task->tasks.prev, struct task_struct, Tasks) for_each_process (Task) macros provide the ability to access the entire task queue in turn, struct task_struct *task; for_each_process (Task) {/ * It prints out each task name and PID */ printk ("%s[%d]\n", Task->comm, task->pid);}

Third, process creation

Fork () Creates a child process by copying the current process.

(The child process differs from the parent process only in pid,ppid and some resources and statistics)

EXEC () is responsible for reading the executable file and loading it into the address space to start running.

1. Write-time copy

A write-time copy is a technique that delays or even eliminates copying data, and the kernel does not replicate the entire process address space, but instead lets the parent and child processes share a copy.

The replication of a resource occurs only when it is required to be written , and is previously shared in read-only mode.

2.fork ()

Do_fork defined in the Kernel/fork.c file, call copy_process

1234. Returns the Do_fork () function if the copy_process () function returns successfully, The newly created child process is awakened and put into operation.
3.vfork ()

Basically the same as the fork () function.

Advantages:

Page table entries for parent processes are not copied

The implementation of the Vfork () system call is done by passing a special flag to clone () .

    • When Copy_process () is called, the Vfor_done member of TASK_STRUCT is set to null.
    • When executing do_fork (), if given a specific flag, vfor_done points to a specific address.
    • After the child process begins execution, the parent process does not resume execution immediately, but waits until the child process sends a signal to it through the Vfor_done pointer.
    • When Mm_release () is called, the function is used for the process to exit the memory address space and to check if Vfor_done is empty, and if not NULL, a signal is sent to the parent process.
    • Back to Do_fork (), the parent process wakes up and returns.

Iv. implementation of threads in Linux

Linux implements all threads as processes.

1. Create a thread
    • The call to Clone () requires that some parameter flags be passed to indicate the shared resource.
0);

The new process and its parent process are threads.

    • An ordinary fork ().
0);
    • Implementation of Vfork ().

0);

The Clone () parameter flag determines how the new creation process behaves and the kind of resources that are shared between parent and child processes.

2. Kernel threads

Kernel threads do not have a separate address space, only in kernel space. Kernel threads can also be created by other threads.

After the kernel thread starts, it runs until it calls Do_exit.

V. The end of the process

At the end of the process, the kernel must release the resources it occupies and inform the parent process.

Reason:

Typically comes from itself, when called by the exit () system call.

    • An explicit invocation
    • Implicitly returning from the main function of a program
1. Delete the process descriptor

When you release the process descriptor, you need to call Release_task ().

2. The dilemma caused by the orphan process

If the parent process exits before the child process, there must be a mechanism to ensure that the child process can find a new parent, otherwise these orphaned processes will always be in a zombie state upon exiting.

Workaround:

    1. Find a thread in the current process group as the adoptive father
    2. Let Init be their parent process.

"Linux kernel design and implementation" Chapter 3 reading 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.