views:

1408

answers:

7

So, 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 shut down the process this way? Does this insure that dynamically allocated memory will return properly? Are there any other negative effects? I've kinda operated under this, I guess I'd call it intuition, that this is kinda a last resort way of terminating a process.

Enlighten me please.

+4  A: 

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.

Vineet
+6  A: 

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

Tom Ritter
Haha, well said. +1
unforgiven3
Offensive? *sigh* I don't understand the SO audience. -9 is wrong in most places I've seen it, but your description sounds good.
Dustin
+2  A: 
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"
Frank Krueger
+2  A: 

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.

frankodwyer
A: 

It seems like if the program is written properly, it will handle the exit message and clean up properly.

I took this from wikipedia:

In Unix and Unix-like operating systems, kill is a command used to send simple messages to processes running on the system. By default, the message sent is the "termination" signal, which requests that the process exit. But kill is something of a misnomer; the signal sent may have nothing to do with process killing. The kill command is a wrapper around the kill() system call, which sends signals to processes or process groups on the system, referenced by their numeric process IDs (PIDs) or process group IDs (PGIDs). kill is always provided as a standalone utility, but most shells have built-in kill commands that may slightly differ from it.

There are many different signals that can be sent (see signal for a full list), although the signals in which users are generally most interested are SIGTERM and SIGKILL. The default signal sent is SIGTERM. Programs that handle this signal can do useful cleanup operations (such as saving configuration information to a file) before quitting. However, many programs do not implement a special handler for this signal, and so a default signal handler is called instead. Other times, even a process that has a special handler has gone awry in a way that prevents it from properly handling the signal.

All signals except for SIGKILL and SIGSTOP can be "intercepted" by the process, meaning that a special function can be called when the program receives those signals. The two exceptions SIGKILL and SIGSTOP are only seen by the host system's kernel, providing reliable ways of controlling the execution of processes. SIGKILL kills the process, and SIGSTOP pauses it until a SIGCONT is received.

Unix provides security mechanisms to prevent unauthorized users from killing other processes. Essentially, for a process to send a signal to another, the owner of the signaling process must be the same as the owner of the receiving process or be the superuser.

Jordan L. Walbesser
+15  A: 

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.

Zan Lynx
+1 for mentioning shared memory which the accepted answer does not
Stephen Darlington
+1. Would possibly be beneficial to state something about zombied procs.
Kent Fredric
I am not sure what you'd want to say about zombie procs. They can't be killed: they are already dead. They're just waiting for their parent process to acknowledge their exit status.
Zan Lynx
A: 

kill merely sends a signal to the process. The process can trap signals (except for signal 9) and run code to perform shutdown. An app's shutdown is supposed to be brief, but it may not be instantaneous.

In any case, once the process exits, the operating system will reclaim dynamically allocated memory, close open file descriptors, and other resources.

There could be some resources that survive, for example if the app held shared memory or sockets that are also held by other (still living) processes.

Bill Karwin