Unix - Process Scheduling and Process States in UNIX

Process scheduling is one of the most important functions of the UNIX operating system. It determines how the CPU is allocated among multiple processes running on the system. Since several programs may be executing simultaneously, the operating system must decide which process gets access to the processor and for how long. Efficient scheduling ensures that system resources are utilized effectively while maintaining responsiveness and fairness.

What is a Process?

A process is an instance of a program that is currently being executed. When a user runs a command or launches an application, the operating system creates a process for it.

A process contains:

  • Program code

  • Process ID (PID)

  • Memory allocation

  • Open files

  • CPU registers

  • Process state information

  • Scheduling information

Each process is uniquely identified by a Process ID (PID).

For example, when a user executes the ls command, UNIX creates a process to perform the directory listing. Once the command finishes, the process terminates.


Understanding Process Scheduling

The CPU can execute only one process at a time on a single-core processor. However, many processes may be ready to run simultaneously.

Process scheduling is the mechanism through which UNIX decides:

  • Which process should run next

  • How long a process can use the CPU

  • When a process should be suspended

  • When a waiting process should be resumed

The scheduler is a component of the UNIX kernel responsible for making these decisions.


Objectives of Process Scheduling

The primary goals of process scheduling include:

1. Maximum CPU Utilization

The CPU should remain busy as much as possible instead of staying idle.

2. Fairness

Every process should receive a fair share of CPU time.

3. Fast Response Time

Interactive applications should respond quickly to user requests.

4. High Throughput

The system should complete as many processes as possible within a given time.

5. Reduced Waiting Time

Processes should spend less time waiting in queues.

6. Efficient Resource Usage

Memory, CPU, and I/O devices should be used effectively.


Process Life Cycle in UNIX

A process moves through several states during its execution.

The collection of these states is known as the process life cycle.

1. New State

When a process is created, it enters the New state.

Characteristics:

  • Process structure is initialized.

  • Resources are allocated.

  • Process receives a unique PID.

Example:

When a user enters a command in the shell, a new process is created.


2. Ready State

In this state, the process is ready to execute but is waiting for CPU allocation.

Characteristics:

  • All necessary resources are available.

  • Waiting in the ready queue.

  • Execution has not yet begun.

Several processes may remain in the ready state simultaneously.


3. Running State

The process enters the running state when the scheduler assigns the CPU to it.

Characteristics:

  • Instructions are actively executed.

  • CPU is allocated.

  • Process performs computations or system operations.

Only one process can be in the running state on a single-core processor at any given time.


4. Waiting or Blocked State

A process enters the waiting state when it requires an external event before continuing.

Common reasons include:

  • Waiting for keyboard input

  • Waiting for disk access

  • Waiting for network communication

  • Waiting for another process

Example:

A process requests data from a hard disk. Until the data arrives, the process remains blocked.


5. Terminated State

A process reaches the terminated state after completing execution.

Characteristics:

  • Resources are released.

  • Process exits the system.

  • PID becomes available for reuse later.

Example:

After displaying output, a command such as date exits and enters the terminated state.


Process State Transitions

Processes continuously move between states.

New → Ready

The operating system creates the process and places it in the ready queue.

Ready → Running

The scheduler selects the process for execution.

Running → Waiting

The process requests I/O or waits for an event.

Waiting → Ready

The required event completes, and the process becomes ready again.

Running → Ready

The scheduler temporarily removes the process from the CPU to allow another process to execute.

Running → Terminated

The process completes its work and exits.


Process Scheduling Queues

UNIX uses different queues to manage processes.

Ready Queue

Contains processes waiting for CPU time.

Characteristics:

  • Managed by the scheduler.

  • Processes compete for CPU allocation.


Waiting Queue

Contains blocked processes waiting for events.

Examples:

  • Disk operations

  • Network communication

  • User input

Once the event occurs, the process returns to the ready queue.


Context Switching

Context switching occurs when the CPU changes from executing one process to another.

During a context switch, the operating system:

  1. Saves the current process state.

  2. Stores CPU register values.

  3. Loads the next process state.

  4. Restores register values.

  5. Resumes execution.

Example:

If Process A is running and Process B is selected, the operating system saves Process A's information before loading Process B.

Although context switching enables multitasking, excessive switching can reduce system performance because CPU time is spent managing processes rather than executing them.


UNIX Scheduling Priorities

UNIX assigns priorities to processes.

Higher-priority processes generally receive CPU access before lower-priority processes.

Factors affecting priority include:

  • Process type

  • CPU usage

  • User-defined priority

  • System requirements

Interactive processes usually receive better priority than background tasks to ensure quick user response.


Foreground and Background Processes

Foreground Processes

These processes interact directly with users.

Examples:

  • Text editors

  • Terminal commands

  • Interactive applications

Characteristics:

  • Receive keyboard input.

  • Display output directly.


Background Processes

These processes run without direct user interaction.

Examples:

  • Backup programs

  • System monitoring services

  • Log management tasks

Characteristics:

  • Continue running independently.

  • Do not require continuous user input.


Time Sharing in UNIX

UNIX is a time-sharing operating system.

Time sharing allows multiple users and processes to share the CPU.

The CPU is divided into small time intervals called time slices.

Each process receives a time slice.

If the process does not finish during its allocated slice:

  • The scheduler interrupts it.

  • Saves its context.

  • Gives CPU access to another process.

This creates the illusion that multiple programs are running simultaneously.


Process Control Commands in UNIX

Several commands help monitor and manage processes.

ps

Displays information about active processes.

Example:

ps

top

Shows real-time process activity and CPU usage.

Example:

top

kill

Terminates a process using its PID.

Example:

kill 1234

nice

Starts a process with a specified priority.

Example:

nice -n 10 program_name

renice

Changes the priority of a running process.

Example:

renice 5 1234

Importance of Process Scheduling

Process scheduling is essential because it:

  • Enables multitasking

  • Improves CPU utilization

  • Supports multiple users

  • Enhances system responsiveness

  • Ensures fair resource allocation

  • Increases overall system performance

  • Prevents CPU starvation of processes

Without process scheduling, only one process could execute at a time, significantly reducing the efficiency and usability of the operating system.

Conclusion

Process scheduling is a fundamental component of the UNIX operating system that manages how processes access the CPU. By organizing processes into various states such as New, Ready, Running, Waiting, and Terminated, UNIX ensures efficient multitasking and resource utilization. The scheduler, along with mechanisms such as context switching, priorities, and time sharing, enables multiple processes and users to share system resources effectively. Understanding process scheduling and process states is crucial for system administrators, developers, and anyone working with UNIX systems, as it forms the foundation of process management and overall system performance.