Linux Chapter III study notes

Source: Internet
Author: User

Chapter III Process Management

Processes are the most basic of the Unix operating system abstraction concepts.

Process management is the heart of all operating systems.

First, the process

1. The process is a procedure at the time of implementation. In addition to executable code, it also includes open files, pending signals, kernel internal data, one or more execution threads, and many other resources

    • A thread is an object in a process activity; the kernel dispatches a thread rather than a process.
    • In Linux Systems, threads and processes are not differentiated
    • There may be two or more processes executing the same program, or even N processes sharing open files, address space, and the like

2. thread: Is the object that is active in the process. Each thread has a separate program counter, a process stack, and a set of process registers. the kernel dispatches an object that is a thread.

3. in modern operating systems, processes provide two virtual mechanisms: virtual processors and virtual memory. Virtual memory can be shared between threads in the same process, but each has its own virtual storage

4. the life cycle of the process

    • Fork (): Create a new process
    • EXEC (): Create a new address space and load the new program into it
    • Clone ():fork is actually implemented by clone
    • Exit (): Exit execution
    • WAIT4 (): Whether the parent process queries the child process for finalization
    • Wait (),waitpid (): The program exits execution and becomes zombie, calling both to destroy.

5. The process begins to survive at the time it was created, which is usually the result of invoking the fork System. The system call creates a completely new process by copying an existing process. the fork system call returns two times from the kernel, one time to the parent process, and another to the newly generated child process.

Ii. process descriptors and task structures

Process Descriptor: The process list is stored in a doubly linked list (tasklist) , which is the structure of the task_struct that is the process descriptor. The type is defined in <linux/sched.h> . The data contained in the process descriptor can fully describe an executing program:

    • It opens the file
    • The address space of the process
    • Suspended signal
    • Status of the process
    • Other more information

2.1 Assignment of process descriptors
    • Objective:Linux allocates task_ struct structure through Slab, in order to achieve object reuse and cache coloring, so as to avoid resource consumption caused by dynamic allocation and release of resources.
    • The task_ structis now dynamically generated with the slab allocator , so it is only necessary to create a new struct struct thread at the bottom of the stack (for a downward-growing stack) or at the top of the stack (for an upward-growing stack) _ Info

The thread_info structure of each task is allocated at the end of its kernel stack. The end of the stack for each task (for example, for an upward-growing stack, which is at the top of the stack) is a struct thread_ info, which points to the task_ struct struct

2.2 Storage of process descriptors

1. Most of the code in the kernel that processes the processing process is done through the task_ struct , so you need to find the process descriptor for the currently running process through the current macro

2. Use the current macro to find the process descriptor that is currently running the process. in the X86 system, current blocks the second effective bit of the stack pointer to calculate the thread_ info offset (via current_ thread_ info function)

MOVL $-8192,%eax

Andl%esp,%eax3. Process status

3. The kernel identifies each process by a unique process identity value PID . PID stored in the respective process descriptor.

2.3 Process Status

The process must be in one of five states at any time.

    • Task_running Run
    • Task_interrupt can be interrupted
    • Task_uninterrupt non-disruptive
    • Task_traced processes that are tracked by other processes
    • task_stopped process stops running

    • Task_running: May be running or may indicate an executable
    • Task_ interrupt/task_uninterrupt: both indicate that it is blocking, but the latter state will not be awakened after receiving a signal .
2.4 Setting the current state of the process

Call the set_ task_ State (task,state) function to set the process to the specified status

2.5 Process Context
    • Executable code is executed from an executable file loaded into the address space of the process. When a program executes a system call, the kernel " executes on behalf of the process" and is in the context of the process
    • Contrast: In an interrupt context, the system does not represent process execution - There is no process to interfere with these interrupt handlers
2.6 Process Family Tree

All processes are descendants of the init process with pid 1 .
The kernel initiates the init process at the last stage of system startup .

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.
This relationship is stored in the process descriptor, which points to the parent process task_struct, andchildren is the child process list.

Get the process descriptor of the parent process:

struct Task_struct *my_parent = current->parent;

To access a child process:

struct Task_struct *task;

struct List_head *list;

List_for_each (List,¤t->children) {

Task = list_entry (list, struct task_struct, sibling);

/* Task now 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);

Get the previous process in the list:

List_entry (Task->tasks.prev, struct task_struct, tasks);

The above relies on the two macro implementations of Next_task (Task) and prev_task (Task) .
For_each_process (Task) macro, which accesses the entire task queue, each access task pointer points to the next element in the list.

struct Task_struct *task;

For_each_process (Task) {

/* It prints out the name and PID of each task * /

PRINTK ("%s[%d]\n", Task->comm, Task->pid);

}

Third, process creation

how the Unix system processes are created

    • Fork () creates a child process by copying the current process
    • EXEC () is responsible for reading the executable file and loading it into the address space to start running
    • Linux fork () uses write-time copies to postpone or even dispense copies. The kernel does not replicate the entire address space when it creates a new process, but rather allows the parent and child processes to share the same copy, until the child process / parent process is required to write, before the copy is read as read-only.
    • the actual cost of fork is simply to copy the page table of the parent process and create a unique process descriptor for the child process
3.1 Write-time Copy 3.2 fork ()
  1. Linux implements fork via clone system call
  2. Fork (),vfork (), and__clone () call clone () according to their desired parameter flags . called by clone to call do_fork ()
  3. Define do_ Fork () in <kernel/fork.c> to complete most of the work in creation, which calls Copy_ process function, and then let the processes start running

※ The general kernel chooses the child process to execute first.
Why?
The normal child process calls the exec () function immediately, avoiding the extra overhead of copying at write time.

The last copy_process returned is a pointer to the child process.

3.3 Vfork ()

Thevfork () system call and the Fork () function are the same except for page table entries that do not copy the parent process. The child process runs as a separate thread of the parent process in its address space, and the parent process is blocked until the child process exits or executes exec ().

  vfork () the implementation of a system call is achieved by Clone () A system call is passed with a special flag :

  1. Call copy_process () Yes, the vfor_done member of Task_struct is set to NULL .
  2. When executing do_fork () , if given a specific flag, vfor_done points to a specific address.
  3. 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.
  4. 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.
  5. Back to do_fork (), the parent process wakes up and returns.
Iv. implementation of threads in Linux

The threading mechanism provides a set of threads that run in shared memory address space within the same program. In a Linux system, a thread is only considered a process that shares certain resources with other processes. Each thread has its own task_struct.

4.1 Creating Threads

1. thread creation is similar to normal process, except that you need to pass some parameter flags to indicate the shared resource when you call clone () .

2. The parameter flags passed to clone () determine how the new creation process behaves and what kind of resources are shared between parent and child processes

4.2 Kernel threads

Kernel threads differ from normal processes only in that kernel threads do not have a separate address space and never switch to user space and can be dispatched and preempted.

    • It can only be created through other kernel threads, and the kernel derives all kernel threads through the Kthread kernel process
    • The newly created thread is in a non-operational state until Wake_ up_process () explicitly wakes it
V. The end of the process

Most of the finalization process relies on do_exit () to complete:

Re-find the adoptive father for the child process (other threads in the thread group or the init process)

Call Schedule () to switch to a new process

After this, the process is not operational and is in the Exit_zonbie exit state, and all memory consumed is the kernel stack, thethread_info structure, and the task_struct structure. The only purpose of the process at this time is to provide information to its parent process.

5.1 Deleting a process descriptor
    1. This task is performed separately from cleanup work, because the system can still get its information after the process is terminated
    2. Implementation of Process descriptor deletion via release_task ()
    3. At this point, all resources have been released.
5.2 Resolving the Orphan process

1. Orphan process : The parent process exits before the process, leaving behind the child process, the orphan process

2. workaround : Find a new parent process within the current thread group for the orphan process;

Otherwise, direct init as its parent process

    • Call Order:Do_ exit ()-->forget_ Original_ parent ()-->find_ new_ parent ()-->ptrace_ exit_ finish ()

do_exit () exit_ Notify ()
exit_notify ()
Forget_original_parent () call find_new_reaper ()
then iterates through all the child processes and sets a new parent process for them.
Then call ptrace_exit_finish () ptraced The child process of looking for a father.
Last init wait ()

Once the system successfully finds and sets up a new parent process for the process, there is no danger of hosting the zombie process again. Init The process will routinely invoke Wait () to check its child processes, clearing all of its associated zombie processes.

Linux Chapter III study 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.