[comp.sources.misc] v06i069: bug fix in pipes.c of byte benchmark

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (03/21/89)

Posting-number: Volume 6, Issue 69
Submitted-by: pb@idca.tds.PHILIPS.nl (Peter Brouwer)
Archive-name: pipes

A few month a received a set of sources of the second byte benchmark.
One of the tests is a pipe throughput test.
This source file contains a bug. 
The test was done as follows:

		open pipe;
		fork;
		if child ( read n times x bytes )
		if parent ( write n times x byte )

This may not always work. When reading from a pipe not the same number of bytes
may be received as is written on the other side in one write read pair.
So we changed the receive loop so that the total number of bytes to be received
is used in the receive loop.
Below is the modified source.

#  Peter Brouwer,                     ##
#  Philips TDS, Dept SSP-V2           ## voice +31 55 432523
#  P.O. Box 245                       ## UUCP address ..!mcvax!philapd!pb
#  7300 AE Apeldoorn, The Netherlands ## Internet pb@idca.tds.philips.nl
-----Cut Here-----Cut Here-----Cut Here-----Cut Here-----
: #!/bin/sh
: # shar:	Shell Archiver
: #	Run the following text with /bin/sh to create:
: #	pipes.c
echo shar extracting: pipes.c
sed 's/^X//' << '!FUNKY!FUNKY!STUFF!' > pipes.c
X/* File: pipes.c - created by Marty Leisner */
X/* leisner.Henr         1-Dec-87  8:55:04 */
X
X/* Copyright (C) 1987 by Martin Leisner. All rights reserved. */
X
X#ifndef	BLOCK_SIZE
X#define BLOCK_SIZE 	1000
X#endif	/* BLOCK_SIZE */
X
X#define NUM_BLOCKS	1000
Xchar buffer[BLOCK_SIZE];
X
Xmain()
X{
X	int pipefd[2];
X	register int i;
X	register int j;
X	register int k;
X	pipe(&pipefd);
X	
X	
X	
X	switch(fork()) {
X		case 0:
X			/* child code */
X			for(i = 0, j=BLOCK_SIZE*NUM_BLOCKS; j>0 ; i++) {
X				if((k = read(pipefd[0], &buffer, BLOCK_SIZE)) < 0)
X					break;
X				j -= k;
X			}
X			printf("child done, i = %d\n", i);
X			exit();
X			break;
X		case -1:
X			perror("fork broke");
X			exit();
X		default:
X			/* parent code */
X			for(i = 0; i < NUM_BLOCKS; i++)
X				if	(write(pipefd[1], &buffer, BLOCK_SIZE) != BLOCK_SIZE)
X					break;
X				
X			puts("parent done");
X			wait((char *) 0);
X			break;
X	}
X	return 0;
X}
X
!FUNKY!FUNKY!STUFF!
echo 'Orignal Sum -> 60843 2 pipes.c'
echo -n 'Current Sum -> '
sum pipes.c
exit 0
-- 
#  Peter Brouwer,                     ##
#  Philips TDS, Dept SSP-V2           ## voice +31 55 432523
#  P.O. Box 245                       ## UUCP address ..!mcvax!philapd!pb
#  7300 AE Apeldoorn, The Netherlands ## Internet pb@idca.tds.philips.nl


----- End Forwarded Message -----