A process needs to be in memory to be executed. A process, however, can be swapped temporarily out of memory to a backing store and then brought back into memory for continued execution.
Ideally, the memory manager can swap processes fast enough that there are always processes in memory ready to execute when the CPU scheduler wants to reschedule the CPU.
A variant of this swapping policy is used for priority based scheduling algorithms. If a higher priority process arrives and wants services, the memory manager can swap out the lower priority process so that it can load & execute the higher priority process. When the higher priority process finishes, the lower priority process can be swapped back in and continued. This variant of swapping is sometimes called roll out, roll in.
Normally a process that is swapped out will be swapped back into the same memory space that is occupied previously, but it is dictated by the method of address binding. If binding is done at assembly or run time, then the process cannot be moved to difference locations but if binding is done at execution time then it is possible to swap a process into a difference memory space because the physical address are computes during execution time.
Swapping requires a backing store. The backing store is commonly a fast disk. It must be large enough to accommodate copies of all memory images, for all users, and must provide direct access to these memory images.
The system maintains a ready queue consisting of all process whose memory images are on the backing store or memory, that are ready to run.
Whenever the CPU scheduler decides to execute a process, it calls the dispatcher. The dispatcher checks to see whether the next process in the queue is in memory. If the process is not, and there is no free memory region, the dispatcher swaps out a process currently in memory and swaps in the desired process. If then reloads registers as normal and transfers control to the selected process.
Notice that the major part of the swap time is transfer time. The total transfer time is directly proportional to the amount of memory swapped. If we have a computer system with 1 megabyte of main memory and a resident O.S. taking look, the maximum size of the user process is look
There are other constraints on swapping. If we want to swap a process, we must be sure that it is completely idle. If a process is waiting for an I/O (input/output) operation, we may want to swap that process to free up its memory. However, if the I/O asynchronously accessing the user memory for I/O buffers, then the process cannot be swapped.
Assume that the I/O operation was queued because the device was busy. Then, if we were to swap out process P1and swap in process P2, the I/O operation might then attempt to use memory that now belongs to process P2. The two main solutions to this problems are
(1)Never to swap a process with pending I/O, or
(2)To execute I/O operations only into operating system buffers.
Generally, swap space allocated as a separate chunk of disk separate from the file system, so that its use is as fast as possible.
Paging is the one of the possible solution to the external fragmentation. Paging avoids the considerable problem of getting the varying sized memory chunks onto the backing store, from which most of memory management schemes suffered.
Basic Method :
Physical memory is broken into fixed size blocks called frames. Logical memory is also broken into blocks of the same size called pages when a process is to be executed, its pages are loaded into any available memory frames from the backing store. The backing store is divided into fixed sized blocks that are of the same size as the memory frames. A smaller page size increase the overhead in terms of tables and table manipulation. A larger page leads to internal fragmentations.
Every address generated by the CPU is divided into two parts :
Page Number (p), and
Page offset (d).
Page number is used as an index into a page tables. Page table contains the base address of each page in physical memory. Physical memory address is defined by combination of page table and page offset.
The size of a page is typically a power of two varying between 512 bytes and 8192 bytes per page depending on computer architecture.
Paging itself is framer of dynamic relocation. Every logically address is bound by the paging hardware to some physical address. When we are paging scheme we have no external fragmentation. We may have some internal fragmentation.
If process size is independent of page size we expect internal fragmentation to average one half page per process. There is a quite a bit of overhead involved in each page table entry, and this overhead is reduced as the size of the page increases. Generally, page sizes have grown over time as process, data sets, and main, memory have become larger. When a process arrives in the system. Its size in pages is examined. Each page of the process needs one frame. It process requires n pages, there must be at lease n frames available in memory. The first page of the process is loaded into one of the allocated frames, and the frame no is put in the page table for this process.
The next page is loaded into another frame, this frame number also part into the page table and continuously this process. Since the O.S. is managing physical memory it must be aware of the allocation details of physical memory : which are allocated, which are available, what are the total frames.
If a user makes a system call and provides and address as a parameter, that address must be mapped to produce the correct physical address. The O.S. must map a logical address to a physical address, paging therefore increase the context switch time.