0% found this document useful (0 votes)
11 views66 pages

Process Management and Scheduling Overview

The document provides an overview of process management in operating systems, detailing the definition of processes, their states, and the role of the Process Control Block (PCB). It also covers process scheduling, inter-process communication (IPC), and threading models, highlighting the advantages and disadvantages of user-level and kernel-level threads. Additionally, it lists common process commands in Linux/Unix for monitoring and managing processes.

Uploaded by

devshah271206
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views66 pages

Process Management and Scheduling Overview

The document provides an overview of process management in operating systems, detailing the definition of processes, their states, and the role of the Process Control Block (PCB). It also covers process scheduling, inter-process communication (IPC), and threading models, highlighting the advantages and disadvantages of user-level and kernel-level threads. Additionally, it lists common process commands in Linux/Unix for monitoring and managing processes.

Uploaded by

devshah271206
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit - 2

Process Management
Process States & Process Control
Block (PCB)
What is a Process
• A process = a program in execution

• Active entity (has CPU state, memory,


resources)

• Managed by the Operating System


Key Differences
Aspect Program Process

Definition Set of instructions (static) Program in execution (dynamic)

Nature Passive entity Active entity

Stored in Disk (secondary storage) RAM (main memory)

Resource Doesn’t need resources Requires CPU, memory, I/O


Allocation
Multiplicity One program → many processes Each process = one instance
Process States
• New – Program is loaded
• Ready – Waiting for CPU
• Running – Instructions executing
• Waiting – Waiting for I/O or event
• Terminated – Execution finished

• New → Ready → Running → Waiting → Ready →


Running → Terminated
Detailed Process States
• New (Created):
• - The process is being created.
• - Program is loaded into memory but hasn’t started
execution yet.

• Ready:
• - Process is ready to run, waiting in ready queue.
• - Has all required resources except CPU.
• - Example: Students raising hands, waiting for teacher
(CPU).
• Running:
• - Process is currently executing on CPU.
• - Only one process per CPU core can run at a time.

• Waiting (Blocked):
• - Process paused until event occurs (like I/O
completion).
• - Example: Waiting for keyboard input, file read, or
network.

• Terminated (Exit):
• - Process finished execution or was killed.
• - All resources released back to OS.
Process Control Block (PCB)
Process Control Block (PCB)
• A Process Control Block (PCB) is a data structure maintained by the
operating system for every process.

• It stores all the information about a process so the OS can manage


and resume it properly.

• Without PCB, the OS would not know:


• Which process is running
• What resources it owns
• Where to continue execution after being interrupted

• Think of PCB as a 'report card' or 'identity card' of a process.


Process Control Block (PCB)
• OS data structure for each process

• Holds:
• Process ID (PID)
• Process State
• Program Counter
• CPU Registers
• Memory Info
• I/O Status
Process ID (PID)
• • Unique number assigned to each process.
• • Used by the operating system to identify and
manage processes.
• • Example: Each running program has a
different PID.
Process State
• Describes the current condition of the
process.
• States include:
• - New: Process is being created.
• - Ready: Waiting for CPU.
• - Running: Currently executing.
• - Waiting/Blocked: Waiting for I/O or event.
• - Terminated: Finished execution.
Program Counter (PC)
• Holds the address of the next instruction to
be executed.
• Ensures correct continuation after
interruptions.
• Essential for process resumption.
CPU Registers
• Temporary storage inside the CPU
(accumulator, stack pointer, etc.).
• PCB saves register values when process is
switched out.
• Ensures accurate process resumption.
Memory Info
• Information about process memory usage.
• Includes base and limit registers, page tables,
segment tables.
• Helps OS know where process instructions
and data are stored.
I/O Status
• Details of input/output devices allocated to
the process.
• Includes information about open files,
pending I/O requests.
• Tracks usage of devices by the process.
Role of PCB
• Enables context switching
• Tracks CPU & memory usage
• Maintains process integrity & security
• Essential for scheduling and resource
allocation
Summary
• Process = running instance of a program
• OS keeps a PCB for every process
• Process moves among states (New, Ready,
Running, Waiting, Terminated)
• PCB + state transitions allow multitasking &
efficient resource use
Process Scheduling in Operating
Systems
Scheduling Queues, Types of
Schedulers, and Context Switching
Introduction to Process Scheduling
• Process scheduling decides which process
runs on the CPU.

• Goals:
• Maximum CPU utilization
• Minimum waiting time
• Fairness among processes
• Balanced throughput and response time
Scheduling Queues
• When a process enters the system, it is placed in a
queue:

• Types:
• Job Queue → All processes entering system
• Ready Queue → Processes waiting for CPU
• Device Queue → Processes waiting for I/O

• Flow: New → Job Queue → Ready Queue → CPU →


(I/O → Device Queue → Ready Queue) → Termination
Long-Term Scheduler
• Selects processes from Job Queue → Ready
Queue

• Features:
• Controls degree of multiprogramming
• Executes less frequently
• Example: Batch systems – decides which jobs
to admit
Medium-Term Scheduler
• Temporarily suspends/swaps out processes

• Features:
• Balances CPU-bound and I/O-bound
processes
• Reduces system load
• Example: Time-sharing systems – suspends
background processes
Short-Term Scheduler (CPU Scheduler)
• Selects processes from Ready Queue → CPU

• Features:
• Runs very frequently (milliseconds)
• Uses scheduling algorithms:
• – FCFS, SJF, Priority, Round Robin, Multilevel Queue

• Frequency:
• Long-term → Minutes
• Medium-term → Seconds
• Short-term → Milliseconds
Context Switch
• Switching CPU from one process to another

• Steps:
• Save PCB (Program Counter, Registers, State)
• Load PCB of new process
• Resume execution

• Characteristics:
• Pure overhead (no useful work)
• More processes → More context switches
• Time depends on hardware
Summary
• Scheduling Queues: Job, Ready, Device
• Schedulers:
• – Long-term → Controls multiprogramming
• – Medium-term → Suspends/resumes
processes
• – Short-term → Selects process for CPU
• Context Switch: Saves/restores process states
Inter-Process Communication
(IPC)
Shared Memory vs. Message Passing
Introduction to IPC
• IPC allows processes to exchange data and coordinate actions.

• Goals:
• Share information
• Increase speed & performance
• Ensure synchronization
• Achieve modularity

• Two models:
• Shared Memory System
• Message Passing System
Shared Memory System
• Processes share a region of memory for communication.

• How it works:
• OS creates shared memory segment → a block in RAM.
• Processes attach it to their address space
• Any process can read/write :(like a global variable).
• Because multiple processes write/read at the same time, synchronization
(semaphores/mutexes) is needed.

• Example: Producer-Consumer Problem

• Advantages:
• Very fast (no kernel after setup)
• Good for large data transfer

• Disadvantages:
• Needs synchronization (mutex, semaphores)
• Complex to implement safely
Producer-Consumer Problem with Shared
Memory

• Producer → generates data and puts it into


shared memory (buffer)
• Consumer → takes data out of shared
memory and uses it
• Problem → If producer writes when buffer
is full or consumer reads when buffer is
empty → inconsistency
Example Working

• Shared memory segment is created as a buffer (array


of size N)
• Producer process writes items into buffer
• - Before writing, checks if buffer is not full
• - Uses a semaphore/mutex to ensure no conflict
• Consumer process reads items from buffer
• - Before reading, checks if buffer is not empty
• - Uses a semaphore/mutex to ensure safe access
Synchronization
• Both coordinate using synchronization:
• - Producer waits if buffer is full
• - Consumer waits if buffer is empty
• Ensures consistency and avoids data loss
Message Passing System
• Processes communicate by sending/receiving messages via OS.

• How it works:
• OS provides send() and receive()
• Direct (process-to-process) or Indirect (via mailbox)

• Example: Client-Server model

• Advantages:
• Simpler than shared memory
• Works in distributed systems

• Disadvantages:
• Slower (kernel involvement, data copying)
• Overhead of system calls
Summary
• IPC enables data exchange & synchronization

• Shared Memory:
• Fast, efficient
• Needs synchronization

• Message Passing:
• Simple, secure
• Works across machines
• Slower due to kernel involvement
Threads in Operating System
What is a Thread?
• In an operating system, a thread is a lightweight unit
of execution within a process. A process is an
instance of a program that is being executed, and a
thread is a subset of the process that can run
concurrently with other threads within the same
process.
• It represents a single sequence of instructions
executed by the CPU.
• Threads share resources of the process (memory,
files, code).
• Each thread has its own program counter, registers,
and stack.
Program

executed
Advantages of Threads
1. Improved Performance (Faster Execution):
• Multiple threads can execute simultaneously on multi-core CPUs.
• Example: Web browser – separate threads for rendering,
downloading, and media.

• 2. Reduced Context Switching Time


• Switching between processes requires saving and restoring all process
information (program counter, registers, memory maps, etc.), which is
relatively slow.
• Switching between threads of the same process is faster because they
share the same memory and resources, so only the thread-specific
data (program counter, stack, registers) needs to be saved and
restored.
User-Level vs Kernel-Level
Threads
User-Level Threads
• Managed by user-level thread libraries, not the OS kernel.
• Kernel is unaware of individual threads.
• Context switching is fast (done in user space).
• Scheduling handled by thread library.

• Advantages:
• - Fast creation and switching.
• - Works on any OS without kernel support.

• Disadvantages:
• - If one thread blocks, the whole process blocks.
• - Cannot fully utilize multiple CPUs.
User Level Threads
Kernel-Level Threads
• Managed directly by the operating system kernel.
• Kernel is aware of each thread and schedules them.
• Context switching is slower (system call needed).
• True parallelism on multiprocessor systems.

• Advantages:
• - If one thread blocks, others can still run.
• - Better multiprocessor utilization.

• Disadvantages:
• - Slower to create and manage.
• - More overhead compared to ULTs.
Kernel Level Threads
Feature User-Level Threads (ULTs) Kernel-Level Threads (KLTs)

Managed By User thread library OS Kernel

Kernel Awareness No Yes

Context Switching Fast (no system call) Slower (system call needed)

Multiprocessor Use Limited Full utilization

Blocking Issue Blocks entire process Only blocks that thread


Multithreading Models in
Operating Systems
Many-to-One, One-to-One, and
Many-to-Many
Many-to-One Model
• Multiple user-level threads → One kernel thread
• Thread management done in user space
• Kernel is unaware of individual threads

• Advantage:
• Fast thread switching (no kernel involvement)

• Disadvantages:
• If one thread blocks, entire process blocks
• Cannot use multiple CPUs effectively
One-to-One Model
• Each user-level thread → One kernel thread
• Kernel is fully aware and schedules all threads

• Advantages:
• True parallelism on multiprocessors
• If one thread blocks, others continue

• Disadvantage:
• High overhead if too many threads

• Example: Windows, Linux (POSIX Pthreads)


Many-to-Many Model
• Many user-level threads → Many kernel threads (M:N)
• User threads scheduled dynamically onto kernel threads

• Advantages:
• Flexible, combines benefits of ULTs & KLTs
• Efficient resource utilization
• Multiple threads can run in parallel

• Disadvantage:
• • Complex implementation

• Example: Solaris, Windows NT (older versions)


• Modern OS use One-to-One Model

• Windows → One-to-one with kernel threads


• Linux → Pthreads (POSIX), kernel threads
• macOS → One-to-one via Mach kernel

• Reason:
• Multi-core processors allow true parallelism
• Kernel efficiently handles scheduling, blocking, load balancing

• Many-to-Many was used in some older systems (like Solaris,


Windows NT), but it’s rare today because it adds complexity
without much benefit in modern hardware
Process Commands in Operating
System
Introduction
• In Linux/Unix, a process is a program in execution.
• The OS provides commands to monitor, control, and manage
processes.

• Common Commands:
• top
• ps
• kill
• wait
• sleep
• exit
• nice
top Command
• Displays a real-time view of running processes.
• Shows PID, CPU usage, memory usage,
running time.
• Helps monitor system performance.

• Example:
• top
• (Press q to quit)
ps Command (Process Status)
• Displays snapshot of current processes.
• Shows PID, terminal, CPU time, and
command.

• Examples:
• ps → processes of current shell
• ps -e → all processes
• ps -ef → detailed info about all processes
kill Command
• Used to terminate a process by its PID.
• Sends a signal to the process (default
SIGTERM).

• Examples:
• kill 1234 → kills process with PID 1234
• kill -9 1234 → forcefully kills (SIGKILL)
wait Command
• Makes a process wait until another finishes.
• Useful in shell scripts.

• Example:
• ./longtask &
• wait
• echo "Task finished"
sleep Command
• Suspends execution for given time (seconds).

• Example:
• sleep 5
• echo "This prints after 5 seconds"
exit Command
• Terminates current shell or script.
• Can return exit status code.

• Examples:
• exit 0 → success
• exit 1 → error
nice Command
• Starts a process with a priority level.
• Default priority = 0.
• Lower value = higher priority, higher value = lower
priority.

• Examples:
• nice -n 10 ./myprogram → run with lower priority
• nice -n -5 ./myprogram → run with higher priority
(root needed)
Summary of Commands
• top → live process monitoring
• ps → snapshot of processes
• kill → terminate process
• wait → wait for process completion
• sleep → pause execution
• exit → end shell/script
• nice → run process with set priority
THANK YOU

You might also like