How the operating system works ———— a streamlined operating system kernel (20135304 Liu Xipeng)

Source: Internet
Author: User

How the operating system works ———— a streamlined operating system kernel

20135304 Liu Xipeng

Original works reproduced please indicate the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Experimental process

Open the shell with the lab virtual machine, load the Linux kernel needed for the lab, and execute the built-in system

CD linuxkernel/linux-3.9. 4  -kernel Arch/x86/boot/bzimage

Always executes mystartkernel, alternating mytimerhandler

View MYMAIN.C Code

voidinit mystartkernel (void) from this start is the entry of the operating system 100,000 times per cycle of printing a Mystartkernel

View MYINTERRUPT.C Code

Each time a clock interrupt is called PRINTK prints a Mytimerhandler, which is some interrupt handling when the interrupt actually occurs.

This simulates the work of the hardware platform, including the initialization of the action is completed

In Https://github.com/mengning/mykernel, we can find the multi-process time slice rotation code needed in the experiment.

Switching in 0, 1, 2, 3 processes can be observed after running

Code Analysis

Open Mypcb.h

Thread is used to save the EIP and ESP, in the PCB, the int PID defines the process id;volatile long state; Char Stack[kernel_stack_size] defines the kernel stack. My_schedule represents the scheduler, Task_entry is the entry

Open MYMAIN.C

Mymain.c tells us how to initialize and describes the initialization of process # No. 0.

This code is designed to create multiple processes

After the creation of the No. 0 process, this is an embedded assembly code, where%0 represents the parameter thread.ip,%1 represents the parameter thread.sp. The 49th line is to put the parameter Thread.sp in the ESP, then push% 1, and because the current stack is empty, ESP=EBP is equivalent to the push EBP, and then the push Thread.ip;ret is equivalent to the pop thread.ip; the last pop Ebp.

After ret, process No. 0 officially started.

The function my_process defines the work of all processes. This function defines a loop in which the IF statement represents a loop of 10 million times before the opportunity to determine if a dispatch is required. This is an active scheduling mechanism.

Open myinterrupt.c

Next = my_current_task->next;//The next process of the current process is assigned to next
Prev = my_current_task;//Current Process
if (next->state = = 0) If the next process state is 0 (executing)

/* Switch to Next process *///(context switch), here is an embedded assembler code,

The IF statement indicates that the task is empty, which is returned when an error occurs. Line 52nd begins the work of My_scheduel, assigning the next process of the current process to next, and the current process as prev.

The 54th line indicates that if the state of the next process is executing, the process is switched using the method represented by the code in the If statement, which is embedded assembler code similar to the MYMAIN.C code.

%0 indicates that prev->thread.sp,%1 indicates that prev->thread.ip,%2 represents next->thread.sp,%3 for Next->thread.ip. The push EBP is the EBP that holds the current process, then the ESP of the current process is saved, the SP of the next process is placed in the ESP, then the eip,$1 represents the position of the following label 1: Then the EIP of the next process is pushed into the stack.

After RET, the next process begins to execute. This is the key code for process switching.

Unlike the previous piece of code, if the next process is a new process, this piece of code in else is used. This process is first set to the runtime state, which is used as the currently executing process. After the code for the Embedded assembly code, a little bit different from the above, but the statements are almost similar, here will not repeat.

Summarize

The first week of study let me learn the basic model of computer work, that is, the memory program computer + function call battle, in this week in-depth learning operating system work principle, first introduced an important model is the interrupt mechanism, I think this is the core of modern computer operating system work, The early computer in the CPU completely processing a program before the next task of processing, which greatly reduces the efficiency of the computer, and modern computer introduced interrupt mechanism, and time-slice rotation model is the basic model of modern computer, This makes the computer through the interrupt mechanism in a number of processes in the clock signal I based on switching, from a macro point of view is a number of programs implemented together, greatly improving the efficiency of the computer.

How the operating system works ———— a streamlined operating system kernel (20135304 Liu Xipeng)

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.