PROCESS MANAGEMENT
What is a Process?
● A program in execution
● Has its own memory space in the RAM
● Has threads, which may run concurrently or in parallel
● An operating system has many processes, which may run user programs (source
codes).
● Note that process is a unit of work, meaning it corresponds to a task.
PROCESS MEMORY LAYOUT
1. Text - the source code/executable code.
2. Data - global variables.
3. Heap - dynamically-allocated memory during runtime.
4. Stack - temporary data storage when invoking functions (e.g., return statements, local
variables, and parameters).
PROCESS STATES
1. New - occurs when the process is created.
2. Ready - occurs when the process has acquired all of its needed resources, but is waiting
for a processor.
3. Waiting - occurs when the process is waiting for an event to occur (in most cases, this is
I/O).
4. Running - the instructions of the process are being executed.
5. Terminated - the process has finished execution and releases all of its acquired
resources.
PROCESS CONTROL BLOCK
● For each time a process is created, a process control block is created to track it.
1. Program State - New, Waiting, Ready, Running, Terminated
2. Process Number - Process ID (a unique identifier for each process)
3. Program Counter - holds the address of the next instruction to be executed.
4. Register - small storage for temporary data
5. Memory Limits - range of memory space allocated to a process.
6. List of Open Files - I/O Devices and Files utilized by a process.
THREADS
● A smaller unit of execution.
● Modern computers and operating systems can perform multiple threads of execution at
once, which leads to parallelism.
● Has its own PID and state, but relies on shared memory and system resources.
PROCESS SCHEDULING
● A process scheduler selects a ready program.
● Each CPU core can run one process at a time. this suggests that:
○ Single CPU Core - only one process is executed at a time.
○ Multicore System - more than one process is executed at a time.
○ Number of Process > Number of Cores - excess processes must wait for an
available core.
MULTIPROGRAMMING
● Programs are stored in the main memory, and the CPU switches between them.
● The number of programs currently held by the memory is called the Degree of
Multiprogramming.
● CPU Bound: A process that spends more time in the processor/CPU or computing.
● I/O Bound: A process that spends more time on I/O devices.
TIME-SHARING
● A CPU core switches among processes frequently.
SCHEDULING QUEUE
A new process is created -> Enters ready queue -> The CPU Scheduler selects among
processes in the ready queue, which gets allocated a CPU Core -> If it needs an I/O operation,
it enters the waiting queue -> If an input has been received, it goes back to the ready queue ->
Sent to the CPU for execution -> Repeats until the process is completed.
CONTEXT SWITCH
● The current progress of a process is saved in its Process Control Block so the CPU can
load it again in the future.
● The operating system stops a process from running and runs another in the CPU.
PROCESS OF CONTEXT SWITCH
1. Context switches are triggered by interrupts, which are signals that require immediate
attention.
2. The OS saves the process's state, memory management, and program counter into its
Process Control Block.
3. The OS loads the progress of the next process from its PCB.
PROCESS CREATION
● A running program may create another process along the way, making it a parent
process.
● The process created at runtime is called a child process, which is given a PID.
● A child process may create another child process, resulting in a tree.
● A process may have children processes, making it a parent process.
PROCESS TERMINATION
● A parent process can terminate its children.
● When a process finishes with its last instruction, it must call exit() to return its exit status
to the parent process.
● The parent process can retrieve the resources of the child process using the wait()
system call.
CASCADING TERMINATION
● Occurs when child processes are forcibly terminated when their parent process
terminates.
● Note that a parent can terminate its children if:
○ The child processes overuses resources.
○ The work of the child process is no longer needed.
○ If the parent process is already exiting the operating system.
● Zombie Processes - a child process finishes execution, but its exit status is still kept in
the process table because the parent has not yet called wait().
● Orphan Processes - a parent gets terminated before its child without calling wait().
INTERPROCESS COMMUNICATION
● A protocol for cooperating processes to exchange data and communicate with each
other.
(a). SHARED MEMORY
● A specific memory segment is shared/used by many processes.
● Process A writes to a specific memory segment that Process B reads from.
● The OS does not interfere with how data is stored or access, as the processes
themselves coordinate to avoid conflicts (e.g., writing on the same memory location at
the same time).
(b). MESSAGE PASSING
● Processes send and receive messages through channels managed by operating
systems.
● A message can have a fixed size (easy for the operating system) or variable size (easy
for the programmer).
● Communication Link: A link must exist between communicating processes, which is
provided by the OS itself.
NAMING IN MESSAGE PASSING IPC
(b1). DIRECT COMMUNICATION
● Both the sender and the receiver must name each other when sending
messages.
● Exactly one link exists between the sender and the receiver.
● One-to-one communication.
(b2). ASYMMETRIC DIRECT COMMUNICATION
● only the sender must name the receiver.
(b3). INDIRECT COMMUNICATION
● A message is sent and received from a shared mailbox (port).
● A port has a unique identifier.
● A link between two processes will only be generated if they share a mailbox.
● There can be more than one links between the same processes (one per
mailbox).
● A mailbox can cater many-to-many communication.
● A mailbox can be owned by a process or the OS itself.
○ Process-Owned Mailbox - lives as long as the process exists. Other
processes can only send messages.
○ OS-Owned Mailbox - The OS provides calls to create, use, and delete a
mailbox.
● For multiple receivers, only one should receive first before the other.
(c). BUFFERING
● Defines whether and when a sender has to wait.
● A sent message is stored temporarily in a queue before the receiver retrieves it.
TYPES OF BUFFERRING
(c1). Zero Capacity
● Queue length is 0.
● The sender must block until the receiver is ready.
(c2). Bounded Capacity
● Queue length is n.
● If the queue is not full, the sender can continue running.
● If the queue is full, the sender blocks until space is made.
(c3). Unbounded Capacity
● Queue length is infinite.
● Sender never blocks.
● Receiver still receives one by one.
Pipe: Unidirectional communication channel between processes.
Socket: Bidirectional communication (full-duplex) between processes.