Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to leave (interrupt) the waitpid() function?

Currently I am programming a shell and I use the waitpid() function for my children processes.

I also installed a signal handler so I can trap the SIGINT (CTRL+C) signal.

So what I want now is when someone presses the SIGINT (CTRL+C) signal it should leave the waitpid() functions and keep going as usually.

I am looking for a function who can help me about that.

like image 422
Nado Ba Avatar asked Jun 21 '26 20:06

Nado Ba


1 Answers

Install your signal handler without the SA_RESTART flag:

void handler( int sig, siginfo_t *si, void *arg )
{
    ...
}

...

struct sigaction newact;
memset( &newact, 0, sizeof( newact ) );
sigemptyset( &newact.sa_mask );
newact.sa_flags = SA_SIGINFO;  // note that **lack** of SA_RESTART
newact.sa_siginfo = handler;

sigaction( SIGINT, &newact, NULL );

You'll have to add error checking to that, and note that you will now have to handle interrupted calls in your code.

like image 65
Andrew Henle Avatar answered Jun 23 '26 11:06

Andrew Henle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!