Skip to content

Instantly share code, notes, and snippets.

@atdt
Created July 20, 2013 09:17
Show Gist options
  • Save atdt/6044411 to your computer and use it in GitHub Desktop.
Save atdt/6044411 to your computer and use it in GitHub Desktop.
Get maximum pipe size. See F_GETPIPE_SZ in fcntl(2).
# -*- coding: utf-8 -*-
"""
Get maximum pipe size (F_GETPIPE_SZ)
See http://man7.org/linux/man-pages/man2/fcntl.2.html
"""
import fcntl
import os
F_GETPIPE_SZ = 1032 # Linux 2.6.35+
def get_max_pipe():
"""Get the maximum pipe buffer size or None."""
r, w = os.pipe()
try:
return fcntl.fcntl(w, F_GETPIPE_SZ)
except OSError:
return None
finally:
os.closerange(r, w)
@ae6rt
Copy link

ae6rt commented Dec 14, 2019

Thanks for posting this. The return value of the call to fcntl represents the capacity of the pipe, not the maximum size.

       F_GETPIPE_SZ (void; since Linux 2.6.35)
              Return (as the function result) the capacity of the pipe
              referred to by fd.

The maximum size can be learned by reading /proc/sys/fs/pipe-max-size

       F_SETPIPE_SZ (int; since Linux 2.6.35)
              Change the capacity of the pipe referred to by fd to be at
              least arg bytes.  An unprivileged process can adjust the pipe
              capacity to any value between the system page size and the
              limit defined in /proc/sys/fs/pipe-max-size (see proc(5)).
              Attempts to set the pipe capacity below the page size are
              silently rounded up to the page size.  Attempts by an unprivi‐
              leged process to set the pipe capacity above the limit in
              /proc/sys/fs/pipe-max-size yield the error EPERM; a privileged
              process (CAP_SYS_RESOURCE) can override the limit.

from http://man7.org/linux/man-pages/man2/fcntl.2.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment