First assignment: Deep source Analysis Process Model

Source: Internet
Author: User
Tags prev

1Introduction to Linux operating system

Linux systems typically have 4 main parts: The kernel, the shell, the file system, and the application. The kernel, shell, and file systems together form a basic operating system structure that allows users to run programs, manage files, and use the system.

(1) kernel

The kernel is the core of the operating system and has many of the most basic features, such as virtual memory, multitasking, shared libraries, demand loading, executable programs, and TCP/IP networking capabilities. The modules of the Linux kernel are divided into the following parts: storage management, CPU and process management, file system, device management and driver, network communication, system initialization and system invocation, etc.

(2) Shell

The shell is the user interface of the system and provides an interface for users to interoperate with the kernel. It receives the command entered by the user and sends it to the kernel to execute, which is a command interpreter. In addition, Shell programming languages have many features of common programming languages, and Shell programs written in this programming language have the same effect as other applications.

(3) File system

A file system is an organization method that files reside on storage devices such as disks. Linux systems can support a variety of currently popular file systems such as EXT2, EXT3, FAT, FAT32, VFAT, and ISO9660.

(4) Application

Standard Linux systems generally have a set of assemblies called applications, including text editors, programming languages, Xwindow, office suites, Internet Tools, and databases.

Process Organization for 2.Linux operating systems

(1) What is a process

A process is a program that is in execution and all the resources it contains, including virtual processors, virtual spaces, registers, stacks, global data segments, and so on.

In Linux, each process is assigned a data structure when it is created, called Process Control Block, or PCB. The PCB contains a lot of important information for the system scheduling and the process itself to perform the use. The PCB of all processes is stored in the kernel space. The most important information in the PCB is the process PID, the kernel through this PID to uniquely identify a process. The PID can be reused and the maximum value is 32768. The PID of the Init process is 1, and the other processes are descendants of the INIT process.

In addition to the Process Control block (PCB), each process has a separate kernel stack (8k), a process descriptor structure, which is stored in the kernel space as the control information of the process, while the process's user space primarily stores code and data.

(2) Process creation

 1 #代码示例: 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 6 int main (int ar   GC, char * * argv) {7 int flag = 0;   8 pid_t pid = fork ();  9 if (pId = =-1) {Ten perror ("fork Error");  Exit (Exit_failure);  The else if (pId = = 0) {int mypid = Getpid ();  parentpid int = Getppid ();  printf ("child:selfid=%d parentid=%d \ n", Mypid, Parentpid);  + flag = 123;  printf ("child:flag=%d%p \ n", flag, &flag);  int count = 0;  do {count++;  Sleep (1);  printf ("Child count=%d \ n", count);  if (Count >= 5) {break;  (1);  Exit_success return;  "Else {" parent:selfid=%d mychildpid=%d \ n ", Getpid (), pId);  flag = 456; printf ("parent:flag=%d%p \ n", FLag, &flag);  Even the address is the same, the description is true full copy, but the value is already different.  int count = 0;  do {count++;  Sleep (1);  Notoginseng printf ("Parent count=%d \ n", count);  if (Count >= 2) {;  (1);  (exit_success);  45}

(3) Process revocation

The process exits execution by calling exit (), which ends the process and frees all resources. The parent process can query whether the child process is terminated through WAIT4 (). The process exits after execution in a zombie state until its parent process calls wait () or waitpid (). When the parent process exits, the kernel specifies another process for the thread group or the INIT process as the new parent process for its child processes. The kernel can force a process to terminate when the process receives a signal that cannot be processed or ignored, or when a non-recoverable CPU exception is generated in the kernel state and the kernel is running on behalf of the process.

(4) Process Management

The kernel stores process information in a two-way circular list called the task queue (the kernel space). Each item in the list is of type task_struct, called the process Descriptor structure (procedure descriptor), which contains all the information for a specific process, including the open file, the address space of the process, the pending signal, the status of the process, and so on.

struct Task_struct {volatile long state;pid_t pid;unsigned long timestamp;unsigned long rt_priority;struct mm_struct *mm, *ACTIVE_MM}

For a downward-growing stack, a new struct thread_info is created at the bottom of the stack (on the top of the stack for an upward-growing stack), making it easy to calculate its offsets in assembly code.

#在x86上, the THREAD_INFO structure is defined in file <asm/thread_info.h> as follows:
struct thread_info{ struct task_struct * task struct exec_domain *exec_domain; unsigned long flags; unsigned long status; __u32 CPU; __s32 Preempt_count; mm_segment_t Addr_limit; struct Restart_block restart_block; unsigned long previous_esp; _u8 supervisor_stack[0]; };

The kernel organizes all processes that are in the task_running state into a single, bidirectional circular queue. The dispatch function is executed by scanning the entire operational queue for the process that is most deserving of execution. Avoid scanning all processes and improve scheduling efficiency.

#进程调度使用schedule () function to complete, the following we start from the analysis of the function, the code is as follows: 1 asmlinkage __visible void __sched schedule (void) 2 {3     struct TASK_ struct *tsk = current;4 5     sched_submit_work (tsk); 6     __schedule (); 7}8 Export_symbol (schedule);
#在第4段进程调度中将具体讲述功能实现
Process state transitions for 3.Linux operating systems

There are the following process states:

Transition of process state:

Specific conversion Analysis:

(1) Initial state of the process

The process is created through the system calls of the Fork series (fork, clone, Vfork), and the kernel (or kernel module) can also create kernel processes through the Kernel_thread function. The functions that create child processes essentially do the same thing-copying the calling process to get the child process. (You can use option parameters to determine whether a variety of resources is shared or private.) So now that the calling process is in the task_running state (otherwise, it's not running, how does it make the call?). ), the child process is also in the Task_running state by default. In addition, the system call to clone and kernel function Kernel_thread also accepts the clone_stopped option, which resets the initial state of the child process to task_stopped.

(2) Process state change

After the process has been created, the state may change a series of changes until the process exits. While there are several process states, the process state changes in only two directions-from the task_running state to a non-task_running state, or from a non-task_running state to a task_running state. That is, if a sigkill signal is sent to a task_interruptible state process, the process will first be awakened (into the task_running state) and then exited (into a task_dead state) in response to the sigkill signal. does not exit directly from the task_interruptible state. The process changes from a non-task_running state to a task_running state, and is implemented by a wake-up operation from another process (or possibly an interrupt handler). The process setting that wakes up is task_running the state of the wake process, and then joins its task_struct structure to the executable queue of a CPU. The awakened process will then have the opportunity to be dispatched for execution.

When a process changes from a task_running state to a non-task_running state, there are two ways:

    • To enter a task_stoped state, or Task_dead state, in response to a signal;
    • The execution system call actively enters the task_interruptible state (such as a nanosleep system call), or Task_dead state (such as an exit system call), or the resource that is required to execute the system call is not satisfied and enters the Task_ A interruptible state or task_uninterruptible state (such as a select system call).
Process scheduling for 4.Linux operating systems

Needless to do, we use the schedule () function to complete the process scheduling, and then we take a look at the code of the process scheduling and the implementation process.

1 asmlinkage __visible void __sched schedule (void) 2 {3     struct task_struct *tsk = current;4 5     sched_submit_work (TS k); 6     __schedule (); 7}8 Export_symbol (schedule);

Line 3rd Gets the current process descriptor pointer, which is stored in the local variable tsk. Line 6th calls __schedule () and the Code is as follows (KERNEL/SCHED/CORE.C):

static void __sched __schedule (void)

The 9th line prohibits kernel preemption. Line 10th gets the current CPU number. Line 11th Gets the current CPU's process run queue. The 13th line holds the descriptor pointer of the current process in the Prev variable. The 55th row of the next scheduled process descriptor pointer is placed in the next variable. Line 56th clears the kernel preemption token for the current process. The 60th line determines whether the current process and the next schedule are the same process, and if not, dispatch. Line 65th Toggles the context of the current process and the next process (the context is toggled before scheduling). Here's a look at the function (kernel/sched/core.c):

context_switch (struct RQ *rq, struct task_struct *prev, struct task_struct *next)

Context switches are generally divided into two, one is the hardware context switch (refers to the CPU register, to the current process to save the contents of the register, and then the next program to restore the contents of the register), the other is to switch the process of the address space (white is the program code). The Address space (program code) of the process is mainly stored in the struct mm_struct struct in the process descriptor, so the function is mainly to manipulate the struct body. Line 17th If the next process address space is scheduled to be empty, the next process is a thread, there is no separate address space, and the address space of the owning process is shared, so the 18th row of the address space used by the previous process active_mm the pointer to the domain of the next process. The next process also uses this address space. Line 22nd, if the next process address space is not empty, indicating that the next process has its own address space, then execute the SWITCH_MM Switch Process page table. Line 40th Toggles the hardware context of the process. The Switch_to function code is as follows (Arch/x86/include/asm/switch_to.h):

__visible __notrace_funcgraph struct task_struct * __switch_to (struct task_struct *prev_p, struct task_struct *next_ P)

The function is to do some initialization work for the new process just switched over. For example, the 34th thread local bucket (TLS) used by the process is loaded into the global descriptor table of the local CPU. The 84th line of the return statement is compiled into two assembly instructions, one is to save the return value prev_p to the EAX register, and the other is the RET instruction, which pops the top element of the kernel stack out of the EIP register, starting at the EIP pointer, which is the pointer that was pressed into the last function 17th line. In general, the pressed pointer is the address represented by the label 1 in line 20th of the previous function, and when returned from the __SWITCH_TO function, it will run from the label 1.

It is important to note that for processes that have been transferred, the return from the __switch_to function will start at the label 1, but for a new process that was just created with the fork (), Clone (), and so on, will enter the Ret_from_fork () function, Since the Do_fork () function creates a process, it gives the thread_info.ip of the Ret_from_fork function the address of the process, not the address of the label 1, so it jumps into the ret_from_fork function. We'll see when we analyze the call of the fork system in the rear.

5. Some personal views on the Linux operating system process model

Just as human beings based on theoretical practice of great design wisdom experience crystallization, Linux operating system is system, efficiency, security, and through the commercial companies, large community groups, operating system enthusiasts are improving forward, but if one day Linux operating system closed source, only the domestic open source code, Have not mastered the core technology, how to get stuck neck? We can not have a completely use of the mentality, but also a solid grasp of the basic knowledge, improve self-innovation awareness. For the Linux operating system process model, understand it deeply, you will find that the application of the Linux operating system is more efficient, and through it you can achieve more fun operations.

First assignment: Deep source Analysis Process Model

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.