Process Management
Unit 2
Marks-14
Process
⚫ A process is simply a program in
execution.
⚫ Throughout its life, a process doesn't just
run continuously; it moves through
different stages or states.
Process States
1. NEW
2. READY
3. RUNNING
4. WAITING
5. TERMINATED
Process States
1. New: The process has just been created. It
exists but hasn't been loaded into the main
memory yet.
2. Ready: The process is in the main memory
and is ready to execute. It's waiting for its
turn to be assigned to the CPU. Running:
The process's instructions are currently
being executed by the CPU. At any given
moment, a single-core CPU can only have
one process in this state
3. Waiting (or Blocked): The process has
stopped its execution and is waiting for an
event to happen. This could be waiting for
user input, a file to be read, or a device to
become available
New State: Process Creation
⚫ fork(): The fork() call creates a child
process that is an exact copy of the parent
process, including its memory space and
open files. The parent and child then
execute independently.
⚫ exec(): This family of system calls (execve(),
execlp(), etc.) is used to load and execute a
new program within the context of an
existing process.
⚫ It replaces the current process's code, data,
and stack with a new program, effectively
"transforming" the process.
⚫ A common pattern is for a parent process
to fork() a child, and then the child calls
Running to Waiting/Blocked
⚫ read()/write(): These system calls are used for I/O
operations. When a process calls read() to get data
from a file or device, it will be blocked until the
data is available. Similarly, write() can block if the
output buffer is full.
⚫ wait()/waitpid(): A parent process uses these calls
to pause its own execution until one of its child
processes terminates. This is crucial for managing
child processes and preventing them from
becoming "zombies."
⚫ sleep(): This system call voluntarily puts the
process into a waiting state for a specified amount
of time. It's used when a process has nothing to do
for a period but doesn't want to waste CPU time.
⚫ sem_wait() and sem_post(): These are used for
process synchronization. A process calling
sem_wait() might be blocked if a semaphore's
Running to Terminated State
⚫ A process enters the Terminated state when its
execution is complete, either normally or
abnormally. This is when its resources are
released.
⚫ exit(): A process calls exit() to voluntarily
terminate its own execution. This is the
normal way for a process to finish.
⚫ The OS reclaims all resources (memory, file
descriptors, etc.) used by the process.
⚫ kill(): This system call is used by one process
to send a signal to another process. While it's
used for many purposes, one of its primary
uses is to force another process to terminate
(e.g., sending a SIGKILL signal).
Process Control Block
⚫ The Process Control Block (PCB) is a data
structure in the operating system that
holds all the essential information about a
process.
⚫ It acts as a "passport" or "ID card" for each
process.
⚫ When a process switches from one state to
another, the OS saves its current context in
the PCB so it can be resumed later from
exactly where it left off.
⚫ Process ID (PID): A unique number assigned
to each process to identify it.
⚫ Process State: The current state of the process
(e.g., Running, Ready, Waiting).
⚫ Program Counter: This is a pointer that holds
the memory address of the next instruction to
be executed. It's crucial for resuming a process
after it has been interrupted.
⚫ CPU Registers: These are small, fast memory
locations in the CPU. The values of these
registers are saved in the PCB so that the
process's work can be restored accurately.
⚫ Memory Management Information: This
includes details about the memory allocated to
the process, such as page tables or segment
tables.
⚫ I/O Status Information: A list of I/O devices
(like a printer or a scanner) that the process is
using and the open files.
⚫ Accounting Information: Details like the
Types of Queues in OS
[Link] Queue
⚫ The job queue holds all the processes in
the system. When a new process is created,
it's placed in this queue.
⚫ It's essentially the entry point for all
processes and contains every process
regardless of its current state (e.g., waiting
for I/O, ready to run, etc.).
⚫ The operating system's long-term
scheduler selects processes from this queue
2. Ready Queue
⚫ The ready queue contains processes that
are ready and waiting to be assigned to a
CPU.
⚫ These processes are in the main memory
and are in the "ready" state. When a CPU
becomes available, the operating system's
short-term scheduler selects a process
from this queue to execute.
⚫ This queue is often implemented as a
linked list or a circular queue and is crucial
3. Device Queues
⚫ Device queues are a collection of processes
that are blocked and waiting for a specific I/O
device.
⚫ For example, a process might be waiting for a
printer, a disk, or a keyboard. Each device has
its own queue.
⚫ When a process requests a device, it's placed
in that device's queue until the device becomes
available.
⚫ Once the I/O operation is complete, the
Types of Schedulers in OS
1. Long-Term Scheduler ( Job Scheduler)
⚫ The long-term scheduler decides which processes
from a pool of jobs in secondary storage (like a hard
disk) should be loaded into main memory (RAM) and
placed in the ready queue.
⚫ Its primary function is to control the degree of
multiprogramming, which is the number of processes
currently in memory.
⚫ The long-term scheduler aims to achieve a good
balance between I/O-bound processes (those that
spend more time waiting for I/O operations) and
CPU-bound processes (those that spend more time
using the CPU) to prevent the CPU from being idle.
2. Short-Term Scheduler (CPU Scheduler)
⚫ The short-term scheduler selects a process
from the ready queue and allocates the CPU to
it.
⚫ This is the fastest and most frequently
executed scheduler, as it's invoked whenever a
process needs to be switched (e.g., when a
running process terminates, waits for I/O, or is
interrupted).
⚫ The short-term scheduler is where various
CPU scheduling algorithms (such as
3. Medium-Term Scheduler (Swapper)
⚫ The medium-term scheduler is responsible for
moving processes between main memory and
secondary storage, a process known as swapping.
⚫ This is typically done to manage the degree of
multiprogramming and free up memory.
⚫ When a process becomes idle or is of a low priority,
the medium-term scheduler can temporarily "swap it
out" of main memory and onto the disk to make room
for other processes.
⚫ When the process is ready to continue, it can be
"swapped in" again. This scheduler runs more
Inter process Communication in
OS
⚫ Inter-process communication (IPC) is a set of
mechanisms provided by an operating system
that allows processes to exchange data and
synchronize their actions.
⚫ This is essential for cooperating processes
that need to work together to complete a task.
1. Shared Memory
2. Message Passing
1. Shared Memory
⚫ Shared memory is one of the fastest IPC
methods.
⚫ It involves creating a region of memory
that is shared by multiple processes.
⚫ Once the memory is established, processes
can exchange data by reading from and
writing to this shared region.
⚫ The operating system provides
mechanisms for creating and managing
this shared space, but the processes
themselves are responsible for
synchronizing access to prevent conflicts
and ensure data consistency.
[Link] Passing
⚫ Message passing is a mechanism where processes
communicate by sending and receiving messages.
⚫ The operating system manages a message queue or a
channel for communication.
⚫ A sending process formats a message and sends it to
the destination process.
⚫ The receiving process then retrieves the message
from the queue.
⚫ This method is often used for exchanging small
amounts of data and can be implemented in both
synchronous (sender blocks until the message is
⚫ Processes: On the left, you have "Process A,"
and on the right, "Process B." These represent
two separate programs or threads that need to
exchange data.
⚫ Message Queue: In the center is the "Message
Queue," which acts as a buffer managed by the
operating system. It is a shared data structure
where messages are temporarily stored. The
diagram shows it containing multiple smaller
blocks, representing individual messages
waiting to be processed.
⚫ Sending a Message: The arrow from Process A
to the Message Queue labeled "Send Message
(msg)" demonstrates the first step. Process A
packages the data it wants to send into a
message and uses a system call to place it into
the queue.
⚫ Receiving a Message: The arrow from the
Message Queue to Process B labeled "Receive
Message (msg)" shows the second step. Process
B uses a system call to check the queue and
Threads in OS
⚫ A thread is the smallest unit of execution
within a process. It's often called a "lightweight
process" because it's a fundamental part of a
process that can be managed and scheduled
independently by the operating system.
⚫ Thread is a unit of execution that exists within
a process.
⚫ All threads belonging to the same process share
the process's code, data, and resources (like
open files and sockets). However, each thread
has its own:
1. Program Counter: Tracks the next instruction to
be executed.
2. Register Set: Holds temporary data during
execution.
3. Stack: Stores local variables and function calls for
Types of Threads
[Link]-Level Threads (ULT):
⚫ These are managed entirely in user space by
a thread library. The operating system
kernel is unaware of their existence.
⚫ Advantages: They are very fast to create and
manage because no kernel calls are
required.
⚫ Disadvantages: If one user-level thread
performs a blocking system call, the entire
process and all of its threads will block, as
the kernel treats the process as a single unit
[Link]-Level Threads (KLT):
These are managed directly by the operating
system kernel. The kernel has a thread table
to keep track of all threads in the system.
⚫ Advantages: If one thread blocks, the
kernel can schedule another thread from
the same process, preventing the entire
process from halting. They can also be
scheduled on multiple processors for true
parallelism.
⚫ Disadvantages: They are slower to create
and manage than user-level threads
because every operation requires a system
call to the kernel.
1. Many-to-One Model
⚫ Many user-level threads
are mapped to a single
kernel thread.
⚫ Advantages:
⚫ Fast thread management
(done in user space).
⚫ Simple implementation.
⚫ Disadvantages:
⚫ If one thread blocks, the
whole process blocks.
⚫ Cannot take advantage
of multiprocessor
systems (only one kernel
thread runs at a time).
2. One-to-One Model
⚫ Each user thread maps to a separate kernel thread.
⚫ Advantages:
⚫ True parallelism on multiprocessor systems.
⚫ If one thread blocks, others can still run.
⚫ Disadvantages:
⚫ Higher overhead (creating a kernel thread for each user
thread).
⚫ Limited number of threads (depends on OS and resources).
3. Many-to-Many Model
⚫ Many user threads are
mapped to a smaller or equal
number of kernel threads.
⚫ Advantages:
⚫ Combines benefits of both
previous models.
⚫ User can create many
threads, kernel schedules
only necessary ones.
⚫ Better resource utilization
and concurrency.
⚫ Disadvantages:
⚫ More complex to
implement.
⚫ Needs good coordination
between user-level thread
library and OS.
top
Column Meaning
PID Process ID (unique number assigned to each process).
USER The user who owns the process.
PR Priority of the process (lower number = higher priority).
Niceness value (nice level). Range: -20 (highest priority) → 19
NI
(lowest).
Virtual memory used by the process (total memory the process can
VIRT
access).
RES Resident memory (actual physical RAM used, not swapped).
SHR Shared memory (can be shared with other processes).
S Process state: R (running), S (sleeping), T (stopped), Z (zombie).
%CPU CPU usage percentage for the process.
%MEM Memory usage percentage of total physical RAM.
Total CPU time used by the process (in format
TIME+
minutes:[Link]).
COMMAND The command/program that started the process
ps :shows processes for current
shell
ps -e # shows all processes
ps -ef :full details of all
processes
⚫ wait <PID> # wait until process
with PID finishes
⚫ wait # wait for all
background jobs to finish
⚫ sleep <seconds>
⚫ sleep 5 # sleep for 5 seconds
⚫ sleep 1m # sleep for 1 minute
⚫ nice -n <priority> command
⚫ nice -n 10 sleep 60 # run sleep with
lower priority
⚫ renice <priority> -p <PID>