Docsity

Operating System Process synchronization, Summaries of Computer science

Partial preview of the text

Download Operating System Process synchronization and more Summaries Computer science in PDF only on Docsity!

Memory Hierarchy Memory in a computer system is organized into different levels according to speed, cost, and capacity. A typical hierarchy is: CPU { Cache Memory J Primary Memory(RAM) J Secondary Storage(SSD / HDD) General rule As we move upward: Access speed increases Access time decreases Cost per bit increases Storage capacity decreases As we move downward: Access speed decreases Access time increases Cost per bit decreases Storage capacity increases 1. Cache Memory Cache is a small and very fast memory located close to/between the CPU and main memory. It stores frequently or recently used data and instructions so that the CPU can access them faster. Important The CPU generally checks the cache first. If the required data is not there (cache miss), it is obtained from a lower level of the memory hierarchy. 2. Primary Memory / Main Memory Primary memory mainly refers to RAM in this context. It stores the programs and data that are currently being used by the CPU. Characteristics Faster than secondary storage. Smaller capacity than secondary storage. More expensive per bit than secondary storage. Volatile โ€” RAM loses its contents when power is removed. 3. Secondary Storage Secondary storage is used for long-term/permanent storage of programs and data. Examples e SSD e HDD Characteristics Much larger capacity than RAM. Slower than RAM. Cheaper per bit. Non-volatile โ€” data remains stored even after power is turned off. Memory Allocation to Processes A process requires memory to store its program instructions, data, stack, heap, and other information while it is executing. The operating system allocates memory to each process. Memory allocation can be broadly classified into two types: 1. Static Memory Allocation In static allocation, the amount of memory required by a process is determined before execution and allocated in advance. Characteristics Memory size is decided before the program runs. The allocated memory size generally does not change during execution. Simple to manage. Can lead to unused memory if the process does not use all the allocated space. Example Suppose a process requires 100 MB: Main Memory Even if P1 actually uses only 70 MB, the remaining allocated space may remain unused. 2. Dynamic Memory Allocation In dynamic allocation, memory is allocated to a process during execution according to its requirements. A process can request additional memory when needed and release memory when it is no longer required. Characteristics Memory is allocated at runtime. Memory can be allocated as the process's requirements change. Makes better use of available memory. Requires more complex memory management by the OS. Example Process starts + Needs 5@ MB + 0S allocates 50 MB + Later needs 38 MB more 4 0S allocates additional memory Static vs Dynamic Allocation Static Allocation Dynamic Allocation Allocation occurs before/during program setup Allocation occurs during execution Size is generally fixed Size can change as required Simpler to manage More complex to manage May cause unused allocated memory Generally uses memory more flexibly Less flexible More flexible Memory Management Memory management is an important function of the operating system that manages main memory (RAM) and allocates it efficiently among processes. Main Goal | pa | | | There are 4 processes in memory. Therefore: Degree of multiprogramming = 4 Why is it important? The OS tries to keep an appropriate number of processes in memory so that the CPU has another process to execute when one process is waiting for I/O. P1 โ€” CPU = I/O wait J P2 โ€” CPU This helps improve CPU utilization and throughput. Important point A higher degree of multiprogramming does NOT always mean better performance. If too many processes are kept in memory: Memory becomes crowded. More overhead is created. Processes may spend more time waiting. Excessive paging can lead to thrashing. So the OS tries to maintain a suitable degree of multiprogramming. Memory Management Strategies Memory management strategies determine how the operating system manages data between secondary storage and main memory (RAM). They are broadly divided into three categories: 1. Fetch Strategies Fetch strategies determine when and what data should be brought from secondary storage into main memory. Types e Demand Fetching โ€” Data is loaded when it is actually needed/requested. e Anticipatory (Prefetch) Fetching โ€” Data is loaded in advance, based on the expectation that it will be needed soon. 2. Placement Strategies Placement strategies determine where in main memory a program/data should be placed. This is particularly important when there are multiple free areas (holes) in memory. Common placement strategies include: e First Fit โ€” Place the data in the first suitable free block. e Best Fit โ€” Place it in the smallest suitable free block. e Worst Fit โ€” Place it in the largest available free block. 3. Replacement Strategies Replacement strategies determine which data/page should be removed from main memory when space is required for new data. This is mainly encountered in virtual memory/page replacement. Common replacement algorithms include: e FIFO โ€” Remove the page that has been in memory the longest. e LRU โ€” Remove the page that has not been used for the longest time. e Optimal โ€” Remove the page that will not be used for the longest time in the future. agement iques v (Gea) a VetiablePartiion ) | Muttlevel Paging _) Inverted Paging _ | | Segmantation โ€”| segmented Pacing | Advantages Simple to implement Low OS overhead Disadvantages Internal fragmentation: a process may not use the entire partition allocated to it. Limited number of processes: maximum number of processes in memory is limited by the number of partitions. Can be inefficient if partitions are poorly sized. Dynamic Partitioning Dynamic partitioning is a contiguous memory allocation technique in which partitions are created dynamically according to the size of the process when the process is loaded. Working : 1. 2 In dynamic partitioning, the memory is not divided into fixed partitions at the beginning. Initially, after the OS area, the remaining memory is essentially one large free space. When a process arrives, the OS creates a partition according to that process's size. When another process arrives, another partition is created from the remaining free space. Example: Initially: | Free Memory | โ€”โ€”โ€”โ€” P1 needs 200 MB: ยฉ 200 MB partition created 1 Free Memory | aay, Then P2 needs 100 MB: | Free Memory | ee Advantages e Better memory utilization than fixed partitioning. e Little/no internal fragmentation because partition size is based on process size. e Number and size of partitions are not fixed beforehand. Disadvantages e Causes external fragmentation (many small free holes). e More complex than fixed partitioning. e May require compaction to combine small free holes. Placement Algorithms There are two types: 1. Internal Fragmentation Memory is allocated in a block larger than the process actually needs, so some space inside the allocated block remains unused. Example: Partition = 100 MB Process 82 MB rn | | Process = 80 MB | โ€”S | Unused - 28 MB | + Internal fragmentation โ€”โ€”โ€” _โ€”_______)} Internal = wasted space INSIDE an allocated partiti Commonly associated with fixed partitioning. โ€”> can be reduced by efficiently assigning the smallest partition to a process which is large enough for that process that is using best fit. External Fragmentation External fragmentation occurs when free memory is available but is divided into small, non-contiguous holes, so a process requiring a large contiguous block cannot be allocated. It commonly occurs in dynamic partitioning when processes are loaded and removed, leaving small free holes between allocated processes. Example se main memory is 10 MB. A.A MB process PI is loaded: โ€˜Then a3 MB process P2 is loaded: [ri eme | rz ame) 3B | This is external fragmentation. Now P1 finishes, so its 4 MB becomes free: [4m | FREE Now: * Total free memory = 4 + 3 = 7MB * Anew pr requires 5 MB * But there is no single contiguous 5 MB block Therefore, the proc nnot be allocated, even though 7 MB is free. How can it be reduced? Compaction can be used to combine scattered free spaces into one large contiguous block. Geo meek Kemi \efne cowrbac han Belen ere A be โ€œompachen "sat SM. piIE Se Beye (shaded means free memory) Swapping Swapping is a memory-management technique in which a process is temporarily moved from main memory (RAM) to secondary storage (swap space) to free RAM, and later brought back into RAM when it is needed. Working Suppose RAM is full: Process | rr | | Part 1 | โ€” Ram | Part 2 | โ€” Secondary Storage | Part 3 | โ€” Ram | Part 4 | โ€” Secondary Storage Lt The OS moves required parts between RAM and secondary storage as needed. Why Virtual Memory? Virtual memory gives a process the illusion of having a large memory space, even when the available physical RAM is smaller. For example: A program may require 8 GB of memory, while the computer has only 4 GB RAM. With virtual memory, the OS can keep only the currently needed parts in RAM and keep the remaining parts on secondary storage. Paging Paging is a non-contiguous memory-management technique in which a process is divided into fixed-size blocks called pages, and main memory is divided into fixed-size blocks called frames. Basic Concept Process โ€” Pages Main Memory (RAM) โ€” Frames Page size = Frame size Pages of a process can be placed in any available frames, so they do not need to be stored contiguously. Advantages Eliminates external fragmentation. Allows a process to be stored non-contiguously therefore No need for one large contiguous block of free memory and hence makes memory allocation more flexible. e Swapping becomes easy to equal size of frame and page Disadvantages e Paging causes internal fragmentation because memory is allocated in fixed-size pages/frames. If the last page of a process does not completely fill a frame, the remaining unused space inside that frame is wasted as internal fragmentation. e Address Translation is slow as main memory is accessed two times(one for page table and one for actual access of the page in main memory). A considerable amount of space is wasted in storing page table. Translation process is difficult and complex to understand. Virtual Memory Virtual Memory is a memory-management technique that allows a process to execute even when the entire process is not present in main memory (RAM). Main Idea e Only the required parts/pages of a program are kept in RAM. e The remaining pages are kept in secondary storage (disk). e When a required page is not in RAM, the OS brings it from disk into RAM. Example Suppose: Program size = 16 GB RAM available = 4 GB The program can still execute because only the required pages need to be in RAM at a time. Suppose the program is Virtual Memory But RAM has fewer frames: Physical Memory (RAM) I | Frame o | [| Frame a | | Frame 2 | | Fras j Only some pages are currently in RAM. The page table / memory map keeps track of: In demand paging, each page-table entry contains an additional valid-invalid bit. Bit Meaning Valid (1) Page is currently present in main memory Invalid (0) Page is not currently present in main memory Page fault and its handling : When the CPU references a page whose valid-invalid bit is invalid, the page is not present in RAM. This condition is called a page fault. Handling : 1) 2) 3) 4) 5) 6) 7) 8) When a page fault occurs,the control is transferred from cpu to the operating system. The O.S. first looks at the page table. If its an invalid reference then the call is aborted otherwise page is loaded from the main memory. The operating system locates the required page on secondary storage. The OS looks for a free frame in main memory. If no free frame is available, a page replacement algorithm is used to select a page to remove. The required page is transferred from secondary storage into the selected frame. The OS updates the page table with frame number and changes the valid invalid bit to 1. The interrupted instruction is restarted, and the process continues execution. โ€” ) page ison Decking store โ€˜operating system reference @ load M * fi ยฉ restart | page table instruction) free frame 7 eS ยฉ @ reset page bring in table missing page physical 01 Segmentation Segmentation is a non-contiguous memory-management technique in which a process is divided into variable-sized logical units called segments. Unlike paging, where the process is divided into equal-sized pages, segmentation divides a program according to its logical structure. Examples of Segments A process may contain: Code segment โ€” program instructions Data segment โ€” global/static variables Stack segment โ€” function calls and local variables Heap segment โ€” dynamically allocated memory Process a | Code | โ€” Segment 0 | | Data | โ€” Segment 1