X01. OS. 9: process switching, x01. OS. 9 process switching
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.
How do I perform process switching when a process is switched?
Zhang Tingting, you don't have a reward, so I won't give you an answer. My homework is ready.
But let me give you some tips: But I want to name my answer as the best answer, do you know?
On the surface, the process switching function is very simple. At a certain time point, a running process is interrupted, the operating system specifies another process as the running state, and gives control to the process. However, this may cause several problems. First, what events trigger process switching? Another problem is that you must recognize the difference between mode switching and process switching. Finally, to implement process switching, what does the operating system have to do with the various data structures it controls?
When to switch Processes
Process switching can occur at any time when the operating system obtains control from the currently running process.
First, consider system interruption. In fact, most operating systems distinguish between two types of system interruptions. One is called interruption, and the other is called a trap. The former is related to some types of external events unrelated to the currently running process, such as completing an I/O operation; the latter is related to errors or abnormal conditions generated by the currently running process, such as illegal file access. For normal interruptions, the control is first transferred to the interrupt processor, which performs some basic auxiliary work and then to the operating system routines related to the specific types of interruptions that have occurred. See the following example:
Clock interruption: the operating system determines whether the execution time of the currently running process has exceeded the maximum allowed time period (time slice, that is, the maximum time period before the process is interrupted ), if the number exceeds, the process must switch to the ready state and call another process.
I/O interruption: the operating system determines whether I/O activities have occurred. If an I/O activity is an event that one or more processes are waiting, the operating system converts all the corresponding blocked State processes to the ready state (blocking/suspended state processes to the ready/suspended state ), the operating system must decide whether to continue executing the currently running process or preemptible the ready process with a high priority.
Memory failure: When the processor accesses a virtual memory address and the address unit is not in the memory, the operating system must transfer the referenced memory block (page or segment) from the external memory to the memory. After an I/O request is sent to the memory block, the operating system may execute a process switchover to resume the execution of another process. A process with memory failure is set to a blocking state, when the desired block is transferred to the memory, the process is set to the ready state.
For traps, the operating system determines whether the error or exception conditions are fatal. If yes, the currently running process is switched to the exit state, and process switching occurs. If not, the operating system action depends on the type of error and the operating system design, the action can be to attempt to recover or notify the user. The operating system may perform a process switchover or continue to execute the currently running process.
Finally, the operating system may be activated by a system call from a program being executed. For example, if a user process is running and a Command requesting I/O operations is being executed, such as opening a file, this call is transferred to a routine that is part of the operating system code for execution. Generally, system calls may cause user processes to be blocked.
What is the difference between mode switching and process switching?
Process switching means that a running process is interrupted, the operating system specifies another process as the running state, and gives control to the process. Process switching can occur at any time when the operating system obtains control from the currently running process. Due to switching between processes in different States, various resources need to be re-allocated, the operating system needs to do more work.
Mode switching refers to switching between user and connotation. Because their resources are shared, they are highly efficient and do not change the status of running processes.