IO in Preemptive Multitasking

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
singerng
Posts: 21
Joined: Sun Jan 20, 2013 6:27 pm

IO in Preemptive Multitasking

Post by singerng »

How would a preemptive multitasking system use IO routines that are non-interruptable? (for example, HD reading and moving the VGA cursor)? I've found that when these routines get interrupted, chaos seems to ensue, and eventually a fault of some type occurs. How do modern OSes, like Linux and OS X do this?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: IO in Preemptive Multitasking

Post by Combuster »

HD reading/writing is not non-interruptable and you can pause dealing with the request if so needed. It is however non-reentrant so you can't start a transaction when another hasn't been completed yet. Also, DMA takes care of the atual transfer of data if you're careful actually leaving you time to do other things.

Similar concerns hold for the VGA. The registers in question can be written atomically and if you strictly need that the cursor never appears with the old X and the new Y coordinate, then you should check for things like vertical synchronisation before trying to be sure.

What remains is that the kernel can disable preemption and/or interrupts at its own leasure, so that the truly short I/O sequences and similar critical sections execute without the risk of being split by preemption.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: IO in Preemptive Multitasking

Post by turdus »

The most frequently used solution is daemons. Restrict the IO access to one process only, and queue requests from other processes. Things will never mix up that way, and you can prevent preemptive task switch while the daemon is in a critical section (only for daemons, other user processes should be preempted).
Typical example is cups, where only one process allowed to access the printer directly, yet many process can print.
rdos
Member
Member
Posts: 3276
Joined: Wed Oct 01, 2008 1:55 pm

Re: IO in Preemptive Multitasking

Post by rdos »

Generally, there are two types of solutions for these things:

1. Use semaphores to protect the IO device and let any thread do IO directly
2. Let a server (thread, daemon or process) do all IO, and queue up requests in some way.

I use both alternatives. When the device has IRQs, it is generally best to use a server thread, while in other cases any of the alternatives are possible.
Post Reply