Created
September 11, 2015 01:53
-
-
Save cyfdecyf/1ee981611050202d670c to your computer and use it in GitHub Desktop.
Test get and set pipe/fifo buffer size.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipe-size: pipe-size.o | |
$(CC) $< -o $@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?