NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON | |
pidfd_open(2) System Calls Manualpidfd_open(2)pidfd_open - obtain a file descriptor that refers to a process
Standard C library (libc,-lc)
#include <sys/syscall.h>/* Definition ofSYS_*constants */#include <unistd.h>int syscall(SYS_pidfd_open, pid_tpid, unsigned intflags);Note: glibc provides no wrapper forpidfd_open(), necessitating the use ofsyscall(2).
Thepidfd_open() system call creates a file descriptor that refers to the process whose PID is specified inpid. The file descriptor is returned as the function result; the close-on-exec flag is set on the file descriptor. Theflags argument either has the value 0, or contains the following flag:PIDFD_NONBLOCK(since Linux 5.10) Return a nonblocking file descriptor. If the process referred to by the file descriptor has not yet terminated, then an attempt to wait on the file descriptor usingwaitid(2) will immediately return the errorEAGAINrather than blocking.
On success,pidfd_open() returns a file descriptor (a nonnegative integer). On error, -1 is returned anderrno is set to indicate the error.
EINVALflags is not valid.EINVALpid is not valid.EMFILEThe per-process limit on the number of open file descriptors has been reached (see the description ofRLIMIT_NOFILEingetrlimit(2)).ENFILEThe system-wide limit on the total number of open files has been reached.ENODEVThe anonymous inode filesystem is not available in this kernel.ENOMEMInsufficient kernel memory was available.ESRCHThe process specified bypid does not exist.
Linux.
Linux 5.3.
The following code sequence can be used to obtain a file descriptor for the child offork(2): pid = fork(); if (pid > 0) { /* If parent */ pidfd = pidfd_open(pid, 0); ... } Even if the child has already terminated by the time of thepidfd_open() call, its PID will not have been recycled and the returned file descriptor will refer to the resulting zombie process. Note, however, that this is guaranteed only if the following conditions hold true: • the disposition ofSIGCHLDhas not been explicitly set toSIG_IGN(seesigaction(2)); • theSA_NOCLDWAITflag was not specified while establishing a handler forSIGCHLDor while setting the disposition of that signal toSIG_DFL(seesigaction(2)); and • the zombie process was not reaped elsewhere in the program (e.g., either by an asynchronously executed signal handler or bywait(2) or similar in another thread). If any of these conditions does not hold, then the child process (along with a PID file descriptor that refers to it) should instead be created usingclone(2) with theCLONE_PIDFDflag.Use cases for PID file descriptors A PID file descriptor returned bypidfd_open() (or byclone(2) with theCLONE_PIDflag) can be used for the following purposes: • Thepidfd_send_signal(2) system call can be used to send a signal to the process referred to by a PID file descriptor. • A PID file descriptor can be monitored usingpoll(2),select(2), andepoll(7). When the process that it refers to terminates, these interfaces indicate the file descriptor as readable. Note, however, that in the current implementation, nothing can be read from the file descriptor (read(2) on the file descriptor fails with the errorEINVAL). • If the PID file descriptor refers to a child of the calling process, then it can be waited on usingwaitid(2). • Thepidfd_getfd(2) system call can be used to obtain a duplicate of a file descriptor of another process referred to by a PID file descriptor. • A PID file descriptor can be used as the argument ofsetns(2) in order to move into one or more of the same namespaces as the process referred to by the file descriptor. • A PID file descriptor can be used as the argument ofprocess_madvise(2) in order to provide advice on the memory usage patterns of the process referred to by the file descriptor. Thepidfd_open() system call is the preferred way of obtaining a PID file descriptor for an already existing process. The alternative is to obtain a file descriptor by opening a/proc/pid directory. However, the latter technique is possible only if theproc(5) filesystem is mounted; furthermore, the file descriptor obtained in this way isnot pollable and can't be waited on withwaitid(2). The program below opens a PID file descriptor for the process whose PID is specified as its command-line argument. It then usespoll(2) to monitor the file descriptor for process exit, as indicated by anEPOLLINevent.Program source #define _GNU_SOURCE #include <poll.h> #include <stdio.h> #include <stdlib.h> #include <sys/syscall.h> #include <sys/types.h> #include <unistd.h> static int pidfd_open(pid_t pid, unsigned int flags) { return syscall(SYS_pidfd_open, pid, flags); } int main(int argc, char *argv[]) { int pidfd, ready; struct pollfd pollfd; if (argc != 2) { fprintf(stderr, "Usage: %s <pid>\n", argv[0]); exit(EXIT_SUCCESS); } pidfd = pidfd_open(atoi(argv[1]), 0); if (pidfd == -1) { perror("pidfd_open"); exit(EXIT_FAILURE); } pollfd.fd = pidfd; pollfd.events = POLLIN; ready = poll(&pollfd, 1, -1); if (ready == -1) { perror("poll"); exit(EXIT_FAILURE); } printf("Events (%#x): POLLIN is %sset\n", pollfd.revents, (pollfd.revents & POLLIN) ? "" : "not "); close(pidfd); exit(EXIT_SUCCESS); }clone(2),kill(2),pidfd_getfd(2),pidfd_send_signal(2),poll(2),process_madvise(2),select(2),setns(2),waitid(2),epoll(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17pidfd_open(2)Pages that refer to this page:pgrep(1), clone(2), fanotify_init(2), pidfd_getfd(2), pidfd_send_signal(2), process_madvise(2), seccomp_unotify(2), setns(2), syscalls(2), wait(2), id_t(3type), sd_bus_creds_get_pid(3), sd_bus_creds_new_from_pid(3), sd_event_add_child(3), sd_pid_get_owner_uid(3), org.freedesktop.systemd1(5), fanotify(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |