简体   繁体   中英

How to know what are various files opened by self process

In my c++ program I have various files open at a point where it does a fork() . After forking the program duplicates itself and run. The child process will share all opened file handles.

My question is, 'Is there a way to know what are various files that are open? such that I can close those file handles and reopen with a different file.'

Note: I am using linux machine.

It's possible to see what files are opened in a given process on linux by checking out /proc/<pid>/fd/* (or /proc/self/fd if you want the current process). Each file there is a sym link named for the corresponding fd, and the link points to the opened file, which you can find by using readlink() . You can redirect any particular fd using a call to dup2() to dup the new file's fd onto that fd.

That said, since you control all the processes, it may make more sense to just keep track internally. This is also far more portable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM