[comp.unix.wizards] Tty queues

mcvoy@uwvax.UUCP (03/01/87)

Hi-
	I work on a system V machine at home sometimes and have a homebrew
sysline program hardwired for a h29 terminal.  Occassionally the output
gets mangled (ie., normal output gets mixed in with the sysline stuff).
The call to print the junk on the line looks like

    printf("%s%80.80s%s", TOSYS, string, TOSCRN);

where the manifests are the escape sequences to get to and back from the
status line.  The tty is running at 19.2K.

Well, we were thinking about this and wondering how this works.  The naive
view (ours) would have it that printf => write (on the whole string) and
write copies from user to kernel space, and then sends it to the tty.
It's not clear where the stuff gets mixed up.  We're guessing that the 
problem is in printf, that printf makes multiple calls to write() and
other stuff gets intermixed between those calls.  Is that right?  If
I made just one call to write with the whole string, would the problem
go away?  Or is it something like the copy getting interrupted by
new tty i/o (I hope not).  
-- 
Larry McVoy 	        mcvoy@rsch.wisc.edu, 
      		        {seismo, topaz, harvard, ihnp4}!uwvax!mcvoy

"It's a joke, son! I say, I say, a Joke!!"  --Foghorn Leghorn

henry@utzoo.UUCP (Henry Spencer) (03/05/87)

> It's not clear where the stuff gets mixed up.  We're guessing that the 
> problem is in printf, that printf makes multiple calls to write()...

Could be, depends on *exactly* what's been done to your printf.  Old ones
generally write a character at a time, most new ones try to do only one
write (but often have only finite buffering available).

> ...If I made just one call to write with the whole string, would the problem
> go away?  Or is it something like the copy getting interrupted by
> new tty i/o (I hope not).  

You're not going to like this, but in general there is *no* guarantee about
the order or interleaving of tty output coming simultaneously from multiple
sources.  None.  The system will *try* to complete a write as an atomic
entity, but there is only a finite amount of buffering between the write
and the device, and if there's no room, the write will have to wait.
If more than one write is waiting for buffer space, which one gets first
grab when more space appears is unpredictable.  That's my recollection,
anyway.  Much depends on details, which differ between Unix variants.
-- 
"We must choose: the stars or	Henry Spencer @ U of Toronto Zoology
the dust.  Which shall it be?"	{allegra,ihnp4,decvax,pyramid}!utzoo!henry