Processor execution Mode + process switching

Source: Internet
Author: User

Most processors support at least two modes of execution. Some instructions can only be executed in privileged mode, including reading or altering instructions for control registers such as the program status Word, raw IO instructions, and memory management-related instructions. In addition, a portion of the memory area can be accessed only under privileges.

A non-privileged state is often called a user state, because the user program is usually executed in that mode, and the privileged state is called the System state, the control state, or the kernel state, which refers to the kernel of the operating system, which is the part of the operating system that contains important system functions.

This creates two questions: how the processor knows what mode it is executing in and how to change the pattern. For the first question, there is a bit in the program status word that represents the execution pattern, which is changed by the requirements of certain events. In a typical case, when a user invokes an operating system service or interrupts the execution of a system routine, the execution mode is set to the kernel state, and when it is returned from the system service to the user process, the execution mode is set to the kernel state, and when the system service returns to the user state, the execution mode is set to the user state. For example, psr,cpl level 0 for IA64 is a privilege level. For Intel Itanium, applications use system calls by placing system invocation identifiers and parameters in a predefined area, and then executing a special instruction to interrupt the execution of the user-state program and give control to the kernel.

Once the operating system decides to create a new process, it can follow these steps:

1) Assign a unique process identifier to the new process. At this point, a new table entry is added to the main process table, and each new table item in the table corresponds to a process.

2) Allocate space to the process. This includes all elements in the process image. Therefore, the operating system must know how much space the private user address space (Programs and data) and user space stacks require. You can use the default values depending on the type of process, or you can set it based on user requests when the job is created. If a process is created by another process, the user process can pass the desired value to the operating system as part of the process creation request. If any existing address space is shared by this new process, the correct connection must be established. Finally, you must allocate space to the process control block.

3) Initialize the process control block. The process identifier section includes the process ID and other related IDs, such as the ID of the parent process, and most of the items in the Processor state information section are typically initialized to 0, except for program counters (which are placed as program entry points) and system stack pointers (to define process stack boundaries) The initialization of the Process Control Information section is based on the standard default values and the properties requested for the process. For example, the process state is initialized to ready/suspended in a typical situation, unless a higher priority is explicitly requested, the default value of the priority is the lowest priority, and unless it is explicitly requested or inherited from the parent process, the process initially does not own any resources (IO devices, files).

4) Set the correct connection. For example, if the operating system saves each dispatch queue as a linked list, the new process must be placed in the Ready or Ready pending list.

5) Create or augment other data structures. For example, the operating system may save an accounting file for each process, which can be used for billing and performance evaluation.

Process switching

From the surface, the function of process switching is very simple. At some point, an ongoing process is interrupted, the operating system specifies that the other process is running, and gives control over to the process. But this can cause a number of problems. First, what events trigger a process switch? Another problem is the need to recognize the difference between pattern switching and process switching. Finally, what does the operating system have to do with the various data structures it controls to enable process switching?

When to switch processes

Process switching can occur at any moment when the operating system gains control from the currently running process. First consider the system outage. In fact, most operating systems differentiate between two types of system interrupts. One is called an interrupt, and the other is called a trap. The former relates to a type of external event that is not related to the currently running process, such as completing an IO operation, which is related to errors or exception conditions that are caused by the currently running process, such as illegal file access. For normal interrupts, the control is first transferred to the interrupt processor, which does some basic auxiliary work, and then goes to the operating system routines associated with the particular type of interrupt that has occurred. See the following examples:

Clock Interrupt: The operating system determines whether the currently running process has exceeded the maximum allowable time period (the time slice, the maximum period of time that the process can execute before it is interrupted), and if it is exceeded, the process must switch to the ready state to invoke another process.

IO Interrupt: The operating system determines if IO activity has occurred. If the IO activity is an event that one or more processes are waiting for, the operating system transitions all the corresponding blocking processes to the ready state, blocking the pending go-to-ready suspend, and the operating system must decide whether to continue the process that is currently running, or to preempt the process with a high-priority ready-state process.

Memory failure: When the processor accesses a virtual address and the address unit is not in memory, the operating system must transfer the memory block (segment or page) containing the reference into memory from external memory. After an IO request to the memory block is emitted, the operating system may perform a process switchover to resume execution of another process, the process in which the memory failure occurs is set to a blocking state, and the process is set to the ready state when the desired block is called into memory.

For traps, the operating system determines whether an error or abnormal condition is fatal. If it is, the currently running process is converted to an exiting state and a process switch occurs, and if not, the action of the operating system depends on the type of error and the design of the operating system, and its behavior can be to attempt to recover or notify the user that the operating system may make a process switch or continue executing the currently running process.

Finally, the operating system may be activated by a system call from the executing program. For example, a user process is running and executing an instruction that requests an IO operation, such as opening a file, which causes the transfer to be performed on a routine that is part of the operating system code. In general, using system calls can cause the user process to be placed in a blocking state.

Mode switching

If there is an unhandled interrupt, the processor needs to do the following:

1) Place the program counter at the start address of the interrupt handler.

2) switch from the user state to the kernel state, so that the interrupt processing code can contain privileged instructions.

The processor now continues to refer to the stage and takes the first instruction of the interrupt handler, who will serve the interrupt. At this point, the interrupted process context is saved in the PCB of the interrupted program.

The question is, what does a saved context include? The answer is that it must include all the information that is required to interrupt the processing of potentially changed information and to recover the interrupted program. Therefore, you must save the part of the process control block called processor state information, which includes program counters, other processor registers, and stack information.

Is there any other work to do? It depends on what happens next. An interrupt handler is usually a small program that performs some basic tasks related to interrupts. For example, it resets flags or indicators that indicate an interruption. An answer may be sent to the entity that generated the interrupt, such as an IO module. It also does some basic ancillary work related to the resulting interruption of the event results. For example, if an interrupt is associated with an IO event, the interrupt handler checks for an error condition, and if an error occurs, the interrupt handler sends a signal to the process that originally requested the IO operation. If the clock is interrupted, the handler hands over the control to the scheduler, and when allocated to the current running time slice is exhausted, the scheduler transfers control to another process.

How does the other information in the process control block work? If you switch to another application after the interruption, you need to do some work. However, in most operating systems, the occurrence of interrupts is not accompanied by process switching. It is possible that the currently running process continues to execute after the interrupt processing is executed. In this case, all you need to do is save the processor state information when the interrupt occurs. To reply to this message when control returns to the program. In typical cases, the save and restore functionality is implemented by hardware.

Changes in Process state

Obviously, mode switching is different from process switching. A pattern switch can not change the state of a process that is in the running state, in which case it requires little overhead to save the context and later restore the context environment. However, if the currently running process is switched to another state (ready, blocked, and so on), the operating system must make a substantial change to its environment, and the complete process switching steps are as follows:

1) Save the processor context, including program counters and other registers.

2) Update the process control block of the currently running process, including changing the state of the process to another State (ready, blocked, ready/suspended, or exited). You must also update other related domains, including reasons for leaving the run State and accounting information.

3) Move process Control blocks of processes to the appropriate queue (ready, block at event I, ready/pending).

4) Select another process to execute.

5) Update the process control block for the selected process, including turning the status of the process into a running state.

6) Update the data structure of the memory management, depending on how the address translation is managed.

7) The recovery processor in the selected process last switch out of the running state of the context, by loading the program counter and other registers before the value of the implementation.

Process switching involves state changes and therefore requires more work than module switching.

Processor execution Mode + 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.