sixstring
sixstring

Reputation: 31

Problem stopping a child loop using sigaction and intercepting Ctrl*C

I'm trying to write a program that intercepts Ctrl^C using sigaction, and then terminates the child of a fork.

Code:

static void usrHandler(int sig, siginfo_t * si, void * ignore) {
    printf("Interrupt Worked");
}

int main(int argc, char * argv[]) {
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sigemptyset( & sa.sa_mask);
    sa.sa_sigaction = usrHandler;
    sigaction(SIGINT, & sa, NULL);

    int currentFile = 1;
    int fd;
    int forkChild;

    forkChild = fork();
    if (forkChild == 0) {
        sleep(100);
    } else if (forkChild > 0) {
        sa.sa_sigaction = SIG_IGN;
        sigaction(SIGUSR1, & sa, NULL);
    }
}

I tried to remove all non necessary code for my example. For some reason I can not get the interrupt to work when I press Ctrl^C. Eventually I would like to be able to close the child and continue in the parent. Am I doing something wrong here?

Upvotes: 0

Views: 270

Answers (1)

Yunbin Liu
Yunbin Liu

Reputation: 1498

For some reason I can not get the interrupt to work when I press Ctrl^C.

Because your data in IO buffer, so change printf("Interrupt Worked"); to printf("Interrupt Worked\n"); (add \n), you will get data.

For IO buffer, see https://stackoverflow.com/a/53083985/7671328

Upvotes: 0

Related Questions