• Operating System Video Tutorials

Operating System - Operations on Processes



What is a Process in OS?

A process is a program in execution that undergoes a number of states in its lifetime. In each of these states, a process undergoes certain operations that enable the process to execute into completion.

States of a Process

The different states of a process are as follows −

  • New − Process is created.
  • Ready − Process is ready with all its resource allocation and waiting for CPU allocation.
  • Running − Instructions in the process are being executed in CPU.
  • Waiting − Process is in waiting for completion of an input output or any other event.
  • Terminated − Process finishes execution and exits the system.

The process states are depicted in the following diagram −

States of a Process

Different Process Operations in Operating System

The operations on a process occur when the process is in a particular state or when the process makes a transition from one state to another. There are mainly four operations on a process −

  • Process Creation
  • Process Dispatch
  • Process Pre-emption
  • Process Blocking
  • Process Termination

Process Creation

The first operation that a process undergoes when it enters a system is process creation. It involves formation of a process and is associated with New state of the process. Process creation occurs due to any of the following events −

  • System initialization − When the computer is started, a number of system processes and background processes are created.
  • User request − A user may start executing a program leading to creation of a process.
  • Child process system call − A running process may create a child process by process creation system call.
  • Batch system − The batch system may initiate batch jobs

A process may be created by another process using fork(). The creating process is called the parent process and the created process is the child process. A child process can have only one parent but a parent process may have many children. Both the parent and child processes have the same memory image, open files, and environment strings. However, they have distinct address spaces.

A diagram that demonstrates process creation using fork() is as follows −

Process Creation

Process Dispatch

When a process in the ready state is selected for execution by the scheduler, process dispatch operation occurs. Process dispatch is initiated according to the scheduling algorithm used. It may happen when the CPU becomes idle and the ready queue has processes, time quantum allotted for an on-going process expires, a high priority process enters the system or a hardware interrupt occurs. When a new process is assigned to the CPU, its status is loaded from its Process Control Block (PCB).

Process Preemption

Process pre-emption is an operation by which the execution of an on-going process is suspended and some other process is selected by the CPU for execution. Process pre-emption may occur when time quantum of on-going process expires, a high priority process enters the ready queue or a hardware interrupt occurs.

The preemption of executing processes in the CPU causes a phenomenon called context switch. Here, the context or state of the out-going process is stored in its PCB so that it can be reloaded when required and execution can be resumed from the same point as earlier.

The following diagram demonstrates process dispatch and process pre-emption −

Process Preemption

Process Blocking

The on-going process is blocked and put in the Waiting state and swapped out of the CPU, if it needs some event to occur in order to proceed with execution. This event may be an input/output system call since the I/O events are executed in the main memory and don't require the processor.

Here, the operating system blocks the process, when the process itself demands so. After the event is complete, the process again goes to the ready state.

A diagram that demonstrates process blocking is as follows −

Process Blocking

Process Termination

Process Termination is the operation of ending a process and releasing all the resources that have been held by the process. The process ceases to exist after its termination.

Similar to process creation, there may be multiple reasons for process termination, as follows −

  • The process has completed the execution of its last instruction and so it is terminated by the operating system.
  • A child process can be terminated by its parent process if its task is no longer relevant. The child process sends its status information to the parent process before it terminates. Also, when a parent process is terminated, its child processes are terminated as well as the child processes cannot run if the parent processes are terminated.
  • The process can be terminated by the operating system if there are service errors.
  • A hardware malfunction may also cause process termination.

In most cases, process termination occurs when a process executes to completion. The steps involved are as follows −

  • The parent process issues a SIGTERM message to the child process as a signal to initiate termination.
  • On receiving this signal, the child process performs clean-up procedures like releasing allocated resources, closing shared files, giving up access to shared variables and tables etc.
  • The child process then exits and the control returns to either the parent process or the operating system.
Advertisements