How not to open a file twice in linux?

后端 未结 3 1013
挽巷
挽巷 2021-01-21 18:24

I have a linked list with an fd and a string I used to open this file in each entry. I want to open and add files to this list only if this file is not already opened, because I

3条回答
  •  滥情空心
    2021-01-21 19:13

    When you successfully open a file use fstat on the file. Check to see if the st_ino and st_dev of the struct stat filed in by fstat have already been recorded in your linked list. If so then close the file descriptor and move on to the next file. Otherwise add the file descriptor, the file name and st_ino and st_dev values to the list.

    You can instead use stat to check before opening the file, but using fstat after will be slightly faster if the usual case is that file hasn't already been opened.

提交回复
热议问题