2

In my company I have seen use killall -9 to kill daemons and then start them again. (Which should really be killall -SIGKILL)

I am trying to explain to colleagues that this shouldn't really be the way we restart daemons. Maybe use the SIGINT symbol so that the process can shutdown correctly or use the init.d scripts to correctly restart the service.

We program in C so is there anything that could occur that would be detrimental to the system from doing a SIGKILL? Obviously this signal cannot be caught by the process so no internal signal handlers can do any nice shutdown processing, but

  • Would file read/write buffers be flushed correctly?
  • Would dynamically memory be reclaimed correctly?
  • Any other examples?

I could guess at the answers to these questions but does anyone know the answers? Can anyone flex their Unix knowledge to help me out? Armed with concrete knowledge I can put across a much better argument!

Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • 1
    When an app is killed, regardless of how, any memory it was allocated is freed up again. That's not to say that the app couldn't have triggered a memory leak in some OTHER service/process it was using at the time, but anything the process itself received via malloc() and the like will be released and reusable. – Marc B Feb 22 '12 at 15:45
  • @MarcB - "That's not to say that the app couldn't have triggered a memory leak in some OTHER service/process it was using at the time" - Pertinent. e.g. Process A 'initializes' a conversation with process B. Process B allocates memory for this conversation, and will only free it when Process A 'terminates' the conversation. Unfortunately, A gets `kill -9`ed and B never deallocates. Until ofcourse, B ends. – ArjunShankar Feb 22 '12 at 16:13

1 Answers1

2

a. Read/write buffers. Here is a simple experiment, that will hopefully 'prove' to your colleagues that abnormal termination is bad. Execute this perl script, and while it is sleeping, kill -9 it:

#!/usr/bin/perl
open FILE, ">file.txt" or die $!;
print FILE "This is it";
sleep 100;
close FILE;

On my machine, file.txt is empty if I kill before 'close' => looks like abnormal termination can mean loss of data. More on that at the accepted answer to this question: What happens if I don't call fclose() in a C program?

b. Dynamically allocated memory IS freed. This is basically a block of memory in the program's memory space called the 'heap'. The program requests the OS to adjust heap size depending on dynamic allocations, and the OS knows the limits of this block. Even on a clean exit of the program, if it has memory leaks, this memory is reclaimed.

Community
  • 1
  • 1
ArjunShankar
  • 23,020
  • 5
  • 61
  • 83