Blocking I/O

One problem that might arise with read is what to do when there’s no data yet, but we’re not at end-of-file.

The default answer is ``we must go to sleep waiting for data.'' This section shows how a process is put to sleep, how it is awakened, and how an application can ask if there is data, without blocking within the read call. We’ll then apply the same concepts to write.

As usual, before I show you the real code, I’ll explain a few concepts.

Going to Sleep and Awakening

When a process is waiting for an event (be it input data, the termination of a child process, or whatever else) it should be put to sleep so another process can use the computational resources. You can put a process to sleep by calling one of the following functions:

void interruptible_sleep_on(struct wait_queue **q);
void sleep_on(struct wait_queue **q);

Processes are then awakened by one of:

void wake_up_interruptible(struct wait_queue **q);
void wake_up(struct wait_queue **q);

In the preceding functions, the wait_queue pointer-pointer is used to refer to an event; we’ll discuss it in detail later in Section 5.2.3. For now, it will suffice to say that processes are awakened using the same queue that put them to sleep. Thus, you’ll need one wait queue for each event that can block processes. If you manage four devices, you’ll need four wait queues for blocking-read and four for blocking-write. The preferred place to put such queues is the hardware data structure associated with each ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.