Processes -- Operating Systems


1. Processes

Definition
Process States

Pasted image 20240804121027.png

Processes in an operating system can exist in several states during their execution. These states are essential for managing process scheduling and ensuring that resources are utilized efficiently. The primary process states are:

  1. New: The process is being created.
  2. Running: Instructions are being executed by the CPU.
  3. Waiting (or Blocked): The process is waiting for some event to occur (such as I/O completion or signal reception).
  4. Ready: The process is ready to be assigned to a processor.
  5. Terminated: The process has finished execution.

State Transitions

Pasted image 20240804122322.png

Processes transition between these states based on various events, such as system calls, hardware interrupts, or the completion of operations. The transitions can be visualized in a state diagram:

  1. New -> Ready: The process has been created and is ready to run, but is not currently running.
  2. Ready -> Running: The process is assigned to the CPU and starts execution.
  3. Running -> Waiting: The process requires an event (like I/O) to proceed and thus enters the waiting state.
  4. Waiting -> Ready: The event that the process was waiting for has occurred, and the process is ready to run again.
  5. Running -> Ready: The process is preempted by the scheduler, perhaps due to the time slice being over or a higher-priority process requiring CPU time.
  6. Running -> Terminated: The process has completed its execution and is terminated.
  7. New -> Terminated: The process creation fails (e.g., due to insufficient memory or other resources).

Detailed Explanation of Each State Transition

1. New -> Ready

When a process is created, it starts in the "new" state. This happens when a user starts an application or a system call initiates a process. The operating system allocates memory and initializes the process control block (PCB) with necessary information. Once the setup is complete, the process moves to the "ready" state, indicating that it is prepared to execute as soon as the CPU is available.

2. Ready -> Running

The transition from "ready" to "running" occurs when the scheduler selects a process from the ready queue to use the CPU. This selection is based on the scheduling algorithm employed by the operating system. Once chosen, the process's state changes to "running," and it starts executing instructions.

3. Running -> Waiting

While executing, a process may encounter situations where it cannot proceed without external events. For example, if it requires input/output (I/O) operations, it must wait for these to complete. During this waiting period, the process state changes from "running" to "waiting." The CPU is then free to execute other processes, improving overall system efficiency.

4. Waiting -> Ready

After the required event occurs (such as I/O completion or a signal reception), the waiting process becomes eligible for execution again. The operating system moves the process from the "waiting" state back to the "ready" state, where it awaits CPU allocation.

5. Running -> Ready

A process in the "running" state can be preempted by the operating system if:

6. Running -> Terminated

When a process completes its execution, it transitions from "running" to "terminated." This means the process has finished its intended task, and the operating system de-allocates resources (like memory and open files) used by the process. The process control block (PCB) is then removed from the system.

7. New -> Terminated

In some cases, a process might fail to move from the "new" to the "ready" state due to errors such as insufficient memory, invalid initialisation parameters, or security restrictions. If such a failure occurs, the process transitions directly to the "terminated" state, and the system releases any partially allocated resources.


Process Relationship

Parent and Child Processes

Pasted image 20240804125404.png

When a process creates a new process, it becomes the parent of the new process, and the new process is referred to as its child. The parent and child processes can share various resources or have separate resources depending on the operating system and its implementation of process creation.

Process Creation

During the process creation, the following resources can be shared:

Process Termination

A process can terminate in several ways:


Process Control Block (PCB)

Pasted image 20240804124621.png

The Process Control Block (PCB) is a crucial data structure that stores information about a process. It includes:

The PCB enables the operating system to manage and control processes efficiently, ensuring that each process receives its fair share of resources and that the system operates smoothly.


Context Switching

Context switching is the process by which the operating system saves the state of a currently running process and loads the state of another process. This is essential for multitasking, allowing the CPU to switch between processes and manage multiple tasks effectively.

Steps in Context Switching
  1. Save the State of the Current Process: The current process's CPU registers, program counter, and other essential information are saved in its PCB.
  2. Load the State of the Next Process: The next process's PCB is loaded, and its CPU registers, program counter, and other information are restored.
  3. Switch to the Next Process: The CPU begins executing the instructions of the next process.
Overhead of Context Switching

Context switching incurs overhead because the CPU must save and load process states, which consumes time and resources. Reducing context switching overhead is essential for efficient multitasking and system performance.


Threads: A Deep Dive into Concurrency

Definition: A thread is an independent execution path within a process. Think of it as a lightweight unit of work that can execute concurrently with other threads belonging to the same process. Each thread has its own stack, register set, and program counter, allowing them to operate independently yet share the same resources like memory space and files.

Pasted image 20240924210635.png
A process creating multiple threads that can work concurrently.

Various States: Threads exist in various states throughout their lifecycle:

Benefits of Threads:

Types of Threads:

Multithreading Concept:

Multithreading empowers a single process to execute multiple threads concurrently. This enables parallel processing and significant performance gains for tasks that can be divided into independent units of work.


Process Scheduling: Orchestrating CPU Time Allocation

Foundation & Objectives:

Process scheduling is the crucial task of deciding which process gets access to the CPU at any given time. Its primary objectives are:

Types of Schedulers:

Scheduling Criteria: Various metrics are used to evaluate scheduling algorithms:

Scheduling Algorithms:

In detail:

1. First-Come, First-Served (FCFS)

Pasted image 20240912131642.png

Arrival Times:   P1 = 0, P2 = 2, P3 = 4
Burst Times:     P1 = 5, P2 = 3, P3 = 1
Execution Order: P1 -> P2 -> P3


2. Shortest Job First (SJF)

Pasted image 20240912132223.png

Arrival Times:   P1 = 0, P2 = 2, P3 = 4
Burst Times:     P1 = 7, P2 = 4, P3 = 1
Execution Order: P3 -> P2 -> P1


Shortest Remaining Time First (SRTF) - Preemptive SJF

How It Works:

Example:

Let's say we have a few processes with remaining burst times as follows:

Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 1
Arrival Times:   P1 = 0, P2 = 1, P3 = 2 
Burst Times:     P1 = 8, P2 = 4, P3 = 1 
Execution Order: P1 -> P2 (preempts P1) -> P3(preempts P2) -> P2 -> P1

Pasted image 20241219112921.png

A chart showing how the processes are scheduled according to SRTF

Pros:

Cons:


3. Round Robin (RR)

Pasted image 20240912134242.png

Consider these processes:

Process Burst Time Time Quantum
P1 24 ms 4 ms
P2 3 ms 4 ms
P3 3 ms 4 ms
P4 10 ms 4 ms
with a given time quantum of 4ms.

Pasted image 20241219113854.png

How the processes would be scheduled and executed according to RR


4. Priority Scheduling

Consider three processes with their burst times and priorities as follows:

The goal here is to execute the processes in order of their priorities.

Process Burst TImes Priorities
P1 7 3
P2 4 1
P3 1 2
Execution Order: P2 -> P3 -> P1

Multiprocessor Scheduling

Multiprocessor scheduling refers to the task of allocating and managing processes across multiple processors within a computer system.

The goal is to maximize system performance and resource utilization by efficiently distributing workload and minimizing idle time on any processor.

Real-Time Scheduling

Real-time scheduling is a type of scheduling algorithm used in systems where tasks have strict timing constraints.

These systems require that tasks be completed within a predetermined deadline, otherwise, the entire system's functionality or safety could be compromised. Examples include:

Real-time scheduling algorithms prioritize tasks based on their deadlines and strive to meet all deadlines consistently.

Real-Time Scheduling Example

EDF (Earliest Deadline First) Scheduling

EDF is a real-time scheduling algorithm that prioritizes tasks based on their earliest deadlines.

The core principle of EDF is to always schedule the task with the closest deadline first, regardless of its priority level or execution time. This approach ensures that no task misses its deadline and helps maintain system responsiveness.

EDF effectively utilizes the available processing power by allocating it to tasks nearing their deadlines, minimizing the risk of a cascading effect where missed deadlines lead to further failures.

Let's consider three processes with their respective arrival times, execution times, and deadlines:

Example:

Consider three tasks:

Task Execution Time Deadline
T1 2ms 6ms
T2 1ms 4ms
T3 2ms 8ms
Execution Order:
  1. At time 0ms: All tasks are ready.
    • T2 has the earliest deadline (4 ms), so it runs first.
  2. At time 1ms: T2 completes, T1 has the next earliest deadline (6 ms), so it runs.
  3. At time 3ms: T1 completes, T3 runs next since it has a deadline of 8 ms.

A real-time scheduling algorithm, such as Earliest Deadline First (EDF), would prioritize these processes based on their deadlines.

Pasted image 20241118092626.png

Result: All three processes are completed before their respective deadlines.

Advantages of EDF:

  1. Optimal Scheduling: EDF ensures that if the task set is schedulable, it will meet all deadlines. No other scheduling algorithm can guarantee meeting deadlines for a wider range of task sets.
  2. Flexibility: It works well in both periodic and aperiodic task systems.
  3. Dynamic Adjustments: Because priorities are based on deadlines, EDF can handle variations in task arrival times and execution times more gracefully.

Disadvantages of EDF:

  1. Overhead in Preemptive Systems: Preemptions can introduce significant overhead, especially if new tasks with earlier deadlines frequently arrive, forcing context switches.
  2. Unpredictable Behavior under Overload: When the system is overloaded (i.e., when deadlines cannot be met due to too many tasks), EDF may lead to a situation where many deadlines are missed. It does not prioritize which deadlines to miss first.
  3. Complex Implementation: Implementing EDF requires keeping track of deadlines and dynamically adjusting task priorities, which can introduce complexity and overhead, particularly in multiprocessor environments.

Rate monotonic Scheduling (RM)

Rate monotonic scheduling (RMS) is a preemptive scheduling algorithm used in real-time systems. It prioritizes tasks based on their rates, or how frequently they require execution.

The Core Principle:

Imagine you have several machines with different production cycles: one makes widgets every minute, another builds gears every 5 minutes, and a third assembles entire products every hour. If all machines need to run concurrently, RMS prioritizes the machine producing widgets because it demands attention most frequently.

In technical terms, RMS assigns priority to tasks based on the reciprocal of their periods. A task with a shorter period (executing more frequently) receives a higher priority. For example:

Order of execution: Task A --> Task B


Comparison summary between RM and EDF

Comparison Summary:

Feature EDF RMS
Priority Dynamic (based on deadlines) Fixed (based on periods)
Optimality Optimal on uniprocessors Optimal for static priority scheduling with periodic tasks
Performance under overload Unpredictable behavior Predictable performance but not optimal
Overhead Higher due to dynamic changes Lower due to fixed priorities
Suitability Periodic and aperiodic tasks Periodic tasks