510 likes | 596 Views
Net 321 : Computer Operating System. Lecture # 8 : Main Memory part 2. Chapter 8: Main Memory. BUT – what if the program is still too big ???.
E N D
Net 321 :Computer Operating System Lecture # 8 : Main Memory part 2 Networks and Communication Department
BUT – what if the program is still too big ??? • OR – if you are in a multiprogramming environment and can't get enough code of any program in main memory to run (when have pieces of all processes in memory) • One way to solve is SWAPPING (a memory management technique, not multiprogramming technique, used to change the mix of jobs)
Swapping • A process must be in memory to be executed. A process, however, can be swapped temporarily out of memory to a backing store (disk) and then brought back into memory for continued execution. • Use variants of CPU scheduling algorithms (RR, priority, etc.) to decide which processes swap out Example 1 : A round-robin CPU-scheduling algorithm; when a quantum expires (see Fig. 8.5), • The memory manager will start to swap out the process that just finished • and to swap another process into the memory space that has been freed. • In the meantime, the CPU scheduler will allocate a time slice to some other process in memory. • When each process finishes its quantum, it will be swapped with another process Example 2 : A variant of this swapping policy is used for priority-based scheduling algorithms. This variant of swapping is sometimes called roll out, roll in. • Lower-priority process is swapped out so higher-priority process can be loaded and executed
Swapping cont. • Normally, a process that is swapped out will be swapped back into the same memory space it occupied previously. • This restriction is dictated by the method of address binding. • If binding is done at assembly or load time, then the process cannot be easily moved to a different location. • If execution-time binding is being used, however, then a process can be swapped into a different memory space, because the physical addresses are computed during execution time. • Swapping is constrained by other factors as well. If we want to swap a process, we must be sure that it is completely idle.
1.Contiguous Allocation • The memory is usually divided into two partitions: • one for the resident OS • one for the user processes. • We usually want several user processes to reside in memory at the same time. We therefore need to consider how to allocate available memory to the processes that are in the input queue waiting to be brought into memory. • Input queue (job queue )– collection of processes on the disk that are waiting to be brought into memory to run the program • In this contiguous memory allocation, each process is contained in a single contiguous section of memory.
Contiguous Allocation (Cont.) 1. Equal-size partitions • One of the simplest methods for allocating memory is to divide memory into several fixed-sized partitions. Each partition may contain exactly one process. • Thus, the degree of multiprogramming is bound by the number of partitions(any process whose size is less than or equal to the partition size can be loaded into an available partition) • When a partition is free, a process is selected from the input queue and is loaded into the free partition. • When the process terminates, the partition becomes available for another process. • If all partitions are full, the operating system can swap a process out of a partition • A program may not fit in a partition. The programmer must design the program with overlays
Contiguous Allocation (Cont.) • Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation. • E.g: A program of size 2MB occupies an 8MB partition wasted space internal to a partition, as the data loaded is smaller than the partition size • This method is no longer in use.
Contiguous Allocation (Cont.) 2.Unequal-size partitions (also fixed) • Can assign each process to the smallest partition within which it will fit • Queue for each partition • Processes are assigned in such a way as to minimize wasted memory within a partition • In equal-size partitions because all partitions are of equal size, it does not matter which partition is used
Contiguous Allocation (Cont.) 3. Dynamic allocation • Initially, all memory is available for user processes and is considered one large block of available memory, a hole. • Hole – block of available memory; holes of various size are scattered throughout memory • When a process arrives and needs memory, we search for a hole large enough for this process. • If we find one, we allocate only as much memory as is needed, keeping the rest available to satisfy future requests • Partitions are of variable length and number • Eventually get holes in the memory. This is called external fragmentation • Operating system maintains information about:a) allocated partitions b) free partitions (hole)
Contiguous Allocation (Cont.) OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 10 process 2 process 2 process 2 process 2
Contiguous Allocation (Cont.) Dynamic Partitioning Placement Algorithm • Operating system must decide which free block to allocate to a process • Best-fit • Worst-fit • First-fit • Next-fit • First-fit and best-fit better than worst-fit in terms of speed and storage utilization
Best-fit algorithm • Allocate the smallest hole that is big enough; must search entire list • Produces the smallest leftover hole. • Chooses the block that is closest in size to the request • Worst performer overall • Since smallest block is found for process, the smallest amount of fragmentationisleft • Memory compaction must be done more often
Worst-fit algorithm • Allocate the largest hole; must also search entire list. • Produces the largest leftover hole. • Worst performer overall
First-fit algorithm • Allocate the first hole that is big enough • Scans memory form the beginning and chooses the first available block that is large enough • Fastest • May have many process loaded in the front end of memory that must be searched over when trying to find a free block
Next-fit algorithm • Scans memory from the location of the last placement • More often allocate a block of memory at the end of memory where the largest block is found
Last allocated block (14M) Worst Fit To allocate 16M Block
10K 20K 20K 20K 10K 10K 20K Example • The shaded areas are allocated blocks; the white areas are free blocks. • The next FOUR memory requests are 20K, 50K, 10K and 30K (loaded in that order). • Using the following placement algorithms, show the partition allocated for the requests. • First-fit • Best-fit • Next-fit • Worst-fit
10K 20K 20K 20K 10K 10K 20K Example: First-fit 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K
10K 20K 20K 20K 10K 10K 20K Example: First-fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K
10 10K 20K 20K 20K 10K 10K 20K Example: First-fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K
10 10K 20K 20K 20K 10K 10K 20K Example: First-fit 30 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K • Allocate for 30K
10K 20K 20K 20K 10K 10K 20K Example: Best-fit • 20K, 50K, 10K and 30K (in that order).
10K 20K 20K 20K 10K 10K 20K Example: Best-fit 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K
10K 20K 20K 20K 10K 10K 20K Example: Best-fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K
10 10K 20K 20K 20K 10K 10K 20K Example: Best-fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K
10 10K 20K 20K 20K 10K 10K 20K Example: Best-fit 30 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K • Allocate for 30K
10K 20K 20K 20K 10K 10K 20K Example: Next-fit • 20K, 50K, 10K and 30K (in that order). most recently added block
10K 20K 20K 20K 10K 10K 20K Example: Next-fit 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K most recently added block
10K 20K 20K 20K 10K 10K 20K Example: Next-fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K most recently added block
10 10K 20K 20K 20K 10K 10K 20K Example: Next-fit 50 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K most recently added block
10 10K 20K 20K 20K 10K 10K 20K Example: Next-fit 50 20 30 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K • Allocate for 30K most recently added block
10K 20K 20K 20K 10K 10K 20K Example: Worst fit • 20K, 50K, 10K and 30K (in that order).
10K 20K 20K 20K 10K 10K 20K Example: Worst fit 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K most recently added block
10K 20K 20K 20K 10K 10K 20K Example: Worst fit 50 20 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K most recently added block
10 10K 20K 20K 20K 10K 10K 20K Example: Worst fit 20 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K most recently added block
10 10K 20K 20K 20K 10K 10K 20K Example: Worst fit 20 30 50 • 20K, 50K, 10K and 30K (in that order). • Allocate for 20K • Allocate for 50K • Allocate for 10K • Allocate for 30K most recently added block
Fragmentation • External Fragmentation– total memory space exists to satisfy a request, but it is not contiguous • Equal sized partition and unequal sized partition • Internal Fragmentation– allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used • Dynamic allocation
Fragmentation cont. • Solutions : • One solution to the problem of external fragmentation is compaction. The goal is to shuffle the memory contents so as to place all free memory together in one large block. • The simplest compaction algorithm is to move all processes toward one end of memory; all holes move in the other direction, producing one large hole of available memory. This scheme can be expensive. • Another possible solution to the external-fragmentation problem is to permit the logical address space of the processes to be non-contiguous, thus allowing a process to be allocated physical memory wherever the latter is available.
5K 100K 100K 10K Extra Example • The shaded areas are allocated blocks; the white areas are free blocks. • The next FOUR memory requests are 20K, 5K, 35K and 50K (loaded in that order). • Using the following placement algorithms, show the partition allocated for the requests. • First-fit • Best-fit • Next-fit • Worst-fit most recently added block
Extra example (8.16 at textbook) • Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600 KB (in order), how would each of the first-fit, best-fit, and worst-fit algorithms place processes of 212 KB, 417 KB, 112 KB, and 426 KB (in order)?Which algorithm makes the most efficient use of memory?
Extra example (8.16 at textbook) Answer: • Let p1, p2, p3 & p4 are the names of the processes • First-fit: • P1>>> 100, 500, 200, 300, 600 • P2>>> 100, 288, 200, 300, 600 • P3>>> 100, 288, 200, 300, 183 • 100, 116, 200, 300, 183 <<<<< final set of hole • P4 (426K) must wait • Best-fit: • P1>>> 100, 500, 200, 300, 600 • P2>>> 100, 500, 200, 88, 600 • P3>>> 100, 83, 200, 88, 600 • P4>>> 100, 83, 88, 88, 600 • 100, 83, 88, 88, 174 <<<<< final set of hole
Extra example (8.16 at textbook) • Worst-fit: • P1>>> 100, 500, 200, 300, 600 • P2>>> 100, 500, 200, 300, 388 • P3>>> 100, 83, 200, 300, 388 • 100, 83, 200, 300, 276 <<<<< final set of hole • P4 (426K) must wait • In this example, Best-fit turns out to be the best because there is no wait processes.
FIXED PARTITIONS • Advantages: • Allows multiprogramming • Compared with single partition systems - • Less wasted space • Better memory utilization • Better processor and I/O utilization • Context Switching faster than Swapping • Disadvantages: • Internal Fragmentation (unused memory inside partitions) • Number of concurrent processes limited by number of partitions • Program limited to size of largest partition