Does the Unix kill command ensure that dynamically allocated memory will return properly? Does the Unix kill command ensure that dynamically allocated memory will return properly? unix unix

Does the Unix kill command ensure that dynamically allocated memory will return properly?


The kill command sends a signal to a Unix process. That signal defaults to SIGTERM, which is a polite request for the program to exit.

When a process exits for any reason, the Unix OS does clean up its memory allocations, file handles and other resources. The only resources that do not get cleaned up are those that are supposed to be shared, like the contents of files and of shared memory (like System V IPC).

Many programs do not need to do any special cleanup on exit and use the default SIGTERM behavior, which is to let the OS stop the process.

If a program does need special behavior, it can install a signal handler, and it can then run a function to handle the signal.

Now the SIGKILL signal, which is number 9, is evil, but also necessary. This signal never gets to the process itself, the OS simple stops the process. This should only be used when really, really necessary. It often becomes necessary in multithreaded programs that get into deadlocks or programs that have installed a TERM signal handler, but screwed up during their exit process.


kill is a polite request for the program to end. It cleans up its memory, closes its handles and other such niceities. It sends a SIGTERM

kill -9 tells the operating system to grab the process by the balls and throw it the hell out of the bar. Obivously it is not concerned with niceities - although it does reclaim all the memory, as it's the Operating System's responsability to keep track of that. But because it's a forceful shutdown you may have problems when trying to run the program again (not cleaning up .pid files for example)

See also [wikipedia](http://en.wikipedia.org/wiki/Kill_(Unix)


Each process runs in its own protected address space, and when the process ends (whether it exits voluntarily or is killed by an external signal) that address space is fully reclaimed. So yes, all if its memory is released properly.

Depending on the process, it may or may not cause other problems next time you try to run it. For example, it may have some files open and leave them in an inconsistent state if it's killed unexpectedly. (The files will be closed automatically, but it could be in the middle of writing some application data, for example, and the files may contain incomplete/inconsistent data if interrupted.)

Typically when the system is shutting down, all processes will be sent signal 15 (SIGTERM), at which they can perform whatever cleanup/shutdown actions they need to do. Then a short time later, they'll get signal 9 (SIGKILL), which immediately kills them, without giving them any chance to react in any way. This gives all processes a chance to clean up for themselves, and then forcefully kills any processes that aren't responding promptly.