DDukesterman
DDukesterman

Reputation: 1399

signal handler with sighup

#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.

Upvotes: 3

Views: 6573

Answers (3)

nio
nio

Reputation: 5289

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

Upvotes: 1

aet
aet

Reputation: 7292

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.

Upvotes: 4

Drew McGowen
Drew McGowen

Reputation: 11706

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

Upvotes: 2

Related Questions