X01. OS. 9: process switching

Source: Internet
Author: User

After entering the kernel, do not do nothing. Create three processes to print A, B, and C respectively. Although it is just a simple print, it is the foundation of all extensions and cannot be ignored.

Process switching involves a series of registers that need protection. Therefore, the processstack structure is available. The Code is as follows:

typedef struct {    u32        gs;    u32        fs;    u32        es;    u32        ds;    u32        edi;    u32        esi;    u32        ebp;    u32        KernelEsp;    u32        ebx;    u32        edx;    u32        ecx;    u32        eax;    u32        RetAddr;    u32        eip;    u32        cs;    u32        eflags;    u32        esp;    u32        ss;} ProcessStack;

Note that there is a kernelesp. The role of the stack pointer is to prevent the process from being switched.

Process switching, of course, is inseparable from interruptions. Interestingly, the job status stack TSS involving interrupted calls is exactly the same as processstack.

Another key to process switching is that you cannot use a simple ret to return. The Save code in kernel. S is as follows:

save:    pushad    push        ds    push        es    push        fs    push        gs    mov        dx, ss    mov        ds, dx    mov        es, dx    mov        esi, esp    inc            dword [g_IntReenter]    cmp        dword [g_IntReenter], 0    jne            .1    mov        esp, StackTop    push        Restart    jmp            [esi + P_RetAddr - P_StackBase].1:    push        reenter    jmp            [esi + P_RetAddr - P_StackBase]Restart:    mov        esp, [g_pProcReady]    lldt            [esp + P_LdtSel]    lea            esi, [esp + P_StackTop]    mov        dword [g_Tss + TSS_ESP0], esireenter:    dec            dword [g_IntReenter]    pop        gs    pop        fs    pop        es    pop        ds    popad    add            esp, 4    iretd

TheJMP [ESI + p_retaddr-p_stackbase]That is, jump to the previously saved return address. The returned address is set by kernelmain in Main. C. That is,Pproc-> regs. ESP = (u32) ptaskstack;This kind of multi-weapon operation requires careful understanding before you can understand it.

If you can understand this key point, process switching is not difficult. The so-called process, but on the basis of processstack, add some process IDs, names, and priorities.

Go to the project directory, make, and then go to bochs to see the following interface:

The priority is set to, which is basically the same as that displayed. Complete code, goX01.lab. Download. For more information about virtual machines and development tools, seeX01. OS. 7.

You need to explain the problem. When you make the changes again,/mnt/temp will be busy. You can run the sudo umount/mnt/temp command to uninstall it.

X01. OS. 9: process switching

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.