1

Is there any way to get the number of process those are mapped to a particular file ?

Actually, I want to delete the file which is shared by multiple process when all processes are down. Is there any way to do that?

I tried with fstat function call to get the number of hard links but since after mapping I am closing the file so stat.st_nlink is always 0.

rajenpandit
  • 1,265
  • 1
  • 15
  • 21

2 Answers2

1

At first you need to create a pool of your fork. Then you can access to every file either hardlink,symb-link and so on via struct stat , pleae see man 2 stat.

Because you want to find out relative between PIDs and files you need to /proc you need to : /proc/PID/stat

I think the following project easy your job:

http://brokestream.com/procstat.html

After saving all of files of your process in a std::tuple, you can down your process and then delete your files.

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
1

Assuming that full path to the mmap()-ed file is /dir1/dir2/my_file, you could create a daemon, which executes lsof /dir1/dir2/my_file in a loop, until it finds that no process has my_file opened.

Assuming the applications are your, and you can change the code, I would advise another approach. Have one application that starts before others (can even spawn other processes), and have this application create and delete the file.

BЈовић
  • 62,405
  • 41
  • 173
  • 273