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

I found a bunch of scripts in a project I recently assigned, these are shutdown scripts. They simply perform some basic searches and run the Unix kill command. Is there a reason they should not stop the process this way? Does this mean that dynamically allocated memory will return properly? Are there any other negative consequences? I worked under the intuition that this is the last way to terminate the process.

+4
source share
6 answers

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

When a process exits for any reason, Unix OS cleans up its memory allocations, files, and other resources. The only resources that are not cleared are those that need to be shared, such as the contents of files and shared memory (such as System V IPC).

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

If the program needs special behavior, it can install a signal handler, and then it can run a function to process the signal.

Now the SIGKILL signal, which is number 9, is evil, but also necessary. This signal never reaches the process itself, the OS just stops the process. This should be used when really, really necessary. This often becomes necessary in multithreaded programs that get into deadlocks or programs that have installed a TERM signal handler, but are screwed during their release.

+18
source

kill is a polite request to terminate a program. It clears memory, closes pens and other similar pleasant sensations. He sends SIGTERM

kill -9 tells the operating system to capture the process with balls and throw it out of the bar. Abundantly, he does not care about sympathies - although he restores all memory, since it is responsible for the operating system in order to track this. But due to a strong shutdown, you may encounter problems when you restart the program (for example, when cleaning .pid files)

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

+9
source

Each process starts in its own secure address space, and when the process terminates (regardless of whether it exits voluntarily or is killed by an external signal), the address space is completely fixed. So yes, all if his memory is released properly.

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

Usually, when the system shuts down, all processes will send a signal 15 (SIGTERM), in which they can perform any cleaning / shutdown actions that they need to perform. Then, after a short time, they will receive signal 9 (SIGKILL), which immediately kills them, giving them no chance to somehow react. This gives all processes the opportunity to cleanse themselves, and then forcibly kills any processes that do not respond promptly.

+5
source
 kill -9 

- last, not kill .

  • Yes, the memory is fixed (this is the responsibility of the OS)
  • Programs can respond to a signal, but they want a particular program to do the โ€œright thingโ€.
+2
source

kill will send a termination signal by default, which will allow the process to exit gracefully. If the process does not seem to work in a timely manner, some scripts will then return to kill -9, which causes the output to be "ready or not."

In all cases, operating systems controlled by the OS, such as dynamic memory, will be returned, files closed, etc. But the application level cannot be removed with -9 kill.

+2
source

kill simply sends a signal to the process. A process can capture signals (except signal 9) and run the code to shutdown. Shutting down the application should be brief, but it may not be instantaneous.

In any case, as soon as the process is completed, the operating system will return dynamically allocated memory, close open file descriptors and other resources.

There may be some resources that survive, for example, if the application stores shared memory or sockets, which are also stored in other (still living) processes.

0
source

All Articles