Operating system interrupts, traps, exceptions

trap

The computer has two operating modes: user mode and kernel mode. The operating system runs in the kernel state, in which the operating system has full access to all hardware and can make the machine run any instructions; on the contrary, the user program runs in the user state, in which the software can only use a few instructions, they do not have direct access to the hardware. This presents a problem. What if the software needs to access the hardware or call a function in the kernel. This is the role of the trap. Trap instructions can make the execution flow from user mode into the kernel (this is why it is called a trap, not a hunting trap) and transfer control to the operating system, so that the user program can call kernel functions and use the hardware to obtain the operating system. services, such as playing a movie with video software, the video software will issue a trap to use the monitor and sound card to access the hardware.

The operating system has many system call interfaces for applications to call. The occurrence time of the trap is fixed. For example, when the video software is used for the first time, the software will send the trap command to the operating system when the video is loaded. When the video is played for the second time, the software will still send the trap command at the same time. This is one of the obvious differences from interrupts. Interrupts are described below.

interrupt

Unlike traps, interrupts are caused by external events and their timing is unpredictable. External events mainly refer to clock interrupts, hardware interrupts, etc. Since the CPU can only run one instruction at a time, only one program can run at a time, but we feel that many programs can be run at the same time in our computer. This is due to the rapid switching of the CPU between multiple processes. pseudo-parallel. If a program runs long enough to use up the time slice allocated to it, the CPU decides to switch to another process to run, and a clock interrupt is generated to switch to the next process to run.

As the name implies, hardware interrupts are interrupts caused by hardware. For example, a program requires the user to input a piece of data, but the user has never inputted it. The operating system decides whether to wait for user input or to run another process. Generally, it is to run another process. , if the user's input comes, the keyboard driver will generate an interrupt to notify the operating system, and the operating system saves the state of the running program, thereby switching to the original process to process the incoming data.

Therefore, the interrupt occurs randomly and the main function is to complete the switching between processes, thereby supporting parallelism between the CPU and the device.
Another important difference between interrupts and exceptions is that the CPU will mask the interrupt while processing the interrupt, and will not accept new interrupts until the end of the interrupt processing. The occurrence of the trap does not mask the interrupt and can accept new interrupts.

abnormal

An exception is an abnormal behavior during program execution. Such as division by zero exception, buffer overflow exception, etc. Different operating systems define different types and numbers of exceptions and each exception has a unique exception number. Exceptions will disrupt the normal execution flow of the program, so exceptions are problems that occur when the CPU executes instructions, such as division by zero. The division by zero exception occurs. The occurrence of exceptions indicates that the program design is unreasonable, so try to avoid the occurrence of exceptions when programming.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382160&siteId=291194637