1

Are IO buffers stored during a context switch?

For example, if a process is receiving data from a network through IO buffers, and a context switch occurs, are those buffers stored along with program state?

If not, why not?

2
  • This seems like a homework question. If you could define what a context switch is and what it does, the question answers itself. OTOH in real life if you are dealing with context switches and have a problem that needs an answer to this question, then you are in a situation that is way over your head.
    – sawdust
    Jun 6, 2020 at 4:51
  • 1
    @sawdust it's a past exam question that I'm struggling with. From my understanding, a context switch happens so that an OS can run more than one process "simultaneously", by saving the current state of the registers, PC etc. so that it can begin where it left off. But I'm not exactly sure how this should handle streams of data through buffers: because it hasn't yet processed the data in those buffers, but more data will have arrived by the time the process resumes... it seems like you lose data either way? Jun 6, 2020 at 14:07

1 Answer 1

0

From my understanding, a context switch happens so that an OS can run more than one process "simultaneously", by saving the current state of the registers, PC etc. so that it can begin where it left off.

Okay, you seem to know the basic concept.
But where is this "current state" (of the processor) going to be saved?
The answer is "in memory".

BTW the "PC" (program counter) is also a register.
By your use of "registers" you seem to mean the general purpose registers.

Are IO buffers stored during a context switch?

Where are the I/O buffers?
The answer is "in memory".
Does it make sense to "save" data (i.e. the I/O buffers) that are already in memory (and not part of the processor or processor state) in another location in memory?
The answer is no.

If not, why not?

(This is an obvious hint that the correct answer is "no".)

The processor state is saved in order that it can be restored later, while the processor is used for some other task.
What is going to replace/clobber the I/O buffer?
The answer should be nothing.


For example, if a process is receiving data from a network through IO buffers, and a context switch occurs, are those buffers stored along with program state?

The use of context switch does not necessarily mean that an operating system is active.
For example a context switch can also be used to process an interrupt.
However in the situation that you stipulate, an OS (using its device drivers) will manage all I/O operations (with the exception of odd-ball userspace I/O).
This means that I/O buffers belong to the OS, and not to any process.
Incoming data is stored in system buffers under control of the OS (using either program I/O, aka PIO, or direct memory access, aka DMA).
Data can be received by a device regardless of which process is being executed by the CPU.

"Those buffers" are not part of any (userspace) "program", and therefore not "stored" with any "program state" under any circumstances.

But I'm not exactly sure how this should handle streams of data through buffers: because it hasn't yet processed the data in those buffers, but more data will have arrived by the time the process resumes... it seems like you lose data either way?

When a process makes a "read" system call to the OS, it is actually fetching data from an intermediate system buffer, rather than (directly) performing any I/O.
Depending on the device and the type of data, the received data can be copied several times from one buffer to another buffer (e.g. the network protocol stack) before a process can "read" it.

Since the OS manages all I/O on behalf of all processes, the OS strives not to lose any data.
The I/O buffers belong to the system, and are not part of any process context.
Each device driver maintains the state of its device (using data structures in memory), and manages the I/O that the device is performing.

Regardless, data already in memory are not part of the "processor state", and are not saved or "stored" during a context switch.

1
  • Really great and in-depth answer, thank you. This had cleared up my perception of buffers and I/O. Jun 8, 2020 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .