#include <unistd.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
	/* Write some initial amount of data */
	int remote_buf_size = (argc > 1) ? atoi(argv[1]) : 1;
	void *junk = calloc(remote_buf_size, 1);
	write(1, junk, remote_buf_size);
	free(junk);

	/* Write one byte, then read one byte */
	while (1)
	{
		char c;
		write(1, "", 1);
		read(0, &c, 1);
	}
}

/* Cross-couple two of these like this:
 *
 * $ mkfifo fifo
 * $ ./byte-in-the-pipe < <( ./byte-in-the-pipe < fifo ) > fifo
 *
 * They will run, always keeping at least remote_buf_size bytes
 * off in a kernel buffer somewhere, as long as remote_buf_size
 * is small enough.  Use this to detect how much data your kernel
 * will buffer for you.
 *
 * Observations:
 *
 * x86 FreeBSD 5.5 is live at  8191, deadlocks at  8192
 * x86 Linux 2.6   is live at 61440, deadlocks at 61441 !
 */

