1

I've been working on my shell (using line noise) and fully implemented nearly all of its requirements. What remains is signal handling.

I'd like SIGINT to be forwarded to any running child processes (if any) and be ignored by the parent process completely (ie. the process running the actual shell will never terminate upon CTRL+C).

I've had a look at other posts regarding the issue and tried implementing solutions like setting process groups, but to no avail.

Here's what I did so far:

void handler() {
    printf("\n");
}

void acceptInput(){

    pid_t shell_pid = getpid();
    setpgid(0, 0);

    char* line;

    while((line = linenoise(">")) != NULL) {

        sighandler_t *old_handler = signal(SIGINT, &handler);

        parseInput(input); //later calls the appropriate commands to execute

    }

     linenoiseFree(input);
}

This solution terminates any child running upon CTRL+C, as it is meant to do. But it would also terminate the parent itself if there is no child process running. I have no idea how to have the parent ignore the SIGINT signal.

I'm very confused about this topic and appreciate any clarification.

drew181
  • 333
  • 1
  • 10
  • Have you looked at the possible duplicate [Catch Ctrl-C in C](https://stackoverflow.com/questions/4217037/catch-ctrl-c-in-c)? Also note, in practice you should not place output functions such as `printf` in your signal handler. Also check to the right of the page under the **Related** column for several other answered questions on the topic. – David C. Rankin May 18 '19 at 01:32

0 Answers0