From 59341b52696923e45164e8b436906c61fe34e774 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Wed, 25 Sep 2019 15:23:18 +0200 Subject: [PATCH] pidfd_open.2: Explain how pidfd_open() can be used to with fork() Signed-off-by: Michael Kerrisk --- man2/pidfd_open.2 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/man2/pidfd_open.2 b/man2/pidfd_open.2 index 9443f3baf..7adad55c3 100644 --- a/man2/pidfd_open.2 +++ b/man2/pidfd_open.2 @@ -91,6 +91,27 @@ is Linux specific. Currently, there is no glibc wrapper for this system call; call it using .BR syscall (2). .PP +The following code sequence can be used to obtain a file descriptor +for the child of +.BR fork (2): +.PP +.in +4n +.EX +pid = fork(); +if (pid > 0) { /* If parent */ + pidfd = pidfd_open(pid, 0); + ... +} +.EE +.in +.PP +Even if the child process has already terminated by the time of the +.BR pidfd_open () +call, the returned file descriptor +is guaranteed to refer to the child because +the parent has not yet waited on the child +(and therefore, the child's ID has not been recycled). +.PP The .BR pidfd_send_signal (2) system call can be used to send a signal to the process referred to by