20,135,327 meeting--Reading notes three

Source: Internet
Author: User

Chapter III Process Management 3.1 process
    • A process is a program that is in the execution period (the target code is stored on a storage medium).
    • An execution thread, called thread, is an object that is active in a process. Each Chan Cheng has a separate program counter, process technology, and a set of process registers. The kernel dispatches an object that is a thread, not a process.
    • In modern operating systems, processes provide two virtual mechanisms: virtual processors and virtual memory.
    • The program itself is not a process, and the process is the program of execution and the generic name of the associated resources.
    • Typically, a new process is created to execute a new, different program immediately, and then call exec. This set of functions allows you to create a new address space and load the new program into it.
    • Finally, the program exits execution through the Exi system call.
3.2 Process descriptor and task structure
    • 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 structure of type task_struct, called the process descriptor, which is defined in the

3.2.1 Assigning process descriptors
    • Linux allocates task_struct structures through the slab allocator to achieve object reuse and cache coloring.

3.2.2 Process Descriptor Storage
    • The kernel identifies each process by a unique process identity value (process identification value) or PID. A PID is a number, expressed as a pid_t implicit type, which is actually an int type. In order to be compatible with older versions of UNIX and Linux, the maximum value of the PID is set to 32768 (the maximum value of short int shorter) by default, although this value can also be increased to up to 4 million (this is limited by the PID maximum defined in the <linux/reads.h>) )。 The kernel stores the PID of each process in their respective process descriptors.
    • In the kernel, access tasks usually need to get pointers to their task_struct.
3.2.3 Process Status

The state field in the process descriptor describes the current status of the process (see Figure 3-3). Each process in the system is bound to be in an implied type. The physical representation of an exponential data type is unknown or irrelevant, one of the five process states. The value of this field must also be the following five status flags-:

    • The task_running (run) One by one process is executable: it is either executing or waiting to be executed in the run queue.
    • Task_interruptible (interruptible) One by one process is sleepy (that is, it is blocked), waiting for certain conditions to be reached. Once these conditions are reached, the kernel will set the process state to run. Processes in this state are also woken up early and ready to run because of a signal being received.
    • Task_uninterruptffile (non-disruptive) one by one this state is the same as the interruptible state, except that the received signal will not be awakened or ready to be put into operation.
    • _task_traced-a process that is tracked by another process
    • _ Task_stopped (stop) Each process stops executing: The process is not operational and cannot be put into operation. Usually this state occurs when a signal such as SIGSTOP, SIGTSTP, Sigttin, Sigttou is received. In addition, any signal received during debugging will cause the process to enter this state.

3.2.4 Setting the current process state

The kernel often needs to adjust the state of a process. It is best to use the Set_task_state (task, state) function:
Set_task_state (task, state); /* Set the status of task tasks to state */
The function sets the specified process to the specified state.

3.2.5 Process Context
    • Executable code is an important part of the process. The code is executed from an executable file loaded into the address space of the process.
    • System calls and exception handlers are explicitly defined interfaces to the kernel. The process must pass through these interfaces in order for all accesses to the kernel to execute one by one to the kernel.

3.2.6 Process Family Tree
    • There is an obvious inheritance between the processes of Unix systems, as well as in Linux systems. All processes are descendants of the init process with PID 1.
    • Each process in the system must have a parent process and, accordingly, each process can have 0 or more child processes. All processes that have the same parent process are called brothers. The relationships between processes are stored in the process descriptor. Each task_struct contains a pointer to its parent process tast_struct, called parent, and a list of child processes called children.

3.3 Process Creation

The process creation of Unix is very special. Many other operating systems provide a mechanism for generating the (spawn) process, first creating a process in the new address space, reading the executable file, and finally starting execution. Unix uses a different implementation, which decomposes the above steps into two separate functions: Forko and EXEC ():

    • First, fork () creates a child process by copying the current process. The difference between a child process and a parent process is only the PID (unique per process), PPID (the process number of the parent process, the child process sets it to the PID of the copied process), and some resources and statistics.
    • The exec () function is responsible for reading the executable file and loading it into the address space to start running. The effect of combining these two functions is similar to the effect of a single function used by other systems.

3.3.1 Write-time copy
    • The traditional fork () system call directly copies all the resources to the newly created process. The fork () of Linux is implemented using the write-time copy (Copy-on-write) page. Write-time copying is a technique that can postpone or even eliminate copy data. The kernel does not replicate the entire process address space at this time, but instead allows the parent and child processes to share the same copy.
    • Data is copied only when it needs to be written, so that each process has its own copy.
    • The actual cost of fork () is to copy the page table of the parent process to give the process a unique process descriptor.
3.3.2 Fork ()
    • Linux implements Fork () through clone () system calls.
    • Do_fork completed most of the work in the creation, which is defined in the Kemevfork.c file. The function calls the Copy_ process () function and then lets the process start running. The work done by the copy_process () function is interesting:
      • Call the Dup_task_ struct () to create a kernel branch, read_info structure, and task_struct for the new process, which are the same values as the current process. At this point, the child process and the parent process descriptor are exactly the same.
      • Check to make sure that the number of processes currently owned by the current user does not exceed the limit of the resources allocated by the paint color after the child process is newly created.
      • The child process begins to differentiate itself from the parent process. Many members within the process descriptor are cleared 0 or set to the initial value.
      • The status of the child process is set to task_unjnterruptible to ensure that it will not run.
      • Copy _process () calls Copy_flags () to update the TASK_STRUCT group of AGS members.
      • Call Alloc _pid () to assign a valid PID to the new process.
      • Copy_process () copies or shares open files, file system information, signal processing functions, process address spaces, and namespaces, depending on the parameter flags passed to clone ().
      • Finally, Copy_process () prides itself on working and returns a pointer to the child process.
    • Back to the Do_fork () function, if the copy_process () function returns successfully, the newly created child process is awakened and put into operation.
3.3.3 Vfork ()
    • The vfork () system call and the fork () function are the same except for page table entries that do not copy the parent process.
    • The implementation of the Vfork () system call is done by passing a special flag to the clone () system call.
      • When Copy_process () is called, the Vfor_done member of Task_struct is set to NULL.
      • When Do_fork () is executed, if a special flag is given, vfork_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 Vfork_done pointer.
      • When Mm_release () is called, the function is used for the process to exit the memory address space and to check if Vfork_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.

3.4 Implementation of threads in Linux

Threading mechanism is an abstract concept commonly used in modern programming technology. This mechanism provides a set of threads that run in shared memory address space within the same program. These threads can also share open files and other resources.

Linux implements threading in a very unique mechanism. From the kernel point of view, it does not have the concept of threading. Linux implements all threads as processes.

3.4.1 Creating Threads
    • Creating a thread is similar to creating a normal process, except that you need to pass some parameter flags when invoking clone () to indicate which resources need to be shared:
      Clone (CLONE_VM | clone_fs | clone_flies | clone_sighand,0);

3.4.2 Kernel Programming
    • The kernel often needs to perform some operations in the background. This task can be done through kernel threads (kernel thread)-a standard process that runs independently of the kernel space. The difference between a kernel thread and a normal process is that the kernel thread does not have a separate address space (the MM pointer that actually points to the address space is set to NULL).
    • Linux does take some tasks to the kernel threads, such as flush and KSOFIRQD, which are obvious examples.
    • After the kernel Chan Cheng is started, it runs until the call Do_exit () exits, or the other part of the kernel calls Kthread_ stop () exits, and the arguments passed to Kthread_stop () are returned by the Kthread_create () function task_struct Address of the structure:
      int kthread_stop (struct task_struct *k)

3.5 Process End
  • regardless of how the process ends, the task is mostly done by do_exit () (defined in kemel/exit.c), which does the following tedious work: 
    • set the flag member in Tast_struct to Pf_exiti NG.
    • calls Del_timer_ sync () to remove any kernel timers. Based on the returned results, it ensures that no timers are queued and that a timer handler is running. &NBSP
    • If the BSD process accounting function is turned on, Do_exit () calls Acct_update_ integrals () to output the accounting information.   
    • then calls the EXIT_MM () function to release the mm_struct that the process consumes, and if no other process uses them (that is, the address space is not shared), release them completely.
    • Next calls the Sem_ exit () function. If the process waits for an IPC signal, it leaves the queue.  
    • calls Exit_files () and Exit_fs () to decrement the reference count of the file descriptor, file system data, respectively. If the value of one of the reference counts is reduced to zero, then it means that no process is using the corresponding resource, which can be released at this time.  
    • Then set the task exit code stored in the Exit_code member of Task_struct to the exit code provided by exit (), or to complete any other exit actions specified by the kernel mechanism. The exit code is stored here for the parent process to retrieve at any time.  
    • call Exit_notify () to send a signal to the parent process, re-find the adoptive father for the child process, the adoptive father is the other Chan Cheng in the Chan Cheng group or the Init process, and the process state (stored in task_struct structure exit_state) set to E Xit_zombie.
    •  do_exit () calls schedule () to switch to the new process.  do_exit () never returns.  
    • at this point, all resources associated with the process are freed (assuming that the process is the only consumer of those resources). The process is not operational (there is actually no address space for it to run) and is in the Exit_zombie exit state.

3.5.1 Deleting a process descriptor
    • After Do_exit () is called, the system retains its process descriptor even though the thread is dead and can no longer run.
    • Wait () This family function is implemented through a unique (but very complex) system call to WAIT4 ().
    • When the process descriptor needs to be released eventually, Release_task () is called to do the following:
      • It calls _exit_signal (), which calls _unhash _process (), which in turn calls Detach_pid () to remove the process from the Pidhash and also removes the process from the task list.
      • _exit_signal () Releases all remaining resources used by the current zombie process and makes final statistics and records.
      • If this process is the last process of the thread group and the lead process is dead, then Release_task () notifies the zombie leader process of the parent process.
      • The Release_task () call put_task_ struct () frees the process kernel and also read_info the page that the structure occupies, and releases the slab cache that the tast_struct occupies.
      • At this point, the process descriptor and all the process resources are freed.

3.5.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 at the time of exiting, consuming memory in vain. The previous section has hinted that, for this problem, the workaround is to have the child process find a thread within the current thread group as the father, and if not, let init their parent process.

Once the system successfully finds and sets a new parent process for the process, there is no danger of a zombie-resident process. The init process routinely calls wait () to check its child processes, clearing all of its associated zombie processes.

3.6 Summary

In this chapter, we examine the process of core concepts in the operating system. We also discussed the general nature of the process, why it is so important, and the relationship between processes and threads. Then, we discuss how Linux stores and represents processes (with task_ struct and thread_info), how to create a process (via fork (), essentially clone ()), and how to load the new execution image into the address space (through the EXECO system call family), as What represents the hierarchical relationship of a process, how the parent process collects information about its descendants (through the wait () system call family), and how the process eventually dies (forced or voluntary call to exit ()). Process is a very basic, very critical abstraction, located at the heart of every modern operating system, and the ultimate reason we have an operating system (to run programs).

20,135,327 meeting--Reading notes three

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.