Re: signal, parent & children processes
- From: "Alex Vinokur" <alexvn@xxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 2 Dec 2005 18:13:40 +0200
"Alex Vinokur" <alexvn@xxxxxxxxxxxxxxxxxxxxx> wrote in message news:dmpmvj$oe7$1@xxxxxxxxxxxxxxxxxxxxx
>
> I did something like:
>
> /* --------------------- */
> void sigINT_handler1 (int sig_i)
> {
> fprintf (stderr, "sigINT_handler1: pid#%d ignores a signal %d\n", getpid(), sig_i);
> signal (SIGINT, sigINT_handler1);
> }
>
> void sigINT_handler2 (int sig_i)
> {
> fprintf (stderr, "sigINT_handler2: pid#%d ignores a signal %d\n", getpid(), sig_i);
> signal (SIGINT, sigINT_handler2);
> }
>
>
> int main()
> // fragments od code
> {
> sigINT_handler1 (SIGINT);
>
> if (fork() > 0) // Parent
> {
> // Stuff
> waitpid (pid, &status, 0);
> }
> else // Child
> {
> sigINT_handler2 (SIGINT);
> execvp (<args>);
> }
> }
>
> Parent catchs SIGINT via sigINT_handler1,
> but Child doesn't catch SIGINT via sigINT_handler2.
>
> How can Child catch SIGINT with its own signal handler?
>
[snip]
Actually, what I need is as it follows:
* Parent ignores SIGINT
* Background children ignore SIGINT
* Foreground children have default behavioor on SIGINT
Code below doesn't do that.
Is it possible to change to get desired result?
void sigINT_handler1 (int sig_i)
{
fprintf (stderr, "sigINT_handler1: pid#%d ignores a signal %d\n", getpid(), sig_i);
signal (SIGINT, sigINT_handler1);
}
void sigINT_handler2 (int sig_i)
{
fprintf (stderr, "sigINT_handler2: pid#%d ignores a signal %d\n", getpid(), sig_i);
signal (SIGINT, sigINT_handler2);
}
int main ()
{
while (1)
{
sigINT_handler1 (SIGINT);
read_command_line_and_foreground_flag();
pid = fork();
if (pid < 0)
{
perror ("Unable to execute fork");
continue;
}
if (pid > 0) /* Parent */
{
if (foreground_flag == 1)
{
// Foreground
waitpid (pid, &status, 0);
}
else
{
// Backround
}
}
else /* Child */
{
sigINT_handler2 (SIGINT);
execvp (<args>);
}
}
return 0;
}
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
.
- Follow-Ups:
- Re: signal, parent & children processes
- From: Alex Vinokur
- Re: signal, parent & children processes
- References:
- signal, parent & children processes
- From: Alex Vinokur
- signal, parent & children processes
- Prev by Date: Re: malloc/free question
- Next by Date: Re: execvp() and background process exit
- Previous by thread: signal, parent & children processes
- Next by thread: Re: signal, parent & children processes
- Index(es):
Relevant Pages
|