Operating Systems and Kernels
System Concepts: Processes
Processes are a very important concept in Operating Systems and lie at the heart of most of its tasks. A process, which is a program in execution, goes through the cycle of starting, performing its task and then exiting. Whilst a process is performing its task it usually goes through another cycle of being constantly blocked and then allowed to run again. This is known as the process scheduling model and is shown below:
Why is this necessary? Because the CPU has to deal with multiple processes at the same time and can't afford to let a single process run for as long as it wants. It therefore allows each process to run for a specified time. This is know as time multiplexing and is a fair way of ensuring that each process runs for the same amount of time, under normal circumstances.
Being in the ready state indicates to the scheduler that the process would like to run. When the scheduler makes the decision to allow the process to use the CPU, it moves to the running state. After its time is up, it moves back to the ready state. When a process tries to access a shared section or is waiting for input from an I/O device before it can continue (for example, reading file names from disk), it moves to the blocked state. Once the input becomes available to it, it can then continue.
When are processes created?
1. On system startup. Daemons, or background processes, are automatically started by the Operating System when the system is initialized and they work in the background either listening for events to occur or maintaining some part of the system.
2. On system calls. A process that is already running may wish to give birth to additional processes to help it in a particular task. The latter processes are known as child processes and the former is the parent. It is common to have such a hierarchy of processes.
3. On user requests. Double clicking on a program icon or executing it via the command line automatically creates a process for that program. It is more than likely that it will then generate child processes to perform separate tasks.
When are processes terminated?
1. When they're done. After it completes its tasks, a process voluntarily exits.
2. When an error occurs. If an error occurs that the process cannot ignore or avoid, it voluntarily exits.
3. When a fatal error occurs. A program directly referencing memory (in a language such as C, which allows this) may be referring to data that is nonexistent. In such a case, the system has no option but to kill the process to prevent it from corrupting other processes' data.
4. When killed by another process.
Handling descheduled processes
Scheduling is discussed in more detail here, but it's probably a good idea to briefly mention how the scheduler handles processes that are descheduled. A descheduled process that moves from a running to a ready or blocked state (context switching) may well run again after a period of time. So how does the scheduler ensure that the process can pick up from where it left off?
The Operating System at all times has a table called the process table, in which each process is stored along with its state (ready, running, blocked), program counter, stack pointer, memory allocation and scheduling information. When a process does a context switch, all its information must be updated in the process table.
When another process is allowed to run or when an interrupt occurs (more about interrupts later), the program counter, stack pointer and memory map are all replaced with new values. Just before this is done, the previous values of the previously running process are saved to the process table. When the process is next allowed to run, it reads back its information from the process table and carries on from where it left off.