LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2004, 09:48 AM   #1
icoming
Member
 
Registered: Feb 2004
Posts: 96

Rep: Reputation: 15
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?
 
Old 12-14-2004, 10:01 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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
 
Old 12-15-2004, 02:16 AM   #3
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
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.
 
Old 12-15-2004, 03:34 AM   #4
mayur
LQ Newbie
 
Registered: Jun 2003
Posts: 14

Rep: Reputation: 0
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);
 
Old 12-15-2004, 04:02 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 12-16-2004, 11:46 AM   #6
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
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.
 
Old 06-01-2006, 08:37 AM   #7
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Rep: Reputation: 0
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?
 
Old 06-02-2006, 07:26 AM   #8
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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.
 
Old 06-02-2006, 09:08 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 06-03-2006, 02:14 AM   #10
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
There's the SA_SIGINFO extension to sigaction(2) too. You may even know if the signal was faked.
 
Old 07-20-2010, 07:26 AM   #11
parthesh
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
How to send CTRL-D to child process via a pipe? neo_in_matrix Programming 10 09-15-2005 07:09 PM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
child process -1 exited due to signal 6 ashfaq Linux - Software 1 04-09-2004 05:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration