Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process...

38
Memory Management OS Fazal Rehman Shamil

description

swapping Process execution will again be resumed from the point it left CPU. This scheme is called swapping. More generally we can say swapping is removing of process from memory to secondary memory and again back to main memory.

Transcript of Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process...

Page 1: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Memory Management

OSFazal Rehman Shamil

Page 2: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

swapping

• Swapping concept comes in terms of process scheduling.

• Swapping is basically implemented by Medium term scheduler.

• Medium term scheduler removes process from CPU for duration and reduce the degree of multiprogramming. And after some time these process can again be reintroduced into main memory.

Page 3: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

swapping

• Process execution will again be resumed from the point it left CPU. This scheme is called swapping. More generally we can say swapping is removing of process from memory to secondary memory and again back to main memory.

Page 4: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• Example: Assume a multiprogramming environment with Round-Robin CPU scheduling algorithm.

• When a quantum expires, the memory manager will start to swap out the process that just finished, and swap in another piece to the memory space that has been freed.

Page 5: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• In the meantime, CPU scheduler will allocate a time slice to some other process in memory.

• When each process finishes its quantum it will be swapped back with another process.

Page 6: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• Swapping can be implemented in various ways.• For example, swapping can be priority based.

That means if a higher priority process arrives and wants service, the memory manager can swap out the lower priority process to the secondary memory so that it higher priority process can be loaded to main memory for execution.

Page 7: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• As soon higher priority process finishes, the lower priority process will be swapped back to main memory and execution will be continued.

• Sometimes swapping is also called roll out, roll in.

Page 8: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Page 9: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Contigious memory allocation• One of the simplest methods for memory allocation is to divide

memory into several fixed-sized partitions. • Each partition may contain exactly one process. • In this multiple-partition method, 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. • The operating system keeps a table indicating which parts of memory

are available and which are occupied.• Finally, when a process arrives and needs memory, a memory

section large enough for this process is provided.

Page 10: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Contigious memory allocation

• When a process needs to execute, memory is requested by the process. The size of the process is compared with the amount of contiguous main memory available to execute the process.

• If sufficient contiguous memory is found, the process is allocated memory to start its execution. Otherwise, it is added to a queue of waiting processes until sufficient free contiguous memory is available.

Page 11: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• this approach can suffer from external as well as internal memory fragmentation.

Page 12: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Fragmentation

• In computer storage, fragmentation is a phenomenon in which storage space is used inefficiently, reducing capacity or performance and often both. The exact consequences of fragmentation depend on the specific system of storage allocation in use and the particular form of fragmentation.

Page 13: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Fragmentation

• External fragmentation:• Total memory space is enough to satisfy a

request or to reside a process in it, but it is not contiguous so it can not be used.

Page 14: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Page 15: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Fragmentation

• Internal fragmentation:• Memory block assigned to process is bigger.

Some portion of memory is left unused as it can not be used by another process.

Page 16: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Task 2

• Write a program that takes three memory partitions and programmer itself decides the sizes for each memory allocation.

• The user input the size of three different process. • According to size of process, the program tells

that which process can allocate memory partition, and which process can’t allocate memory.

• Also program tells the total external fragmentation of the program.

Page 17: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Task 3

• You are allocated burst size for each process and to each group members,

• Now consider burst sizes as a resources, and check that whether a system is in safe state or not.

• Also apply banker’s algorithm to all and check that deadlock occurs or not.

Page 18: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Paging

• Both fix and variable partitions create problems of external and internal fragmentation.

• These are not better techniques that can be used for memory management.

• Paging is another memory management technique used to fix this problem.

Page 19: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• Using paging physical memory is divided into small equal fixed size blocks called frames.

• Each process is divided into blocks called pages that are of the same size as frames.

• A frame can hold one page of data.• Individual pages and frames are identified by

unique numbers known as page numbers and frame numbers respectively.

Page 20: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• When a process is to be executed, its pages are loaded into any available memory frames from the backing store.

• Page frames used for the process need not be contigious.

• In memory, smaller process require few frames, while large processes require more frames.

• Paging eliminates external fragmentation, but there may be some internal fragmentation.

Page 21: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• Backing store is also divided into fixed sized blocks that are of the same size as memory frames..

• Size of page and frames are determined by hardware.

• Size of page is typically from 512 bytes to 8kb, depending on the computer architecture.

Page 22: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Page table

• Os maintains a page table for each process.• Page table holds the frame location for each page of

process. • It contains one entry for each page of process.• When a process is entered into a system , it number of

pages is examined. If the process has ‘n’ pages, then ‘n’ frames must be available in memory.

• When process pages are allocated to the available frames in memory, then the frame numbers allocated to the pages are entered into the page table of the process.

Page 23: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Page 24: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Page 25: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Overlying

• Overlying is a technique that is used to execute a program even if the system has insufficient physical memory.

• Therefore to implement the overlying, the programmer splits the program into pieces or segments, called overlays, such that no two overlying segments need to be in memory at the same time.

Page 26: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Overlying

• Overlay 0 would start running first and when it is done, it would call another overlay to execute.

• Os can manage the program execution by swapping the overlay segments..

• Overlays are swapped in and swaped out by the OS

Page 27: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Segmentation

• Is a technique in which a user program can be divided into a number of smaller blocks, called segments.

• For example ,data segments or code segment for each process.

• Unlike paging, segment are having varying sizes and thus eliminates internal fragmentation. External fragmentation still exists but to lesser extent.

Page 28: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Segmentation

Page 29: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Segmentation

• Segmentation is a technique to break memory into logical pieces where each piece represents a group of related information.

• For example ,data segments or code segment for each process.

• Unlike paging, segment are having varying sizes and thus eliminates internal fragmentation. External fragmentation still exists but to lesser extent.

Page 30: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Virtual memory• Virtual memory is a technique that allows the execution

of processes which are not completely available in memory. The main visible advantage of this scheme is that programs can be larger than physical memory. Virtual memory is the separation of user logical memory from physical memory.

• This separation allows an extremely large virtual memory to be provided for programmers when only a smaller physical memory is available. Following are the situations, when entire program is not required to be loaded fully in main memory.

Page 31: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Virtual memory• If your computer lacks the random access memory (RAM) needed to

run a program or operation, Windows uses virtual memory to compensate.

• Virtual memory combines your computer’s RAM with temporary space on your hard disk. When RAM runs low, virtual memory moves data from RAM to a space called a paging file. Moving data to and from the paging file frees up RAM so your computer can complete its work.

• The more RAM your computer has, the faster your programs will generally run. If a lack of RAM is slowing your computer, you might be tempted to increase virtual memory to compensate. However, your computer can read data from RAM much more quickly than from a hard disk, so adding RAM is a better solution.

Page 32: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Virtual memory Advantages

• Main advantage of virtual memory is that it allows users to execute processes that are larger than actual size of physical memory.

• Os keeps necessary parts of process currently in use in main memory and the rest of the program on the backing store.

• Pieces of the program are swapped in and swapped out between memory and backing store during the program exexcution.

Page 33: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Virtual memory

• Most common methods that implement virtual memory are demand paging and demand segmentation.

Page 34: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Page 35: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

Demand paging• Used to implement the virtual memory.• This technique has combined features of simple paging and

overlying.• In a demand paged system, each page of the process is

stored contiguously in the paging swap space on 2ndary storage.

• With demand paging, a page is loaded into main memory only when it is needed. During process execution.

• As locations in pages are referenced, the pages are copied into memory page frames.

Page 36: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• A demand paging system is quite similar to a paging system with swapping. When we want to execute a process, we swap it into memory. Rather than swapping the entire process into memory, however, we use a lazy swapper called pager.

• When a process is to be swapped in, the pager guesses which pages will be used before the process is swapped out again. Instead of swapping in a whole process, the pager brings only those necessary pages into memory. Thus, it avoids reading into memory pages that will not be used in anyway, decreasing the swap time and the amount of physical memory needed.

Page 37: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• Hardware support is required to distinguish between those pages that are in memory and those pages that are on the disk using the valid-invalid bit scheme. Where valid and invalid pages can be checked by checking the bit.

Page 38: Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.

• 1 means page in memory• 0 means page not in memory