Search notes:

libc: signal handling

Checking if ctrl-c was pressed.

The signal handler that is associated with SIGINT is called when the user presses ctrl-c.
The following code snipplet establishes a signal handler that traps the pressing of ctrl-c and writes a corresponding message to stdout.
#include <signal.h>
#include <stdio.h>
#include <time.h>

void ctrl_c_pressed(int signum) {
 //
 // The user pressed ctrl-c. The for-loop
 // below stoped, the following message is printed
 // and the loop is resumed again.
 //
    printf("ctrl-c was pressed.\n");
}

int main() {

    struct timespec duration;
    duration.tv_sec  = 0;
    duration.tv_nsec = 1e6 * 100; // 100 milli Seconds

 //
 // Call ctrl_c_pressed when the user presses
 // ctrl-c:
 //
    signal(SIGINT, ctrl_c_pressed);

 //
 // Print the number 0 through 19 in intervals
 // of 100 milliseconds:
 //
    for (int i = 0; i<20; i++) {
        printf("i = %d\n", i);
        nanosleep(&duration, NULL);
    }

}
Github repository about-libc, path: /signal/SIGINT.c

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759382842, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/C/libc/signal/index(76): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78