3
#include <stdio.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <string.h>

void handler (int sig);
int count;

int main() {

    struct sigaction act;
    memset (&act, 0, sizeof (act));


    act.sa_handler = handler;
    if (sigaction (SIGHUP, &act, NULL) < 0) {
        perror ("sigaction");
        exit (-1);
    }

    int count = 0;
    while(1) {
        sleep(1);
        count++;
    }

}

void handler (int signal_number){
        printf ("count is %d\n", count);
}

I assume i am doing this right, how would I go about call sighup within the command line? It needs to call sighup to print out my count.

DDukesterman
  • 1,391
  • 5
  • 24
  • 42

3 Answers3

4

Technically it is not safe to do I/O in the signal handler, better to set a flag, watch for it, and print based on the flag. On a posix system, you should be able to "kill -HUP " from the command line to send the signal.

aet
  • 7,192
  • 3
  • 27
  • 25
  • Not only printf, all function do signal interrupts, malloc(), here is [List of authorized functions](http://man7.org/linux/man-pages/man7/signal.7.html) – Grijesh Chauhan Jul 18 '13 at 22:17
2

You can use kill -SIGHUP <pid>, where <pid> is the process ID of your code.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
1

try this in console:

ps -a

read out the pid of your program

kill -s HUP target_pid

Here you have a manual page for kill with a list of signals.

EDIT: even simpler you can use killall:

killall -HUP nameofyourbinary
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
nio
  • 5,141
  • 2
  • 24
  • 35