previous contents up next

Unix for Advanced Users

10. Process Management under Unix

10.3. How the Kernel Manages Processes in Unix

10.3.1. Address Space: For each new process created, the kernel sets up an address space in memory. This address space consists of the following logical segments: Each process has two stacks: a user stack and a kernel stack. These stacks are used when the process executes in the user or kernel mode (described below).

10.3.2. Mode Switching: At least two different modes of operation are used by the Unix kernel - a more privileged kernel mode, and a less privileged user mode. This is done to protect some parts of the address space from user mode access.

User Mode: Processes, created directly by the users, whose instructions are currently executing in the CPU are considered to be operating in the user-mode. Processes running in the user mode do not have access to code and data for other users or to other areas of address space protected by the kernel from user mode access.

Kernel Mode: Processes carrying out kernel instructions are said to be running in the kernel-mode. A user process can be in the kernel-mode while making a system call, while generating an exception/fault, or in case on an interrupt. Essentially, a mode switch occurs and control is transferred to the kernel when a user program makes a system call. The kernel then executes the instructions on the user's behalf.

While in the kernel-mode, a process has full privileges and may access the code and data of any process (in other words, the kernel can see the entire address space of any process).

10.3.3. The Context of a Process and Context Switching: The context of a process is essentially a snapshot of its current runtime environment, including its address space, stack space, etc. At any given time, a process can be in user-mode, kernel-mode, sleeping, waiting on I/O, and so on. The process scheduling subsystem within the kernel uses a time slice of typically 20ms to rotate among currently running processes. Each process is given its share of the CPU for 20ms, then left to sleep until its turn again at the CPU. This process of moving processes in and out of the CPU is called context switching. The kernel makes the operating system appear to be multi-tasking (i.e. running processes concurrently) via the use of efficient context-switching.

At each context switch, the context of the process to be swapped out of the CPU is saved to RAM. It is restored when the process is scheduled its share of the CPU again. All this happens very fast, in microseconds.

To be more precise, context switching may occur for a user process when

Context switching for a user process may occur also between threads of the same process.

Extensive context switching is an indication of a CPU bottleneck.

previous contents up next