Are terminal writes atomic?

Conor P. Cahill cpcahil at virtech.UUCP
Sun Oct 8 12:26:54 AEST 1989


In article <191 at promark.UUCP>, mark at promark.UUCP (Mark J. DeFilippis) writes:
> In article <19972 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
> > 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 this
> > 
> > Of course, it might be undocumented because it is false.
> > 
> > On Berkeley systems (4BSD, at least), writes to character devices are
> 
> Indeed I should have qualified my statement, however almost everyone is
> familiar with Rochkind's _Advanced Unix Programming_ book, and they are
> aware it is a SYSTEM V book, not a 4BSD Unix book.
> Under System V, it is a known undocumented item.  By undocumented, I mean
> it was left out of the documentation and under ALL true Unix System V systems,
> atomic writes at the system call level are guaranteed.

NO THIS IS NOT TRUE. Writes are not atomic.  To prove the point I threw the 
following example program together:

	#define BUFSIZE 2048
	main()
	{
		char	buffer[BUFSIZE+1];
		int	i;

		if( fork() == 0 )
		{
			for(i=0; i < BUFSIZE; i++)
				buffer[i] = '0';
			buffer[i++] = '\n';

			write(1,buffer,i);
			exit(0);
		}
		for(i=0; i < BUFSIZE; i++)
			buffer[i] = '1';
		buffer[i++] = '\n';

		write(1,buffer,i);
		exit(0);
	}

On my "true" system V (AT&T System V/386 Release 3.2) this resulted in 
mixed output.  I don't care what someones book says, the real thing says
that it is not atomic.

Try this on your system and see what you get.  

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.unix.questions mailing list