Are terminal writes atomic?

Chris Torek chris at mimsy.UUCP
Wed Oct 4 18:18:41 AEST 1989


In article <186 at promark.UUCP> mark at promark.UUCP (Mark J. DeFilippis) writes:
>How about we take this further.  The answer to this question is an
>undocumented feature of write() under Unix.  Several Authors note
>that write() is atomic under Unix and that it is undocumented.
>One is Marc J. Rochkind in his book _Advanced Unix Programming_,
>Prentice-Hall.  Writes are Atomic, no matter what you are writing to,
>even terminals.

Of course, it might be undocumented because it is false.

On Berkeley systems (4BSD, at least), writes to character devices are
not locked at the syscall level, and hence could be reentered if the
driver (cdevsw[].d_write) function sleeps.  Tty writes all go through
`ttwrite()', which has two ways of sleeping: if the tty output queue
is over the `high water' mark, or if the machine runs out of clist
blocks.

The sleep queues are priority-FIFO, so if two processes sleep for the
same reason, the first one wins when a wakeup() happens (since they are
sleeping at the same priority).  But if one sleeps on lbolt while
another is sleeping on outq/high-water-mark (if this condition is even
possible), they might wake up in the `wrong' order and intermix
output.

Anyway, I am not going to try to figure out whether it never happens,
just remark that it is at least theoretically possible.  `If it ain't
documented, don't trust it'.  (Of course even if it is documented,
you might do well not to trust it anyway.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.questions mailing list