20179223 "Linux kernel Fundamentals and Analysis" Nineth Week study notes

Source: Internet
Author: User
Tags prev git clone

Timing analysis of video learning process scheduling and process scheduling

Different types of processes have different scheduling requirements

First Category:
--i/o-bound:1. Frequent i/o;2. It often takes a lot of time to wait for I/O operations to complete
--cpu-bound:1. Computationally intensive; 2. Requires a lot of CPU time to calculate

Second Category:
--Batch process: 1. Do not need to interact with the user, usually run in the background; 2. Do not need to respond quickly; 3. Typical batch process: compiler, scientific calculation
--Real-time process: 1. There is real-time demand, should not be blocked by the low priority process; 2. Response time is short and stable; 3. Typical real-time processes: Video/audio, mechanical control, etc.
--Interactive process: 1. Need to interact with the user frequently, so it takes a lot of time to wait for user input; 2. The response time is fast and the average delay is less than 50~150ms;3. Typical interactive programs: shell, text editing programs, graphics applications, etc.

Linux supports both common ticks and real-time processes.

Scheduling in Linux is a mixture of various scheduling strategies and scheduling algorithms.

What is a scheduling policy?
is a set of rules that determine when and how to choose a new process to run.

Linux scheduling is based on ticks and priorities
-With changes in the version, the time-sharing technology is changing

Linux processes are queued according to priority
--Calculates the priority of a process based on a particular algorithm, with a value representing
-This value indicates how the process is properly allocated to the CPU

The priority of processes in Linux is dynamic
--The scheduler periodically adjusts the priority of the process according to the behavior of the process: 1. Processes that are not allocated to the CPU for a long time, usually ↑;2. A process that has been running on the CPU for a long time, usually ↓.

Schedule function
Schedule function Implementation Scheduling
Purpose: To find a process in the run queue and assign the CPU to it
Call method: 1. Call schedule directly (); 2. Loosely called, according to need_resched tag

Timing of Schedule Scheduling
1. Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () from the need_resched tag when returning to the user state
2. Kernel threads can directly call schedule () for process switching, or they can be dispatched during interrupt processing, which means that kernel threads can be actively dispatched as a special class of processes or can be passively dispatched
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

Process Context switch related code analysis

Process switching
-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

--suspends the process that is executing on the CPU, which is different from the Save field at the time of the outage, before and after the interrupt is in the same process context, but only by the user state to the kernel state execution

--The process context contains all the information required by the 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 in the hardware context except that the method is saved differently)

The--schedule () function selects a new process to run and invokes Context_switch for context switching, a macro called SWITCH_TO for critical context switching

    1. Next=pick_next_task (Rq,prev);//The Process scheduling algorithm encapsulates this function's internal
    2. Context_switch (Rq,prev,next);//process Context switch
    3. Switch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process
Analysis of general execution process of Linux system

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 Ker Nel stack)
3.save_all//Preservation Site
4. Schedule () is called before the interrupt process or 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 in the process of Linux system execution

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, very similar to the most common situation, but the kernel thread is running in the process of interruption without process user state and kernel state conversion
2. Kernel thread active call schedule (), only the process context of the switch, no interruption context of the switch, and the most general situation is slightly abbreviated
3. Create a child process's system call execution starting point in the child process and return the user state, such as fork
4. After loading an executable program, return to the user status, such as Execve

The core and the dancer

The kernel is a collection of various interrupt processing processes and kernel threads

Linux Operating System Architecture overview

Any computer system contains a basic set of programs, called the operating system.
--Kernel (process management, process scheduling, interprocess communication mechanism, memory management, interrupt exception handling, file system, I/O system, network part)
--Other programs (such as libraries, shell programs, system programs, etc.)

The purpose of the operating system
--Interact with hardware to manage all hardware resources
--Provides a good execution environment with user programs (applications)

$ cd LinuxKernel$ rm -rf menu$ git clone http://github.com/mengning/menu.git$ cd menu

Modify the TEST.c file in the menu directory, add the system call code you wrote in the previous lesson, and make two menu commands, just like the previous experiment
Then make Rootfs to compile

Use the Qumu command to restart the kernel and use the-s and-s parameters to "freeze" the system execution with the following command:

$ qemu -kernel ..//linux-3.18.6/arch/x86/boot/bzImage -initrd ..//rootfs.img -s -S

In order to use GDB to debug, you need to split a window horizontally, enter the following command:

 gdb file ..//linux-3.18.6/vmlinux target remote:1234

Set breakpoints using the GDB Trace schedule () function

Continue to execute,

Find the next running process

The schedule () function internally determines whether a context-switching statement is required. Context_switch () process context switch.

Book 15th, chapter 16 learning process address space

The process address space refers to the memory of processes in user space, which is the memory that each user-space process sees. Linux uses virtual memory technology to share memory between processes in a virtual way, and each process seems to have access to all the physical memory of the entire system.
The process address space is made up of process-addressable virtual memory. The legitimate address space that can be accessed is called the memory area, and the process can only access memory addresses within the valid memory area. A process that accesses a memory area that is not in a valid range or accesses a valid address in an incorrect manner is terminated by memory and returns a "segment error" message.

The memory area contains various memory objects:

Code snippet, you can perform a memory map of the file code.
Data segment, you can perform a memory mapping of the initialized global variables of the file.
A memory map containing 0 pages of uninitialized global variables that are BSS segments.
A 0-page memory map for the process user space stack.
Code snippets, data segments, and BSS segments for each shared library, such as C library or dynamic linker, are also loaded into the process's address space.
Any memory-mapped file.
Any shared memory segment.
Any anonymous memory mappings, such as malloc allocated memory.

Page cache and page write-back

There are three kinds of strategies for write caching:

1. Does not cache, the cache does not cache any write operations, skips the cache to write to disk, and invalidates the data in the cache.
2. Write through the cache, automatically update the memory cache, and also update the disk files.
3. "Write back", write directly to the cache, the backend store does not immediately update directly, the page cache is written to the page marked "dirty" by the writeback process to write dirty pages back to disk. (Flusher core thread is responsible)

20179223 "Linux kernel Fundamentals and Analysis" Nineth Week study notes

Related Article

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.