I have some code that gets file handles (and uses them) and allocates memory to the heap. If I were to press ctrl-c in the terminal that it is running in, and the code has not yet progressed to the part where it frees the memory and closes the handles before safely exiting, will they be still be freed, or do I need to create my own handler as shown in this post?
Asked
Active
Viewed 36 times
1 Answers
3
When you are executing as a user in a normal modern general-purpose multi-user operating system, the operating system will reclaim all resources when the program terminates regardless of the means by which the program terminates. That is a principal job of the operating system. It operates the system.

Eric Postpischil
- 195,579
- 13
- 168
- 312
-
Except deleting files, shared memory, or IPC constructs that the program might have deleted before exiting. – Jerry Jeremiah Aug 10 '23 at 01:23
-
2@JerryJeremiah: Anything the program deleted before exiting is gone. Reclaiming resources does not mean recovering deleted things. It means rescinding reservations on the resources that the program had. Or do you mean the operating system will not delete files that the program would have deleted if it had progressed through its normal execution? That is a different question from closing file handles and releasing allocated memory, which is what the question asks about. – Eric Postpischil Aug 10 '23 at 01:25
-
At least for the shared memory portion on the comment, I believe Jerry was talking about memory similar to SysV shared memory that is shared between processes and will persist on the system until removed regardless what happens with the creating process. – David C. Rankin Aug 10 '23 at 05:42