开发者

How to release all resources for a process?

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 exceptio开发者_运维技巧n 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 :)


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).


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜