Process and Memory Management
UNIT 2:
Process Management: Process concepts, operations on processes, IPC, Process Scheduling, Multithreaded
programming
Memory management: Memory allocation, Swapping, Paging, Segmentation, Virtual Memory, various faults
Process Management
Process Concepts:
A process is an instance of a running program. It encompasses the program code, data, and resources required
for execution, such as memory and I/O devices. Each process maintains its own distinct address space,
execution state, and associated resources. This isolation enables concurrent execution of multiple
processes, ensuring that they operate independently without interfering with one another.
Operations on Processes:
- Create: Creating a new process involves allocating resources such as memory, setting up
the process control block, and initializing the execution environment. The new process is then added to
the list of active processes.
- Destroy: Terminating an existing process deallocates its resources, including memory,
files, and other system resources. The process control block is removed, and any child processes are
orphaned or adopted by the operating system.
- Suspend: Temporarily pausing the execution of a process involves suspending its
execution, saving its state, and releasing the CPU resources it was using. The process remains in a
suspended state until it is resumed.
- Resume: Resuming the execution of a suspended process involves restoring its saved
state and allocating CPU resources for its execution. The process continues its execution from the point
where it was suspended.
- Block: Blocking a process involves temporarily halting its execution until a certain
event occurs. This can include waiting for user input, I/O operations to complete, or signals from other
processes.
- Wake up: Waking up a blocked process occurs when the event it was waiting for happens.
The process is then unblocked and made ready for execution, either immediately or after a scheduling
decision.
Inter-Process Communication (IPC):
IPC facilitates communication and synchronization between processes running concurrently within an operating
system. It enables processes to exchange data, coordinate activities, and share resources. Several
mechanisms are employed for IPC, including:
- Shared Memory: Processes share a portion of memory, allowing them to read from and
write to the same memory locations. This facilitates fast communication between processes, but proper
synchronization mechanisms are necessary to prevent data corruption.
- Message Passing: Processes communicate by sending and receiving messages through a
communication channel established by the operating system. Message passing can be implemented using
various techniques such as queues, sockets, and signals.
- Semaphores: Semaphores are synchronization primitives used to control access to shared
resources by multiple processes. They allow processes to coordinate their activities and prevent race
conditions by enforcing mutual exclusion and synchronization.
- Pipes: Pipes provide unidirectional communication channels between processes. They
allow the output of one process to be connected to the input of another process, enabling data to be
passed between them. Pipes can be either anonymous (unnamed) or named, depending on their usage.
Process Scheduling:
Process scheduling is a vital aspect of operating system design aimed at efficiently utilizing CPU resources.
Various scheduling algorithms are employed to determine the order in which processes are executed. Some
common scheduling algorithms include:
- First Come First Serve (FCFS): Processes are executed in the order they arrive in the
ready queue. It is simple and easy to implement but may lead to poor average waiting times, especially
for long-running processes.
- Shortest Job Next (SJN): Also known as Shortest Job First (SJF), this algorithm selects
the process with the shortest estimated run time for execution. It minimizes average waiting time and is
optimal for certain scenarios, but it requires knowledge of the burst time of each process, which may
not be available in practice.
- Round Robin: Processes are executed in a circular queue, with each process given a
fixed time quantum (also known as a time slice or time slot) for execution. If a process does not
complete within its time quantum, it is preempted and placed back in the queue. Round Robin is fair and
ensures that no process monopolizes the CPU for too long, but it may result in high context-switching
overhead.
- Priority Scheduling: Each process is assigned a priority, and the scheduler selects the
process with the highest priority for execution. Priority can be determined based on factors such as
process type, importance, or resource requirements. Priority scheduling can be either preemptive or
non-preemptive, depending on whether processes can be interrupted during execution.
Multi-threaded Programming:
Multi-threaded programming involves creating and managing multiple threads within a single process. Threads
share the same address space and resources, enabling concurrent execution and potentially improving
performance by utilizing available CPU resources more effectively. Key concepts in multi-threaded
programming include:
- Thread Creation and Termination: Threads are created and terminated dynamically within
a process. Thread creation involves allocating resources and initializing the thread's execution
context, while termination involves releasing resources and cleaning up the thread's state.
- Thread Synchronization (Mutexes, Semaphores): Thread synchronization mechanisms such as
mutexes (mutual exclusion locks) and semaphores are used to coordinate access to shared resources and
prevent race conditions. Mutexes ensure that only one thread can access a resource at a time, while
semaphores allow for more complex synchronization patterns, such as limiting the number of threads
accessing a resource simultaneously.
- Thread Communication (Condition Variables): Condition variables are synchronization
primitives that allow threads to wait for a particular condition to become true before proceeding. They
are commonly used in conjunction with mutexes to implement synchronization patterns such as
producer-consumer and reader-writer.
- Thread Safety and Race Conditions: Ensuring thread safety involves designing concurrent
algorithms and data structures in such a way that they can be accessed and modified by multiple threads
without causing data corruption or inconsistencies. Race conditions occur when the outcome of a
computation depends on the timing or interleaving of thread execution, leading to unpredictable
behavior.
Memory Management
Memory Allocation:
Memory allocation is the process of assigning memory space to processes and data structures. It is a critical
aspect of memory management and plays a vital role in system performance and efficiency. Here are some
common memory allocation techniques:
- Contiguous Allocation: In contiguous allocation, memory is divided into fixed-size
partitions, and processes are allocated contiguous blocks of memory. This technique is simple and
efficient but may lead to fragmentation.
- Linked Allocation: Linked allocation uses linked lists to manage memory allocation.
Each process is assigned a series of non-contiguous memory blocks, linked together using pointers. This
technique reduces fragmentation but requires additional overhead for pointer management.
- Paging: Paging divides memory into fixed-size blocks called pages and allocates memory
to processes in page-sized increments. It allows for efficient use of memory and supports virtual memory
systems.
- Segmentation: Segmentation divides the logical address space of a process into
variable-size segments, each representing a different type of data or code. This technique provides
protection and flexibility in memory allocation.
- Dynamic Memory Allocation: Dynamic memory allocation allows processes to request memory
dynamically at runtime. Common functions such as malloc() and free() are used to allocate and deallocate
memory dynamically.
Effective memory allocation is essential for optimizing system performance, minimizing memory waste, and
preventing memory-related issues such as fragmentation and out-of-memory errors.
Swapping:
Swapping is a memory management technique used by the operating system to temporarily move data from the main
memory (RAM) to disk storage when the RAM is full. This process helps in freeing up memory space for other
programs and managing memory efficiently. Here's how swapping works:
- Memory Overcommitment: When the system runs out of physical memory (RAM) due to the
simultaneous execution of multiple programs, the operating system may overcommit memory by allowing
processes to use more memory than physically available.
- Selection Criteria: The operating system selects which processes or parts of processes
to swap out based on various criteria such as the priority of the process, the amount of time it has
been running, and the amount of memory it is currently using.
- Swapping Process: When a process is selected for swapping, the operating system
transfers its data from RAM to a designated area on the disk known as the swap space or swap file. This
area serves as a temporary storage location for swapped-out processes.
- Page Replacement: Swapping may involve page replacement, where the operating system
selects pages (small portions of memory) to be swapped out based on a page replacement algorithm such as
Least Recently Used (LRU) or First-In-First-Out (FIFO).
- Swapping Policies: The operating system employs swapping policies to efficiently manage
the swapping process, minimize disk I/O operations, and optimize system performance.
Swapping helps prevent system crashes due to out-of-memory errors and enables the system to handle a larger
number of processes than can fit into physical memory. However, excessive swapping can degrade system
performance due to the slower speed of disk storage compared to RAM.
Paging:
Paging is a memory management technique used by the operating system to divide physical memory into
fixed-size blocks called pages. Similarly, processes are also divided into fixed-size blocks called pages.
This technique enables efficient memory management by allowing non-contiguous allocation of memory. Here's
how paging works:
- Page Size: The operating system divides memory into pages of a fixed size, typically
ranging from 4 KB to 8 KB. Each page represents a contiguous block of physical memory.
- Process Segmentation: When a process is loaded into memory, it is divided into pages of
the same size as the physical memory pages. Each page of the process corresponds to a page in physical
memory.
- Address Translation: The operating system maintains a page table for each process,
which maps virtual addresses (used by the process) to physical addresses (used by the hardware). The
page table is used for address translation during memory access.
- Page Faults: If a requested page is not present in physical memory (a condition known
as a page fault), the operating system retrieves the required page from secondary storage (e.g., disk)
and updates the page table accordingly.
- Benefits: Paging allows for efficient memory utilization by allocating memory in
smaller, more manageable units. It also enables memory protection and sharing among processes, as each
page can be protected and managed independently.
- Drawbacks: Paging may incur overhead due to page table management and address
translation. Additionally, frequent page faults can lead to performance degradation, especially if disk
access is slow.
Paging is a fundamental technique used in modern operating systems to provide virtual memory and ensure
efficient memory management.
Segmentation:
Segmentation is a memory management technique used by the operating system to divide the logical address
space of a process into variable-size segments. Each segment represents a different type of data or code,
such as the program code, stack, heap, and data. Segmentation provides protection and flexibility in memory
management. Here's how segmentation works:
- Segmentation Units: The logical address space of a process is divided into segments,
each with its own base address and length. Segments can vary in size and represent different parts of
the process.
- Segmentation Table: The operating system maintains a segmentation table for each
process, which maps segment numbers to base addresses and lengths. The segmentation table is used for
address translation during memory access.
- Address Translation: When a process accesses memory, the operating system translates
the logical addresses used by the process into physical addresses used by the hardware. This translation
involves adding the base address of the segment to the logical address to obtain the corresponding
physical address.
- Protection: Segmentation provides protection by assigning different access rights to
different segments. For example, the program code segment may be marked as read-only to prevent
modification, while the data segment may allow both read and write operations.
- Flexibility: Segmentation allows for flexible memory allocation and management by
dividing the address space into segments of variable size. This flexibility enables efficient memory
utilization and supports dynamic memory allocation.
- Segmentation Faults: If a process accesses an invalid segment or attempts to access
memory beyond the segment boundaries, a segmentation fault occurs. The operating system handles
segmentation faults by terminating the offending process or notifying the user.
Segmentation is a key memory management technique used in modern operating systems to provide protection,
flexibility, and efficient memory utilization.
Virtual Memory:
Virtual memory is a memory management technique used by the operating system to extend the available physical
memory by using disk space as an extension. It allows processes to use more memory than physically available
and provides protection and efficient memory management. Here's how virtual memory works:
- Address Translation: When a process accesses memory, the operating system translates
the virtual addresses used by the process into physical addresses used by the hardware. This translation
involves mapping virtual addresses to physical addresses using a page table.
- Page Faults: If a requested page is not present in physical memory (a condition known
as a page fault), the operating system retrieves the required page from secondary storage (e.g., disk)
and updates the page table accordingly. This process is known as demand paging.
- Swap Space: The operating system maintains a portion of disk space known as swap space
or paging file, which serves as a temporary storage location for swapped-out pages. Swap space allows
the operating system to store pages that are not currently in use in physical memory.
- Memory Protection: Virtual memory provides memory protection by isolating the address
space of each process and preventing unauthorized access to memory locations. Each process has its own
virtual address space, which is managed and protected by the operating system.
- Efficient Memory Utilization: Virtual memory enables efficient memory utilization by
allowing processes to use more memory than physically available. It also supports dynamic memory
allocation and sharing among processes, resulting in improved system performance.
- Benefits: Virtual memory provides several benefits, including increased system
responsiveness, support for larger and more complex applications, and improved multitasking
capabilities.
- Drawbacks: Despite its advantages, virtual memory may incur performance overhead due to
frequent page faults and disk access. Excessive swapping between physical memory and disk can degrade
system performance.
Virtual memory is a fundamental memory management technique used in modern operating systems to provide
flexibility, protection, and efficient memory utilization.
Various Faults:
Various faults occur during memory access, such as page faults and segmentation faults. These faults are
handled by the operating system to ensure proper memory management and prevent program crashes. Here's an
overview of these faults:
- Page Faults: Occur when a process attempts to access a page that is not currently
present in physical memory. The operating system handles page faults by retrieving the required page
from secondary storage (e.g., disk) and updating the page table accordingly.
- Segmentation Faults: Occur when a process attempts to access an invalid memory location
or performs an unauthorized memory operation. Segmentation faults are typically caused by programming
errors, such as accessing an uninitialized pointer or dereferencing a null pointer.
- Handling: The operating system handles various faults by terminating the offending
process or notifying the user of the error. Page faults are transparently handled by the operating
system through demand paging, while segmentation faults result in program termination and may generate
error messages or core dumps for debugging purposes.
- Prevention: Various faults can be prevented through proper memory management
techniques, such as bounds checking, memory protection, and error handling in software development.
Additionally, hardware mechanisms, such as memory protection units (MPUs) and memory management units
(MMUs), help detect and prevent unauthorized memory accesses.
- Impact: Faults can impact system performance and stability by causing program crashes,
resource contention, and degraded responsiveness. Proper handling and prevention of faults are essential
for ensuring reliable and efficient operation of the system.
Various faults are an integral part of memory management in modern operating systems and require careful
consideration to ensure system reliability and performance.