Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Fork bug in Sys5 (or similar) system
@ 1994-05-25 22:16 Guojun Jin[ITG]
  1994-05-26  7:52 ` Casper H.S. Dik
  1994-05-26 18:08 ` Doug Gwyn (ACISD/MCSB) <gwyn>
  0 siblings, 2 replies; 12+ messages in thread
From: Guojun Jin[ITG] @ 1994-05-25 22:16 UTC (permalink / raw)


In sys5 or similar system, the fork() will misplace a file position in a pointer
which opened by a parent and passed to children.  The bugs affects only fread().
fwrite() has not been tested yet, but similar peoblem should exist.

The tested systems are:

HP-UX A.09.01 A 9000/720
DEC OSF/1 V1.3A  (Rev. 112)
SunOS 5.4 ea1 sparc

IRIX 5.2 02282015 IP19 mips (fixed)


------------------------------ Cut Here ------------------------------
/* SV5 fork - fread bug . c
#
% Usages:
%	a.out [nrR] < fork-sv5.c
%		n or nothing = no fork and use gets to input
%		R = no fork and use fread to input
%		r = fork and use fread to input		!!! bug here
%		* all others = fork and use gets to input
%
% Author:	Jin Guojun - LBL
*/

#include <stdio.h>

main(argc, argv)
char	**argv;
{
char	buf[256];
FILE	*fp = stdin;

/*	first read in main()	*/
if (argc > 1 && *argv[1] == 'r')
	read_sbur(fp, buf, 0);
else	printf("start:\n%s", fgets(buf, 128, fp));

/*	second read in either parent or child	*/
if (argc > 1 && *argv[1] != 'n' && *argv[1] != 'R')	{
	switch (fork())	{
	case 0:	if (argc > 1 && *argv[1] == 'r')
			read_sbur(fp, buf, 1);
		else	subr(fp, buf);
		printf("done fork");
		break;
	case -1:printf("fork error\n");
		exit(-1);
	default:printf("FORK\n");
	}
} else	{	/* in parent	*/
	if (argc > 1 && *argv[1] == 'R')
		read_sbur(fp, buf, 1);
	else	subr(fp, buf);
	printf("end no fork\n");
}
exit(0);
}


read_sbur(fp, buf, more)
FILE *fp;
char* buf;
{
do {	int	i, j = fread(buf, 1, 80, fp);
	for (i=0; i<j; i++)	putchar(buf[i]);
} while (more && !feof(fp));
}

subr(fp, buf)
FILE *fp;
char *buf;
{
while (!feof(fp))
	printf("%s", fgets(buf, 128, fp));
}

------------------------------ Cut Here ------------------------------
--
/-------------- Jin Guojun ------------ v ---- Internet: g_jin@lbl.gov ----\
|	Imaging & Distributed Computing | Usenet: ucbvax!g_jin@lbl.gov	   |
|	Lawrence Berkeley Laboratory	| Bitnet:	--		   |
|	50B-2239, Berkeley, CA 94720	-  jin%george.lbl.gov@Csa3.LBL.Gov |


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-25 22:16 Fork bug in Sys5 (or similar) system Guojun Jin[ITG]
@ 1994-05-26  7:52 ` Casper H.S. Dik
  1994-05-26 18:16   ` William Armitage
  1994-05-26 18:08 ` Doug Gwyn (ACISD/MCSB) <gwyn>
  1 sibling, 1 reply; 12+ messages in thread
From: Casper H.S. Dik @ 1994-05-26  7:52 UTC (permalink / raw)


jin@eubie.lbl.gov (Guojun Jin[ITG]) writes:

>In sys5 or similar system, the fork() will misplace a file position in a pointer
>which opened by a parent and passed to children.  The bugs affects only fread().
>fwrite() has not been tested yet, but similar peoblem should exist.

You're not allowed to call exit(3) after fork(), unless you
flush all files before calling fork().

Casper


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-25 22:16 Fork bug in Sys5 (or similar) system Guojun Jin[ITG]
  1994-05-26  7:52 ` Casper H.S. Dik
@ 1994-05-26 18:08 ` Doug Gwyn (ACISD/MCSB) <gwyn>
  1994-05-31 19:34   ` Guojun Jin[ITG]
  1 sibling, 1 reply; 12+ messages in thread
From: Doug Gwyn (ACISD/MCSB) <gwyn> @ 1994-05-26 18:08 UTC (permalink / raw)


In article <2s0io0$248@overload.lbl.gov> jin@eubie.lbl.gov (Guojun Jin[ITG]) writes:
>In sys5 or similar system, the fork() will misplace a file position in a pointer
>which opened by a parent and passed to children.  The bugs affects only fread().
>fwrite() has not been tested yet, but similar peoblem should exist.

No, that's not a bug!  It's because you didn't realize that the standard
I/O library does read-ahead buffering (block buffering).  If you want to
share a file position indicator it *must* be done with a mechanism that
the operating system itself coordinates, e.g. the read() system call.
The standard I/O library maintains a "virtual" file image, some of which
(one bufferload) resides internally to the library (and thus the process
to which the instance of the library is linked) and some of which is
maintained externally by the operating system.  This mechanism is also
responsible for another kind of bug, where one process fwrite()s or
fprintf()s or putc()s to a file, but the data does not seem to be there
when another process tries to read it from that file.  (The data is still
in the writer's own buffer; it is flushed to the external file only under
certain special circumstances, not upon every putc() etc.)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-26  7:52 ` Casper H.S. Dik
@ 1994-05-26 18:16   ` William Armitage
  1994-05-27  8:48     ` Casper H.S. Dik
  1994-05-31  1:25     ` Peter da Silva
  0 siblings, 2 replies; 12+ messages in thread
From: William Armitage @ 1994-05-26 18:16 UTC (permalink / raw)


casper@fwi.uva.nl (Casper H.S. Dik) writes:

 > jin@eubie.lbl.gov (Guojun Jin[ITG]) writes:
 > 
 > >In sys5 or similar system, the fork() will misplace a file position in a pointer
 > >which opened by a parent and passed to children.  The bugs affects only fread().
 > >fwrite() has not been tested yet, but similar peoblem should exist.
 > 
 > You're not allowed to call exit(3) after fork(), unless you
 > flush all files before calling fork().
 > 
 > Casper
 > 

This seems to be a change introduced to support Multi-Threading.
I hit this the other day while getting the Solaris 2 port of the Berkeley
spooler going on my 2.3 machine. At startup the deamon forks a copy of
itself for each printer to process any existing print jobs.
The loop is basicaly -

	pfp = fopen("/etc/printcap", "r");

	while (fgets(buf, sizeof(buf), pfp)) {
		if (fork() == 0) {
			fclose(pfp);
			...
			exit();
		}
	}

lpd ran away forking printer processes. Trussing things showed that the
fclose was doing an lseek before closing. Putting an fflush before the
fork solved the problem.
I'd not seen this mentioned anywhere before i reread the fclose manual page.
It needs a mention in the solaris porting FAQ.

*** printcap.c.orig	Wed Sep  8 11:09:51 1993
--- printcap.c	Tue May 24 21:20:07 1994
***************
*** 29,34 ****
--- 29,41 ----
   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
+  *
+  * wja@cs.nott.ac.uk - Tue May 24 21:16:32 BST 1994
+  *
+  * New class of Solaris 2 porting problem. The 2.3 fclose will do a lseek
+  * on a file descripter if open for reading and not at end of file. This
+  * can be unfortunate in the presence of fork/fclose.
+  * Place fflush before returns from  getprent().
   */
  
  #ifndef lint
***************
*** 109,114 ****
--- 116,122 ----
  				continue;
  			}
  			*bp = '\0';
+ 			fflush(pfp);
  			return(1);
  		case '#':
  			if (bp == tbuf)
***************
*** 119,124 ****
--- 127,133 ----
  			if (bp >= tbuf+BUFSIZ) {
  				write(2, "Termcap entry too long\n", 23);
  				*bp = '\0';
+ 				fflush(pfp);
  				return(1);
  			}
  			*bp++ = c;


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-26 18:16   ` William Armitage
@ 1994-05-27  8:48     ` Casper H.S. Dik
  1994-05-27 12:59       ` Dave Johnson
  1994-05-31  1:25     ` Peter da Silva
  1 sibling, 1 reply; 12+ messages in thread
From: Casper H.S. Dik @ 1994-05-27  8:48 UTC (permalink / raw)


wja@cs.nott.ac.uk (William Armitage) writes:

>This seems to be a change introduced to support Multi-Threading.
>I hit this the other day while getting the Solaris 2 port of the Berkeley
>spooler going on my 2.3 machine. At startup the deamon forks a copy of
>itself for each printer to process any existing print jobs.


It's found in standard SysVr4 libc and doesn't appear to have anything
to do with MP support.  It has one nice feature:

	(head -2; head -2) < foo

now works as you'd (naively) expect.  (It prints the first 4 lines,
not the first 2 lines + 2 lines at offset blocksize)

Casper


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-27  8:48     ` Casper H.S. Dik
@ 1994-05-27 12:59       ` Dave Johnson
  1994-05-27 13:40         ` Casper H.S. Dik
  0 siblings, 1 reply; 12+ messages in thread
From: Dave Johnson @ 1994-05-27 12:59 UTC (permalink / raw)


casper@fwi.uva.nl (Casper H.S. Dik) writes:

>wja@cs.nott.ac.uk (William Armitage) writes:

>>This seems to be a change introduced to support Multi-Threading.
>>I hit this the other day while getting the Solaris 2 port of the Berkeley
>>spooler going on my 2.3 machine. At startup the deamon forks a copy of
>>itself for each printer to process any existing print jobs.


>It's found in standard SysVr4 libc and doesn't appear to have anything
>to do with MP support.  It has one nice feature:

>	(head -2; head -2) < foo

>now works as you'd (naively) expect.  (It prints the first 4 lines,
>not the first 2 lines + 2 lines at offset blocksize)

>Casper

In looking around the fork(2) manual pages on a couple SysV-ish
systems, I found the note quoted earlier (always call fflush(3)
on open file descriptors before doing fork) on some of them.
Other systems (DG/UX and OSF/1) didn't mention the problem at all.
Solaris 2.3 mentions the "other" problem with fork/exit, which is
that you must call _exit(2) (instead of exit(3)) after a failed
exec* call, but none of the others do.  The exit man pages just
say that _exit(2) will not perform the normal cleanup functions,
without saying why you might want to avoid them.

<Enter Old Fozzard mode>

I don't remember where I learned not to do exit() in a child that
hasn't exec'ed (maybe the BSTJ Nov '78 Unix paper?).  This sort of
lore should be clearly stated in the manual pages (under both fork AND
exit in this case).  Word-of-Net is nice, but you'd think after all
these years it would be officially noted.

<Exit Old Fozzard mode>

Just waiting for the coffee to kick in....

	Dave Johnson
	ddj@gradient.com


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-27 12:59       ` Dave Johnson
@ 1994-05-27 13:40         ` Casper H.S. Dik
  1994-05-28  2:51           ` Doug Gwyn (ACISD/MCSB) <gwyn>
  0 siblings, 1 reply; 12+ messages in thread
From: Casper H.S. Dik @ 1994-05-27 13:40 UTC (permalink / raw)


ddj@gradient.gradient.com (Dave Johnson) writes:

><Enter Old Fozzard mode>

>I don't remember where I learned not to do exit() in a child that
>hasn't exec'ed (maybe the BSTJ Nov '78 Unix paper?).  This sort of
>lore should be clearly stated in the manual pages (under both fork AND
>exit in this case).  Word-of-Net is nice, but you'd think after all
>these years it would be officially noted.

><Exit Old Fozzard mode>

I think you can find this in the old BSD vfork() manual pages,
and perhaps in the newer ones as well.

It says something to the effect that you should call _exit() after
vfork() and then goes on to note in brackets: Even with fork it is
wrong to call exit since buffered data would then be flushed twice.

This used to apply only to output buffers, as flushing input buffers
was something people didn't do.

The SVR4 stdio implementation does an
	lseek(fileno(buf), -unread characters in buf, SEEK_CUR).

if it maintained a proper idea of the current file position, it could
instead do:

	lseek(fileno(buf), offset where we're supposed to be , SEEK_SET).


However, this would require an lseek on each input buffer prior to
each read.  Though lseek isn't world's most expensive system call,
and its costs are dwarfed in comparison, it may still be something
you want to avoid.

I'm not 100% sure, but a simple fflush((FILE*) 0) is probably sufficient
prior to fork().

Casper


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-27 13:40         ` Casper H.S. Dik
@ 1994-05-28  2:51           ` Doug Gwyn (ACISD/MCSB) <gwyn>
  0 siblings, 0 replies; 12+ messages in thread
From: Doug Gwyn (ACISD/MCSB) <gwyn> @ 1994-05-28  2:51 UTC (permalink / raw)


In article <2s4t7i$6vl@mail.fwi.uva.nl> casper@fwi.uva.nl (Casper H.S. Dik) writes:
>I'm not 100% sure, but a simple fflush((FILE*) 0) is probably sufficient
>prior to fork().

Ooo, finally somebody found a use for the feature I lobbied for in X3J11!

Actually, exit() is seldom appropriate in this context; it not only does
stdio "cleanup", it also executes the atexit-registered functions, some
of which the programmer may not even realize were registered (by one of
the support libraries his application was linked with, for example).

The real heart of the matter (as I indicated in a previous post) is that
there is a fundamental difference between information that the "kernel"
coordinates and information private to the process.  In the good old
days we used to document the former in Section 2 of the UNIX Programmer's
Manual and the latter in Section 3.  POSIX.1 tried to blur the distinction
but had to reintroduce it in the guise of "underlying functions".  Since
fork() is a kernel action (aka "system call") the other operations
related to using it had better be left to the kernel to handle, i.e.
_exit() not exit(), or read() not fread().

I found the description of SVR4's attempt to unread a buffer rather
amusing, since obviously some UNIX file types simply cannot be unread.
For some reason this reminds me of the earlier UNIX System V stdio
attempts to make use of printf() etc. work reliably when invoked from
a signal handler.  In both cases, the job simply cannot be done right.
(With the ability to "hold" signals within a critical section, it is
now at least possible to get the latter right, and everyone implementing
"thread-safe" stdio has had to figure out how.)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-26 18:16   ` William Armitage
  1994-05-27  8:48     ` Casper H.S. Dik
@ 1994-05-31  1:25     ` Peter da Silva
  1 sibling, 0 replies; 12+ messages in thread
From: Peter da Silva @ 1994-05-31  1:25 UTC (permalink / raw)


Enter old-fogey mode.

In article <1994May26.181612.1484@cs.nott.ac.uk>,
William Armitage <wja@cs.nott.ac.uk> wrote:
> casper@fwi.uva.nl (Casper H.S. Dik) writes:
>  > You're not allowed to call exit(3) after fork(), unless you
>  > flush all files before calling fork().

> This seems to be a change introduced to support Multi-Threading.

No, it has *always* been true that you can't call any routine that might
execute fflush() after a fork() unless you know that all buffered output
streams were already fflush()ed before the fork().

This includes:

	printf, scanf, fread, fwrite, getc, getchar, fgetc, fgets,
	fprintf, fscanf, gets, puts, fputs, putc, fputc, exit, fclose,
	pclose, ...

I was aware of this in 1979, when the standard I/O library was itself pretty
new (in fact, you pulled it in explicitly with -lS), so it shouldn't be a
surprise any more.

> lpd ran away forking printer processes. Trussing things showed that the
> fclose was doing an lseek before closing. Putting an fflush before the
> fork solved the problem.

That's just a new *symptom* of an existing problem. Normally it's only output
file descriptors that trigger the bug.

I'm kinda surprised the routine didn't call close (normally you only call
open, close, pipe, dup, and other O/S-level routines between a fork and
exit/exec).
-- 
Peter da Silva                                            `-_-'
Network Management Technology Incorporated                 'U`
1601 Industrial Blvd.     Sugar Land, TX  77478  USA
+1 713 274 5180                       "Hast Du heute schon Deinen Wolf umarmt?"


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-26 18:08 ` Doug Gwyn (ACISD/MCSB) <gwyn>
@ 1994-05-31 19:34   ` Guojun Jin[ITG]
  1994-05-31 20:07     ` Casper H.S. Dik
  1994-06-01 22:06     ` Peter da Silva
  0 siblings, 2 replies; 12+ messages in thread
From: Guojun Jin[ITG] @ 1994-05-31 19:34 UTC (permalink / raw)


In article <1994May26.180828.3893@arl.army.mil>,
Doug Gwyn (ACISD/MCSB) <gwyn> <gwyn> wrote:
>In article <2s0io0$248@overload.lbl.gov> jin@eubie.lbl.gov (Guojun Jin[ITG]) writes:
>>In sys5 or similar system, the fork() will misplace a file position in a pointer
>>which opened by a parent and passed to children.  The bugs affects only fread().
>>fwrite() has not been tested yet, but similar peoblem should exist.
>
>No, that's not a bug!  It's because you didn't realize that the standard
>I/O library does read-ahead buffering (block buffering).  If you want to
>share a file position indicator it *must* be done with a mechanism that
>the operating system itself coordinates, e.g. the read() system call.
>The standard I/O library maintains a "virtual" file image, some of which
>(one bufferload) resides internally to the library (and thus the process
>to which the instance of the library is linked) and some of which is
>maintained externally by the operating system.  This mechanism is also
>responsible for another kind of bug, where one process fwrite()s or
>fprintf()s or putc()s to a file, but the data does not seem to be there
>when another process tries to read it from that file.  (The data is still
>in the writer's own buffer; it is flushed to the external file only under
>certain special circumstances, not upon every putc() etc.)

Actually, this is not true.  First of all, the BSD system has no such problem.
Secondly, the gets() system call does not have such problem either (see original
testing program).  
The fork() should duplicate the parent space to its child without any changes.
Then, the child(ren) can inherit all resources to continue the execution.
Of course, a patch can be applied to kludge fixing this problem (fflush) at not
cost too much in this case (may not be true in the larger data set case);
but it is still a bug.
In this testing program, doing fflush() on input is not meanful, except
re-postioning the file point postion (does this mean that system keep two
positions for one file ??? Very poor implementation).  If the input is a huge
data set (say 100MB image data), then, does fflush() intend to flush entire data
set into the system cache to get the file postion adjusted ?
BSD does it better, and SGI fixed its problem, Why not others?


--
/-------------- Jin Guojun ------------ v ---- Internet: g_jin@lbl.gov ----\
|	Imaging & Distributed Computing | Usenet: ucbvax!g_jin@lbl.gov	   |
|	Lawrence Berkeley Laboratory	| Bitnet:	--		   |
|	50B-2239, Berkeley, CA 94720	-  jin%george.lbl.gov@Csa3.LBL.Gov |


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-31 19:34   ` Guojun Jin[ITG]
@ 1994-05-31 20:07     ` Casper H.S. Dik
  1994-06-01 22:06     ` Peter da Silva
  1 sibling, 0 replies; 12+ messages in thread
From: Casper H.S. Dik @ 1994-05-31 20:07 UTC (permalink / raw)


jin@eubie.lbl.gov (Guojun Jin[ITG]) writes:

>In this testing program, doing fflush() on input is not meanful, except
>re-postioning the file point postion (does this mean that system keep two
>positions for one file ??? Very poor implementation).  If the input is a huge
>data set (say 100MB image data), then, does fflush() intend to flush entire data
>set into the system cache to get the file postion adjusted ?

Of course not.  It doesn't need to do much.

>BSD does it better, and SGI fixed its problem, Why not others?

Your test programs remain flawed because they call exit() in the child
of a fork().  That is wrong and has always been wrong.

Casper


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Fork bug in Sys5 (or similar) system
  1994-05-31 19:34   ` Guojun Jin[ITG]
  1994-05-31 20:07     ` Casper H.S. Dik
@ 1994-06-01 22:06     ` Peter da Silva
  1 sibling, 0 replies; 12+ messages in thread
From: Peter da Silva @ 1994-06-01 22:06 UTC (permalink / raw)


In article <2sg3h3$onq@overload.lbl.gov>,
Guojun Jin[ITG] <jin@eubie.lbl.gov> wrote:
> The fork() should duplicate the parent space to its child without any changes.

It does.

> In this testing program, doing fflush() on input is not meanful, except
> re-postioning the file point postion (does this mean that system keep two
> positions for one file ??? Very poor implementation).

The system keeps one, the standard I/O library keeps one. If you think about
it, that's the only way it could possibly be implemented.

Sun apparently synchronises these two pointers on close.
-- 
Peter da Silva                                            `-_-'
Network Management Technology Incorporated                 'U`
1601 Industrial Blvd.     Sugar Land, TX  77478  USA
+1 713 274 5180                       "Hast Du heute schon Deinen Wolf umarmt?"


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1994-06-01 22:06 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-05-25 22:16 Fork bug in Sys5 (or similar) system Guojun Jin[ITG]
1994-05-26  7:52 ` Casper H.S. Dik
1994-05-26 18:16   ` William Armitage
1994-05-27  8:48     ` Casper H.S. Dik
1994-05-27 12:59       ` Dave Johnson
1994-05-27 13:40         ` Casper H.S. Dik
1994-05-28  2:51           ` Doug Gwyn (ACISD/MCSB) <gwyn>
1994-05-31  1:25     ` Peter da Silva
1994-05-26 18:08 ` Doug Gwyn (ACISD/MCSB) <gwyn>
1994-05-31 19:34   ` Guojun Jin[ITG]
1994-05-31 20:07     ` Casper H.S. Dik
1994-06-01 22:06     ` Peter da Silva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox