If you use or have access to different flavors of FreeBSD, DragonFlyBSD, NetBSD, OpenBSD, Mac OS X, Solaris, or some modern UNIX, I'd appreciate your help.
Some information is sorely lacking in documentation regarding pipe implementation details (except for in Linux), and I'd like to conduct a survey with your help.
Please download, compile, and run the following code:
Download pipesize.cLanguage: c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <errno.h>
#include <limits.h>
void output_system_id()
{
struct utsname n;
if (!uname(&n))
{
char *p = n.release;
unsigned long major, minor, patch;
major = minor = patch = 0;
major = strtoul(p, &p, 10);
if (*p == '.')
{
minor = strtoul(p+1, &p, 10);
if (*p == '.')
{
patch = strtoul(p+1, &p, 10);
}
}
printf("%s %lu.%lu.%lu %s\n", n.sysname, major, minor, patch, n.machine);
}
}
int main()
{
union
{
int pipefd[2];
struct
{
int read;
int write;
} command;
} u;
output_system_id();
printf("PIPE_BUF size: %d\nPipe size: ", PIPE_BUF);
fflush(stdout);
if (!pipe(u.pipefd))
{
int flags = fcntl(u.command.write, F_GETFL, 0);
if (flags != -1)
{
if (fcntl(u.command.write, F_SETFL, flags | O_NONBLOCK) != -1)
{
size_t amount;
for (amount = 0;; amount += sizeof(size_t))
{
ssize_t w = write(u.command.write, &amount, sizeof(size_t));
if (w != sizeof(size_t))
{
if (w == -1)
{
if (errno == EINTR) { amount -= sizeof(size_t); }
else
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { printf("%zu\n", amount); }
else { perror("Failed to write to pipe"); }
break;
}
}
else if (!w)
{
perror("Pipe unexpectedly closed");
break;
}
else
{
amount -= sizeof(size_t);
amount += w;
}
}
}
}
else { perror("Failed to set pipe nonblocking"); }
}
else { perror("Failed to get pipe flags"); }
close(u.command.read);
close(u.command.write);
}
else { perror("Failed to create pipe"); }
return(0);
}
Example of how to compile and run:
/tmp> gcc -Wall -o pipesize pipesize.c
/tmp> ./pipesize
Linux 2.6.32 x86_64
PIPE_BUF size: 4096
Pipe size: 65536
/tmp>
Please post your output here. Thanks.