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.
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.
Figure 8.5:
Swapping of two processes using a disk as a backing store.
The quantum must be large enough to allow reasonable amounts of computing to be done between swaps.
A variant of this swapping policy is used for priority-based scheduling algorithms. This variant of swapping is sometimes called roll out, roll in.
Normally, a process that is swapped out will be swapped back into thesame 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.
Context-switch time; to get an idea of the context-switch time,
Let us assume that the user process is 10 MB in size and the backing store is a standard hard disk with a transfer rate of 40 MB persecond.
The actual transfer of the 10-MB process to or from main memory takes
10000 KB/40000 KB per second = 1/4 second
= 250 milliseconds.
Assuming that no head seeks are necessary, and assuming an average latency of 8 milliseconds, the swap time is 258 milliseconds.
Since we must both swap out and swap in, the total swap time is about 516 milliseconds.
For efficient CPU utilization, we want the execution time for each process to be long relative to the swap time. Thus, the time quantum should be substantially larger than 0.516 seconds.
Notice that the major part of the swap time is transfer time. Generally, swap space is allocated as a chunk of disk, separate from the file system, so that its use is as fast as possible.
Swapping is constrained by other factors as well. If we want to swap a process, we must be sure that it is completely idle.
Currently, standard swapping is used in few systems. A modification of swapping is used in many versions of UNIX.
Swapping is normally disabled but will start if many processes are running and are using a threshold amount of memory.
Swapping is again halted when the load on the system is reduced.