fd duplicate from python to child process
i think i have a problem with a ttyUSB device that caused from having 2 open ttyUSB fd's at the same time from different processes. it goes like this:
i have a main python process, which opens several ttyUSB fd's, read, write, close, and open processes (with popen) to handle each ttyUSB (of course after the fd was closed). when i do 'lsof | grep ttyUSB' it looks as if all the fd's that were opened in the main process when th开发者_如何学Goe child process started, associated to the child process even though they were already closed by the main process. (btw, the fd's are not associated to the main process)is that behavior normal? (tinycore, kernal 2.6.33.3), do i have a way to prevent it?
thanks.
By default, any file descriptors that a process has open when it forks/execs (which happens during a popen()
) are inherited by the child process. If this isn't what you want to happen, you will need to either manually close the file descriptors after forking, or set the fds as close-on-exec using fcntl(fd, F_SETFD, FD_CLOEXEC)
. (This makes the kernel automatically close the file descriptor when it execs the new process.)
精彩评论