8.2.Processes
\(8.2.\)Processes
1.Basic Concepts
The classic definition of a process is an instance of a program in execution.
Each program in the system runs in the context of some process. The context consists of the state that the program needs to run correctly.
2.Logical Control Flow
If we were to use a debugger to single-step the execution of our program, we would observe a series of program counter(PC) values that corresponded exclusively to instructions contained in our program's executable object file or in shared objects linked into our program dynamically at run time. This sequence of PC values is known as a logical control flow, or simply logical flow.
- Processes take turns using the processor. Each process executes a portion of its flow and then is preempted(temporarily suspended) while other processes take their turns.
3.User and Kernel Modes
In order for the operating system kernel to provide an airtight process abstraction, the processor must provide a mechanism that restricts the instructions that an application can execute, as well as the portions of the address space that it can access.
Processors typically provide this capability with a mode bit in some control register that characterizes the privileges that the process currently enjoys.
A process running in kernel mode can execute any instruction in the instruction set and access any memory location in the system.
A process running in user mode is not allowed to execute privileged instructions that do things such as halt the processor, change the mode bit, or initiate an I/O operation. Nor is it allowed to directly reference code or data in the kernel area of the address space.
Any such attempt results in a fatal protection fault.
To execute these instructions, user programs must instead access kernel code and data indirectly via the system call interface.
4.Context Switches
The operating system kernel implements multitasking using a higher-level form of exceptional control flow known as a context switch.
The kernel maintains a context for each process. The context is the state that the kernel needs to restart a preempted process.
At certain points during the execution of a process, the kernel can decide to preempt the current process and restart a previously preempted process. And we say that the kernel has scheduled that process.
The context switch contains the following steps:
Saves the context of the current process.
Restores the saved context of some previously preempted process.
Passes control to this newly restored process.
If the system call blocks because it is waiting for some event to occur, then the kernel can put the current process to sleep and switch to another process.
A context switch can also occur as a result of an interrupt. For example, all systems have some mechanism for generating periodic timer interrupts. Each time a timer interrupt occurs, the kernel can decide that the current process has run long enough and switch to a new process.
The disk will take a relatively long time to fetch the data, so instead of waiting and doing nothing, the kernel performs a context switch from process A to B.
Process B then runs for a while in user mode until the disk sends an interrupt to signal that data have been transferred from disk to memory.