0

I am running a process, which is creating a file and using that file. After the end of that process, i am deleting that file.

If some exception arises in between then how to know that the file is released by the process or not and how to delete it if it is locked by the process.

Thanks for any help :)

Deviprasad Das
  • 4,203
  • 7
  • 36
  • 51

2 Answers2

1

On Windows, when the process ends, the OS returns all resources owned by that process automatically. End of story.

For example, say you did "CreateFileMapping()" on each of two processes to share a memory segment. When the first process exits, Windows decrements the usage counter, and only the surviving process can still use the object. When the second process does a "CloseHandle()" (or itself exits), the counter is decremented to zero, and the object is completely freed.

In other words, Windows will reclaim the resource when it's no longer used, whether the processes clean up gracefully after themselves or not.

And no, even if you open a file for exclusive access, the file itself won't be "locked" once the file handle is closed (and Windows WILL close it automatically when the program terminates).

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

When your program ends - no matter by what means - the file, if it still exists, will no longer be locked by the process... because the process does not exist.

But I have to wonder; if the file is just being deleted anyway at the end, do you really need to create a file in the first place?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • I am creating the file with some content then using it and after using it i am deleting it. Then again on another request i am creating one more file. The file can't b shared, so if multiple users are using it then i need to create multiple file. – Deviprasad Das Dec 04 '10 at 07:57
  • 1
    What you describe sounds like something where you don't need a file at all. A file is usually for something that *does need to be shared* in some way. If the data is only needed for a single, short-lived context, memory is probably better (unless it's a very large amount of data). But the rest of my answer still applies anyway; Once the process ends, the file will no longer be locked. So just delete it either way. – Andrew Barber Dec 05 '10 at 00:46