A process is an instance of program execution. This means, for example, that if you open up two browser windows then you have two processes, even though they are running the same program.
The life-cycle of a process can be described by a state diagram which has states representing the execution status of the process at various times and transitions that represent changes in execution status.
The operating system maintains management information about a process in a process control block (PCB). Modern operating systems allow a process to be divided into multiple threads of execution, which share all process management information except for information directly related to execution. This information is held in a thread control block (TCB).
Threads in a process can execute different parts of the program code at the same time. They can also execute the same parts of the code at the same time, but with different execution state:
The state diagram for a process captures its life-cycle. The states represent the execution status of the process; the transitions represent changes of execution state.
Each active process has its own execution status, so there is a state diagram for each process. There are relationships between the states of various processes that are maintained by the operating system.
The state of a process represent its execution status.
A process in the ready state has all of the resources that it needs for further execution except for a processor. It is normally held in a ready queue until a processor becomes available.
A process in the running state has all of the resources that it needs for further execution, including a processor.
A process that needs some resource other than a processor for further execution is in a blocked state. It is usually placed in a queue waiting for the needed resource.
The transitions of a process represent changes of its execution state. The transitions can be described in terms of their causes and the resulting actions taken by the operating system.
The creation transition is caused by a system call instruction for loading a program.
A process control block is created for the program. It is initialized so that the process starts with cleared registers and PC set to the program's start (main) address. Usually the operating system sets up three open files: standard input, standard output, and standard error.
A process is dispatched when a processor is free to execute the process and the operating system has scheduled the process to run next. Scheduling involves selecting one of the ready processes to run next. The choice is often based on which ready process has gone the longest time since it last had a running execution status, but the choice may also involve prioritization of processes.
Saved information about the process's register and PC contents is loaded into the processor. The PC contents are typically loaded by executing a jump instruction which, in effect, resumes execution of process code from where it left off.
A timeout is triggered by an external interrupt from a timer device.
Information about the process's register and PC contents is saved into the PCB for the process. The process then goes into the ready state, where it enters a queue with other ready processes. The operating system will the schedule one of the ready processes and dispatch it.
A blocking transition is caused by the process making an operating system request (syscall) that must be satisfied before it can continue executing. The most common type of request is a request for input.
The operating system will initiate an action to satisfy the request. For example, for file input from a disk, the operating system will send a signal to the disk initiating the fetch of a block from the disk. The process is put into a blocked state, where it cannot execute until its request is satisfied.
The unblocking transition is triggered by satisfaction of the request that lead to blocking. For example, if a process requested file input from a disk, the satisfaction will occur several milliseconds later when the disk sends an external interrupt indicating that it is ready to transfer the requested block.
After the operating system has handled the request satisfaction it puts the process into the ready state, entering it into the ready queue. In the file read example, handling the request means storing the block contents in a file structure for the process.
The termination transition may be triggered by an exit syscall from the process (normal termination) or by a processor exception (abnormal termination).
The operating system frees up any resources used by the process. If the termination is abnormal an error message is displayed.
An operating system maintains a structure called a process control block (PCB) for each of its active processes. The PCB for a process contains or refers to the following kinds of information.
Resource management information includes all information about resources other than than the processor that a process is using. This includes page tables and open files and I/O streams. In addition, most operating systems support command-line arguments and environment variables for programs. The resource information for such operating systems contains references to structures containing the command-line arguments and environment variables.
Administrative information includes a process identifier, resource usage information (such as execution time), process ownership, and any kind of administrative data needed by system administrators for running their system securely.
The execution snapshot captures any information about a process that is required to resume its operation from the time when the snapshot was taken. The contents are somewhat dependent on the processor, but always include register and PC contents.
Modern operating systems allow a process to be divided into multiple threads of execution. The threads within a process share all process management information except for information directly related to execution. This information is moved out of the PCB into thread control block (TCBs).
Threads in a process can execute different parts of the program code at the same time. They can also execute the same parts of the code at the same time, but with different execution state:
This is accomplished by moving execution state out of the PCB into thread control blocks (TCBs). Each thread has its own TCB.
Processes start out with a single main thread. The main thread can create new threads using a thread fork system call. The new threads can also use this system call to create more threads. Consequently, a thread not only belongs to a process; it also has a parent thread - the thread that created it.
Different operating systems handle threads in different ways. The description given here should be regarded as only an approximation. It is based on the Mach operating system kernel.
A thread control block (TCB) contains a thread identifier, a reference to the PCB for the process to which it belongs, a reference to the TCB for the thread that created it, and an execution snapshot.
A thread has indirect access to non-execution related resources through the PCB of the process to which it belongs. Thus threads within a process share memory, open files, and I/O streams.
A thread fork operation behaves like a function call. It creates a new child thread whose execution snapshot is identical to the parent thread's snapshot except for the register that contains the function return value. For the child thread the return value is 0; for the parent the return value is the child thread's thread identifier.
Normally, the return value is used in the condition of an if-else statement. If the return value is 0 then child code is executed; otherwise, parent code is executed.
At the system call level, a thread fork operation involves
The operating system then has the option of
FIXME - add text. FIXME - add menu item to ROOT section.