Textbook: Section 2.2
Process states
Synchronization
typesTechniques
examples
definitions
disabling interrupts
Moreover, Posix needs to run all the processes simultaneously - not so easy if, like the Intel CPU, you can only do one thing at a time. (Actually, that's a bit odd: Recent Intel CPUs provide better performance by doing several things at a time, but they go to great lengths to provide the illusion that it can only handle one instruction at a time. And then the OS goes to great lengths to provide the illusion that it can run many processes on the same CPU. There's some performance being lost there.)
To provide the illusion that many processes are running simultaneously, the OS implements multiprogramming. The idea behind multiprogramming is that the OS will rapidly switch processes on and off the CPU, so fast that the user can't tell.
To support this, the operating system maintains a list of processes currently vying for the CPU. At any time, only one process can be running. But many may want it.
Each process has one of three states:
Conceptually, this is what happens. But there is a complication: The CPU can only run one process at a time. It can't both run a process and run the OS. So how does control transfer from a running process into the OS?
The answer is that the OS will set up the clock to trigger a hardware interrupt after a fixed amount of time. When that interrupt occurs, then control transfers into the operating system, and it can take it from there. (Of course, if the running process triggers a software interrupt first, then the OS gets control earlier.)
So what exactly happens when an interrupt occurs? The following shows what happens in Minix, and it's similar on other operating systems.
1. The CPU places the current program counter on the stack. 2. The CPU looks up where the first instruction of the interrupt handler is and jumps there. 3. The assembly code saves the registers in the table. 4. The assembly code sets up the stack for the interrupt handling. 5. The assembly code transfers into a C function. 6. The C function does any immediate handling needed (usually buffering the input). 7. OS moves the waiting process from blocked state to ready state. 8. OS decides which process to run next. 9. The C function returns to the assembly code. 10. The assembly code restores the registers for the new process. 11. The assembly code jumps to the stored as its program counter.