Linux Kernel Learning Summary

Source: Internet
Author: User

Enriched Linux learning has been completed, through this study, Linux has a preliminary understanding and understanding of the formation of a general framework, the work of the Linux process also has a basic concept. The contents of the course are summarized as follows:

1. How the computer works

From the hardware point of view:

Based on the von Neumann architecture,

1) The CPU reads an instruction from the memory code segment by the bus according to the value of CS:IP, and the read instruction enters the instruction buffer;

2) then ip=ip+ the length of the instruction taken, thus pointing to the next instruction;

3) Execute the instruction and go to 1),

Repeat the process.

From the programmer's point of view:?

CPU abstraction for A for loop

for (;;) {Next instruction}?

2. How the operating system works

Operating system on the basis of computer work process scheduling and interruption, so that multiple processes can quickly switch execution, reasonable allocation of CPU resources.

3. About Start_kernel

Startkernel in the Rest_init generation No. 0 process, kernel_init generation 1th process, Kernelthread generation 2nd process, 1th process is the ancestor of all user-state processes, the 2nd process is the ancestor of all kernel threads. All subsequent processes are generated through process 1th and process # 2nd. Dispatched to the idle process when no process is required for the system to execute.

4. System calls

The system call frees the user from the low-level hardware programming, greatly improves the system security, the use and the program has the portability.

?? When the user-state process invokes a system call, the CPU switches to the kernel and starts executing a kernel function. In Linux, you perform a system call by executing an int $0x80, where the assembly instruction produces a programming exception with a vector of 128. The kernel implements a number of different system calls, and the process must indicate which system call is required, which requires passing a parameter called the system call number. System_call is the entry point for all system calls in Linux, with at least one parameter per system call, that is, the system call number passed by EAX an application calls the fork () encapsulation routine, then the value of the EAX register is set to 2 before the int $0x80 is executed (that is, __nr_ Fork). The settings for this register are libc in the encapsulation routines in the library, so the user is generally not concerned about the system call number entering Sys_call and immediately pressing the value of EAX into the kernel stack.

5. Create a new process in the kernel execution process

Linux creates a new process by replicating the parent process, which gives us an idea of the framework that this process provides:

Copy a pcb--task_struct

Err = Arch_dup_task_struct (tsk, orig);

To assign a new kernel stack to the new process

TI = Alloc_thread_info_node (tsk, node);

Tsk->stack = Ti;

Setup_thread_stack (tsk, orig); This is just a copy of the Thread_info, not the kernel stack.

To modify the copied process data, such as PID, process chain list and so on to change it, see copy_process inside.

See Fork () from the user-state code; The function returns two times, that is, each time it is returned in a parent-child process, the parent process returns from the system call is easier to understand, the child process is returned from the system call, and the kernel stack data state and task_ of the child process are involved. The consistency of the SP and IP in the thread record in the struct is set in the Copy_thread in Copy_process.

*childregs = *current_pt_regs (); Copy the kernel stack

Childregs->ax = 0; Why the fork of the subprocess returns 0, here is the reason!

P->THREAD.SP = (unsigned long) childregs; Top of the kernel stack when dispatched to a child process

P->thread.ip = (unsigned long) ret_from_fork; The address of the first instruction when dispatched to a child process

We know that the functions of fork, vfork, clone and so on in Linux system can be used to create a new process, and we can see that the system calls corresponding to the functions of fork, vfork, clone and so on are called do_fork functions. So the specific call procedure is to do_fork the system kernel call, execute to copy_process copy all the information of the parent process to the child process, where the child process is assigned a new stack in Dup_task_struct, Sched_fork is called, and the new process is set to task_running; and copy_thread The register context of the parent process to the child process, and finally setting the address of the ret_from_fork to the value of the EIP register, which causes the child process to execute from the ret_from_fork.

6. The difference between loading execve and fork

Execve and fork are special system calls that fall into the kernel state in return to the user state to continue execution. Fork is special, as the parent process and the normal system call, the child process starts from ret_from_fork to return to the user state. Execve falls into the kernel state and overwrites the current process's executable program with the loaded new executables, and when it returns, it is not the original runnable program, but is the new executor. Because calling exec does not create a new process, it simply replaces the body, data, heap, and stack segments of the current process with a completely new program, so the process ID is not changed before and after

7. Switching of processes

I. Timing of dispatch

1. Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () based on the need_resched tag when returning to the user state;

2. Kernel thread can directly call schedule () for process switching, can also be scheduled during interrupt processing, that is, the kernel thread as a class of special processes can be active scheduling, can also be passive scheduling;

3. The user-state process can not realize the active scheduling, only through the kernel state after a certain point in time to dispatch, that is, in the interrupt processing process scheduling.

Second, when the X user state process through the process scheduling policy switch to the user state process Y,

The most common scenario: The running user-state process x switches to the process of running user-state process y

1. Running user-state process X

2. Interrupt--save Cs:eip/esp/eflags (current) to kernel Stack,then load Cs:eip (entry of a specific ISR) and Ss:esp (point to Kern El Stack).

3.save_all//Save site

4. Schedule () is invoked during interrupt processing or before an interrupt is returned, where SWITCH_TO does a critical process context switch

5. After the label 1 begins to run the user-state process y (here Y has been switched out through the above steps so you can continue from the label 1)

6.restore_all//Recovery site

7.iret-pop cs:eip/ss: esp/eflags from kernel stack

8. Continue to run the user-state process y

Several special cases

1. By interrupting the timing of the processing process, the user-state process and kernel threads switch between each other and the kernel threads switch to each other, and the most common situation is very similar, but the kernel thread is running in the process of interruption without process user state and kernel state conversion;

2. Kernel thread actively calls schedule (), only the process context switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;

3. Create a child process of the system call in the child process execution starting point and return user state, such as fork;

4. After loading a new executable program, return to the condition of the user state, such as EXECVE;

Third, about the process switch

1. In order to control the execution of the process, the kernel must have the ability to suspend a process that is executing on the CPU and resume execution of a previously suspended process, called process switching, task switching, context switching;

2. Suspend the process that is executing on the CPU, which is different from the save scene at the time of interruption, before and after the interruption is in the same process context, only by the user state to the kernel state execution;

3. The process context contains all the information required for process execution

(1) User address space: Including program code, data, user stack, etc.

(2) Control information: Process descriptor, kernel stack, etc.

(3) Hardware context (note that interrupts are also saved by the hardware context, only the method of saving is different)

The 4.schedule () function selects a new process to run and invokes Context_switch for context switching, which calls switch_to for critical context switching

(1) Next = Pick_next_task (RQ, prev);//process scheduling algorithms encapsulate this function inside

(2) Context_switch (RQ, Prev, next);//process Context switch

(3) Switch_to takes advantage of Prev and next two parameters: Prev points to the current process, next points to the scheduled process

Blog Job Directory:

1. Analysis of computer working process by assembly code http://blog.sina.com.cn/s/blog_14375bc180102w2lx.html

2. Simple time slice rotation multi-channel program kernel analysis http://blog.sina.com.cn/s/blog_14375bc180102w2zl.html

3. Trace analysis of the boot process of the Linux kernel http://blog.sina.com.cn/s/blog_14375bc180102w3g4.html

4. Call http://blog.sina.com.cn/s/blog_14375bc180102w3p3.html using the embedded assembly code system in the API and C code

5.system_call Interrupt processing Process Analysis http://blog.sina.com.cn/s/blog_14375bc180102w3z4.html

6. Analyze the process of creating a new process for the Linux kernel http://blog.sina.com.cn/s/blog_14375bc180102w4ai.html

7.Linux how the kernel loads and launches an executable program http://www.cnblogs.com/yuxiaochuan/p/5376507.html

8. Understand the process of process scheduling and process switching during time tracking analysis process http://www.cnblogs.com/yuxiaochuan/p/5402770.html

Summary: What are the biggest gains in learning the Linux kernel analysis course? What is the biggest regret after learning the Linux kernel analysis course?

The biggest gain is figuring out the process scheduling and interrupt mechanism, and feeling the smart design of Linux

The biggest regret is that the course is not much. = =

Shinakawa + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Linux Kernel Learning Summary

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.