LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how a father process know which child process send the signal SIGCHLD (http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal-sigchld-266290/)

icoming 12-14-2004 10:48 AM

how a father process know which child process send the signal SIGCHLD
 
If a process create a lot of child processes.
When a child process ends, it will send a signal of SIGCHLD to its father process.
But how the father process know which of its child processes sends the signal?

itsme86 12-14-2004 11:01 AM

By catching SIGCHLD and then calling wait() in the signal handler.
Code:

RETURN VALUE
      The  process ID of the child which exited, or zero if WNO-
      HANG was used and no child was available, or -1  on  error
      (in which case errno is set to an appropriate value).

man wait

kamstrup 12-15-2004 03:16 AM

As far as I know there's no way to know who send some signal...

Calling wait() only waits until some signal is recieved. If you already recieved the signal, I think you are out of luck.

mayur 12-15-2004 04:34 AM

The wait3()/wait/waitpid function allows the calling process to obtain status information for specified child processes, stored in stat_loc.

See man of following call:
pid_t wait(int *stat_loc);
wait3(stat_loc, options, resource_usage); or
waitpid((pid_t)-1, stat_loc, options);

itsme86 12-15-2004 05:02 AM

Blah...here:
Code:

itsme@dreams:~/C$ cat wait.c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void handler(int sig)
{
  pid_t pid;

  pid = wait(NULL);

  printf("Pid %d exited.\n", pid);
}

int main(void)
{
  signal(SIGCHLD, handler);

  if(!fork())
  {
    printf("Child pid is %d\n", getpid());
    exit(0);
  }
  printf("Parent pid is %d\n", getpid());

  getchar();
  return 0;
}

Code:

itsme@dreams:~/C$ ./wait
Parent pid is 17559
Child pid is 17560
Pid 17560 exited.

It's really that simple.

icoming 12-16-2004 12:46 PM

thank you so much.
What I just want is to get pid of the exitting process,and to run waitpid() to end it.
These days I am programming a mini shell, and I meet a problem. When the shell runs a lot of commands once , and the commands will end in no order, how does the shell run wait() or waitpid() to deal with the zombie processes.
When the shell runs commands, it must be blocked. So I think it is available to run wait() when it receives a signal SIGCHLD.

mozala 06-01-2006 09:37 AM

Quote:

Originally Posted by itsme86
Blah...here:
Code:

itsme@dreams:~/C$ cat wait.c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void handler(int sig)
{
  pid_t pid;

  pid = wait(NULL);

  printf("Pid %d exited.\n", pid);
}

int main(void)
{
  signal(SIGCHLD, handler);

  if(!fork())
  {
    printf("Child pid is %d\n", getpid());
    exit(0);
  }
  printf("Parent pid is %d\n", getpid());

  getchar();
  return 0;
}

Code:

itsme@dreams:~/C$ ./wait
Parent pid is 17559
Child pid is 17560
Pid 17560 exited.

It's really that simple.

Hey Hommy!!
i got one question for u..

Code:

void handler(int sig)
{
  pid_t pid;

  pid = wait(NULL);

  printf("Pid %d exited.\n", pid);
}

handler seems to be having parameter "sig" but its not been used at all in the function...so whats that for?

bastl 06-02-2006 08:26 AM

Hi.
You only have to keep tracking for your child processes at creation time.
The value of SIGCHLD is the PID of any child process.
At creation of a child process the parent get the PID of that new child
and the child gets PID=0. Now you only have to store all your child PIDs in that creation order.
Or you can handle different process names (p1, p2 ...), that a new process gets a new name.

Hko 06-02-2006 10:08 AM

Quote:

Originally Posted by mozala
Hey Hommy!!
i got one question for u..

Who is "Hommy"?

Quote:

Originally Posted by mozala
handler seems to be having parameter "sig" but its not been used at all in the function...so whats that for?

The handler function is registered with the kernel so the kernel knows that it has to call that function when a signal arrives.

When the handler is called, it will pass the received signal number as a parameter (an int), so you'll always need to specify that parameter to be able to register it using signal(2), or (the newer) sigaction(2).

...whether you use it or not.

primo 06-03-2006 03:14 AM

There's the SA_SIGINFO extension to sigaction(2) too. You may even know if the signal was faked.

parthesh 07-20-2010 08:26 AM

For MUltiple Child one has to write
signal action again into handler


void handler(int sig)
{
pid_t pid;
pid = wait(NULL);
printf("Pid %d exited.\n", pid);
signal(SIGCHLD, handler);
return;
}


All times are GMT -5. The time now is 08:53 AM.