How does SIGINT relate to the other termination signals such as SIGTERM, SIGQUIT and SIGKILL?

SIGTERM and SIGKILL are intended for general purpose “terminate this process” requests. SIGTERM (by default) and SIGKILL (always) will cause process termination. SIGTERM may be caught by the process (e.g. so that it can do its own cleanup if it wants to), or even ignored completely; but SIGKILL cannot be caught or ignored. SIGINT and … Read more

Writing programs to cope with I/O errors causing lost writes on Linux

fsync() returns -EIO if the kernel lost a write (Note: early part references older kernels; updated below to reflect modern kernels) It looks like async buffer write-out in end_buffer_async_write(…) failures set an -EIO flag on the failed dirty buffer page for the file: set_bit(AS_EIO, &page->mapping->flags); set_buffer_write_io_error(bh); clear_buffer_uptodate(bh); SetPageError(page); which is then detected by wait_on_page_writeback_range(…) as … Read more

What is the difference between sigaction and signal?

Use sigaction() unless you’ve got very compelling reasons not to do so. The signal() interface has antiquity (and hence availability) in its favour, and it is defined in the C standard. Nevertheless, it has a number of undesirable characteristics that sigaction() avoids – unless you use the flags explicitly added to sigaction() to allow it … Read more

How can I catch a ctrl-c event?

signal isn’t the most reliable way as it differs in implementations. I would recommend using sigaction. Tom’s code would now look like this : #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> void my_handler(int s){ printf(“Caught signal %d\n”,s); exit(1); } int main(int argc,char** argv) { struct sigaction sigIntHandler; sigIntHandler.sa_handler = my_handler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa_flags = 0; … Read more

What does “#define _GNU_SOURCE” imply?

Defining _GNU_SOURCE has nothing to do with license and everything to do with writing (non-)portable code. If you define _GNU_SOURCE, you will get: access to lots of nonstandard GNU/Linux extension functions access to traditional functions which were omitted from the POSIX standard (often for good reason, such as being replaced with better alternatives, or being … Read more