Sigset: ignoring ctrl-c on Unix

I am trying to get my program to ignore Ctrl+ Con unix, which seems to work, the problem is that it continues to write "Syntax error". Here is the code

extern "C" void ignore( int sig )
{            
    fprintf( stderr, "\n"); // Print a new line
    // This function does nothing except ignore ctrl-c
}

int main()
{           
    // For ctrl-c
    sigset( SIGINT, ignore );

    while (1) {
        getUserInput();
    }      

    return 0;
}

Each time I press Ctrl+ C, it starts again via getUserInput, which is the expected behavior, but it also writes โ€œSyntax errorโ€. I checked, and the ignore function starts, and as soon as it is executed, then it prints an error message, I'm not sure why.

Does anyone have any clues?

Many thanks,

Jary

+3
source share
4 answers

sigset(). POSIX 2008, , .

signal(), ISO C, , sigaction(), POSIX.

- , , - , (, SIGCHLD ).

, , signal():

if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    signal(SIGINT, ignore);

, sigaction():

struct sigaction new_sa;
struct sigaction old_sa;
sigfillset(&new_sa.sa_mask);
new_sa.sa_handler = SIG_IGN;
new_sa.sa_flags = 0;

if (sigaction(SIGINT, &new_sa, &old_sa) == 0 && old_sa.sa_handler != SIG_IGN)
{
    new_sa.sa_handler = ignore;
    sigaction(SIGINT, &new_sa, 0);
}

getUserInput(), , " ". , , , , , .

+7

(: , , signal() ( C) sigaction() (POSIX) sigset(), , .)

; - , , .

. , read EINTR. , , - getUserInput() .

, SIG_IGN, sigset(), signal() sigaction().

+5

" " getUserInput ( -, , ) - , , , .

, SIG_IGN . sigset(SIGINT, SIG_IGN)

+1

ignore() , .

UNIX, SIGINT , getUserInput(). a SIGINT , getUserInput() stderr, undefined, fprintf() . ( , , , / . " " SIGINT?) CERT GNU libc .

Win32, SIGINT .

+1

Source: https://habr.com/ru/post/1763683/


All Articles