5
$\begingroup$

I'm recently learning the part of I/O buffering of operating system and according to the book I use,

When a user process issues an I/O request, the OS assigns a buffer in the system portion of main memory to the operation.

I understand how this method is able to avoid the swapping problem in non-buffering situation. But is it assumed that the OS buffering created for the process will never be swapped out?

To extend my question, I was wondering if there is any mechanism where the kernel portion of an OS in memory may also be swapped?

$\endgroup$

2 Answers 2

6
$\begingroup$

Indeed, it wouldn't make sense to swap out an I/O buffer. The point of that buffer is that it's in RAM. The code to perform the I/O and the code to manage swap must not be swapped out either.

Other kernel code can be swapped out, in principle. However this can be problematic for several reasons. Hardware drivers that aren't involved in swap could be swapped out, but this means the kernel had to be very careful about the whole data party to swap (which may involve a disk over some external communication bus or even the network, as well as filesystem code when swapping to a file). Furthermore a lot of driver vice is very performance-sensitive: the kernel had to read it send data when the hardware is ready and may not be able to afford the time to bring vice and data back out of swap. In addition, RAM is usually assumed to be reliable (it's acceptable for the system to crash if the RAM fails), but swap (on disks or over the network) isn't, and it's difficult to design a kernel that can recover if part of it becomes unavailable. Most kernels take the simple approach and never swap out kernel code and data.

$\endgroup$
1
$\begingroup$

Usually pages that are used by the kernel are marked as "forced resident" or "swappable" or "cache".

Let's say the kernel uses some memory to cache the contents of a file or directory. The purpose is to avoid reading the file or directory again. Swapping this memory would be absolutely pointless. Just remember that it doesn't contain any cached data anymore. (Well, not 100% pointless for files on a very old and slow hard drive with swapping to a super fast SSD drive).

Your I/O buffers most likely must be in RAM while I/O happens, because the device performing the I/O will be set up to read from that RAM address, and cannot communicate with your OS at all. So if you swapped out the buffer, and allocated something else at that address, your hardware would happily keep reading/writing from that RAM address, creating absolute chaos.

Then there is everything that is needed to swap anything. For example the software that reads from the swap file. If that gets swapped out, you can't read it back in because you'd need to read it back in before you could use it to read it back in. Similar to leaving your car keys in your locked car. That kind of data would be "forced resident". And anything where some hardware thinks it must be in RAM would be "forced resident".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.