Skip to content

Instantly share code, notes, and snippets.

@cyfdecyf

cyfdecyf/Makefile

Created Sep 11, 2015
Embed
What would you like to do?
Test get and set pipe/fifo buffer size.
pipe-size: pipe-size.o
$(CC) $< -o $@
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
// Must use Linux specific fcntl header.
#include </usr/include/linux/fcntl.h>
int main(int argc, char *argv[]) {
if (argc > 2) {
printf("Usage: %s [fifo]\n\n"
"Test get and set pipe buffer size\n",
argv[0]);
return 1;
}
int fd = 0; // Default to stdin, so we can test fcntl over pipe.
if (argc == 2) {
// The specified file should be a fifo.
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open failed");
return 1;
}
}
long pipe_size = (long)fcntl(fd, F_GETPIPE_SZ);
if (pipe_size == -1) {
perror("get pipe size failed.");
}
printf("default pipe size: %ld\n", pipe_size);
int ret = fcntl(fd, F_SETPIPE_SZ, 1024 * 1024);
if (ret < 0) {
perror("set pipe size failed.");
}
pipe_size = (long)fcntl(fd, F_GETPIPE_SZ);
if (pipe_size == -1) {
perror("get pipe size 2 failed.");
}
printf("new pipe size: %ld\n", pipe_size);
close(fd);
}
#!/bin/bash
echo "Max pipe size for unprivileged user:"
cat /proc/sys/fs/pipe-max-size
echo
echo "Test with pipe"
echo "testdata" | ./pipe-size
echo
echo "Test with fifo"
mkfifo fifo
./pipe-size fifo &
echo "testdata" > fifo
rm fifo
@yurenchen000

This comment has been minimized.

Copy link

@yurenchen000 yurenchen000 commented Mar 22, 2017

mark!

F_SETPIPE_SZ available since Linux 2.6.35.

http://stackoverflow.com/a/14371183/4896468

@czarina

This comment has been minimized.

Copy link

@czarina czarina commented Mar 29, 2017

Hi,

Thanks for posting this code! I'm running Debian Jessie on my Raspberry Pi and I'd like to set a higher buffer size for my FIFO pipes.

I've increased the max pipe buffer size:
sudo sysctl fs.pipe-max-size=4194304

Testing with a pipe works:

$ echo "testdata" | ./pipe-size
default pipe size: 65536
new pipe size: 1048576

Testing with a fifo produces no errors, but crashes my session:

echo "Test with fifo"
mkfifo fifo
./pipe-size fifo &
echo "testdata" > fifo
default pipe size: 65536
new pipe size: 1048576
Connection to raspberrypi.local closed.

Do you know why this would be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.