delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/12/02/12:36:45

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: "Alex Vinokur" <alexvn AT users DOT sourceforge DOT net>
Subject: Re: Signals, parent & children processes
Date: Fri, 2 Dec 2005 19:27:58 +0200
Lines: 93
Message-ID: <dmq085$g5j$1@sea.gmane.org>
References: <dmpslm$2sj$1 AT sea DOT gmane DOT org>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

"Alex Vinokur" <alexvn AT users DOT sourceforge DOT net> wrote in message news:dmpslm$2sj$1 AT sea DOT gmane DOT org...
>
> -----------------------
> CYGWIN_NT-5.0 1.5.18
> Windows 2000
> ----------------------
>
>
> /* --------------------- */
> 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 of 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?
>
>
It seems that code below does that..

// -----------------------------
int foreground_flag = 1; /* 1 - foreground, 0 - backgroung */

int main ()
{

  while (1)
  {
    signal (SIGINT, SIG_IGN);

    pid = fork();

    if (pid < 0)  /* Error while fork */
    {
      perror ("Unable to execute fork");
      continue;
    }

    if (pid > 0)  /* Parent */
    {
      if (foreground_flag == 1) waitpid (pid, &status, 0);
    }
    else  /* Child */
    {
      if (foreground_flag) signal (SIGINT, SIG_DFL);}
      else signal (SIGINT, SIG_IGN);

      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





--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019