Processes
Prev: Exceptions
TOC: Exceptional Control Flow
Next: System Call Error Handling
In Kernel Mode, the CPU can only execute OS's code, the program just calls onto OS's functions to execute them.
- Exceptions are the basic building blocks that allow the OS kernel to provide the notion of a process.
- When we run a program we are presented with the illusion that our program has exclusive access to both our processor and memory. The processor appears to execute the instruction in our program one after another without interruption. Our code and data appear to be the only objects in system's memory, these illusion are provided to us by notion of a process.
- Classically, a process is an instance of a program in execution, where each program in the system runs in the context of some process.
- The context consist of state a program needs to run correctly, like it's general purpose register,s PC, env vars and set of open file descriptors.
- Shell creates a new process and runs the Executable Object Files in the context of the new process, application programs can also create a process on their own.
- A process provides the following abstractions to our application:
- An independent logical control flow that provides the illusion that our program has exclusive rights over a processor.
- A private address space providing illusion that our program has exclusive use of the memory system
Logical Control Flow

- Processor provides each program with the illusion that it has exclusive use of processor even through many other programs are running concurrently on the system.
- If we were to single step into our program's executable using some debugger then we would see series of program counter (PC) values that correspond to instruction contained in our program, this series of PC is logical control flow.
- In above diagram, 3 processes are running concurrently, with one physical logical flow partitioned into 3 logical control flow, represented by each line.
Concurrent Flows
- Logical control flow take many different from in computer systems, exceptional handlers, processes, signal hander,s threads etc
- A logical control flow whose execution overlaps in time with other control flows is called a concurrent flow.
Private Address Space

- Process provides each program with illusion that it has exclusive use of system's address space, for n-bit address, the address space is size 2n possible addresses.
- Bytes of memory associated with particular address space cannot GENERALLY be read/write by other process.
- Above diagram shows the general organization of private space. You can read a bit more in each section here: Loading Executable Object Files.
User and Kernel Modes
- For airtight process abstraction, processor must provide mechanism to restrict instruction that an application can execute as well as the portion of address space it can access.
- This capability is usually provided with mode bit in some control register that defines the privilege that the process currently enjoys, when the mode bit is set the process is running in kernel mode(supervisor mode), else it's in user mode.
- A process running in kernel mode is allowed to run any instruction in the instruction set and access any memory location in the system.
- A process running application code is initially in user mode, The only way for the process change from user to kernel mode is through interrupts, faults and traps.
- When some exception occur due to above reasons, the control is passed to the exception handler, the processor changes the mode from user to kernel and the handler runs in kernel mode. When it then returns to the application code, the processor changes the ode from kernel to user.
- Linux provides a mechanism called /proc file system that allows user mode processes to access the contents of kernel data structure, the /proc filesytem exports contents of many kernel data structures as hierarchy of text files that can be read by the user program.
- Newer Linux kernel has /sys filesystem that exports additional low level information
Context Switches
- It's a "higher level" form of exception control folw cuz it's software defined, and is built up to low level exception mechanism as described in Exceptions portion.
- The kernel maintains a context for each process, a context is the state that the kernel needs to restart a preempted (stopped) process, consisting of the values of the object such as the general purpose registers, floating point registers,, program counter, user's stack, status registers, kernel stack and various kernel data structures like page table that characterizes the address space, a process table info about current process and file table that has info about files that the process has opened.
- At certain point during a process's execution the kernel can decide to preempt the current process and restart a previously preempted process, this decision is known as scheduling and is handled by code in the kernel called the scheduler.
- After the kernel has scheduled the new process to run it preempts the current process and transfers control to the new process using a mechanism called context switch that saves the context of the current process, restores context of some previously preempted process and passes control to this newly restored process.
- Context switch can happend when:
- The process does some syscall, but it's blocked cuz it's waiting for some event, kernel just puts current process into sleep and switches to another process.
- If sleep is called, this is explicit request to preempt the current process and move to something else
- If some internal timer goes off, and the context switch happens as a result of this interrupt
Prev: Exceptions
TOC: Exceptional Control Flow
Next: System Call Error Handling