The os.pidfd_open()
function creates a file descriptor that refers to the ID of a process specified as PID
an argument. The returned file descriptor can be used to perform process management without any external signal.
It also set
close-on-exec
flag on file descriptor. The set flag value means a child process does not inherit the file descriptor.
os.pidfd_open(pid, flags=0)
It takes the following argument values.
pid
: This is the process identifier.flags
: This is the flag argument that can have either 0
or PIDFD_NONBLOCK
flag. Its default is 0
.
flag= PIDFD_NONBLOCK
means os.pidfd_open() will return a non-blocking file descriptor. The non-blocking file descriptor means returning data quickly even if file reading is slow.
It returns either a positive integer or -1
.
-1
.from os import fork, pidfd_open # fork the current process pid = fork(); # if child process is in execution if pid > 0: pidfd = pidfd_open(pid, 0) print("Child Process") else: print("Parent Process")
fork
and pidpf_open
methods from the os
module.fork()
to make a copy of the current process.pid
is greater than 0
, it means the child process is under execution.pidfd_open(pid, 0)
is used to get the file descriptor for the current process."Child Process"
."Parent Process"
.