Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I found a bunch of scripts in the project I have been newly assigned to that are the "shutdown" scripts. They just do some basic searches and run the Unix kill command. Is there any reason they shouldn't shutdown the process this way? Does this ensure that dynamically allocated memory will return properly? Are there any other negative effects? I've operated under an intuition that this is a last resort way of terminating a process.

like image 779
Alex Avatar asked Dec 23 '08 20:12

Alex


5 Answers

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.

like image 189
Zan Lynx Avatar answered Nov 15 '22 12:11

Zan Lynx


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)

like image 22
Tom Ritter Avatar answered Nov 15 '22 13:11

Tom Ritter


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.

like image 44
Vineet Avatar answered Nov 15 '22 12:11

Vineet


kill -9

is the last resort, not kill.

  1. Yes memory is reclaimed (this is the OS's responsibility)
  2. The programs can respond to the signal however they want, it's up to the particular program to do "the right thing"
like image 22
Frank Krueger Avatar answered Nov 15 '22 13:11

Frank Krueger


kill by default will send a terminate signal which will allow the process to exit gracefully. If the process does not seem to exit in a timely fashion, some scripts will then fall back on kill -9 which forces an exit, 'ready or not'.

In all cases OS managed things such as dynamic memory will be returned, files closed etc. But application level things may not be tidied up on a -9 kill.

like image 35
frankodwyer Avatar answered Nov 15 '22 11:11

frankodwyer