LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 06-24-2004, 01:06 PM   #1
iftiuk
Member
 
Registered: Apr 2004
Location: Washington DC
Posts: 43

Rep: Reputation: 15
Question Parent child processes signal sharing


I have a process that is the session leader, it forks and execs a child process. The child process calls setsid(). Now when I kill (SIGKILL) the parent process, the child process gets killed too. How may I avoid the killing of child process when parent process gets killed
 
Old 06-24-2004, 01:31 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I'm not sure if this is helpful to you: http://www.delorie.com/gnu/docs/glibc/libc_574.html

One paragraph on that page that really stands out for me is: The processes belonging to a single command are called a process group or job. This is so that you can operate on all of them at once. For example, typing C-c sends the signal SIGINT to terminate all the processes in the foreground process group.

It talks about the setsid() function there, but it sounds like setsid() should disassociate the child from the parent, not the other way around. Did SIGKILLing the parent process also kill the child process when the child didn't call setsid()?
 
Old 06-24-2004, 01:59 PM   #3
iftiuk
Member
 
Registered: Apr 2004
Location: Washington DC
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks for the reply. Yes even if the child doesn't call setsid() it gets killed if the parent is killed.!
 
Old 06-24-2004, 02:01 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Here's another interesting page: http://www.busan.edu/~nic/networking/puis/appc_01.htm

Check out the last section entitled: C.1.3.4 Process groups and sessions

It looks like your child process might be able to call setpgrp() to avoid the signal sharing? This is interesting to me. I'd like to hear what you find out!
 
Old 06-24-2004, 02:14 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
i think u have to fork, call setsid, then fork again and have first child exit and second continue.
Code:
[n00b@localho.outernet] cat daemon_init.c 
#include <unistd.h>
#include <sys/types.h>
#include <sys/signal.h>

#define MAXFD   64

void
daemon_init(const char *pname, int facility)
{
        int             i;
        pid_t   pid;

    printf("parent pid = %d, pgrp = %d\n", getpid(), getpgrp());
        if ( (pid = fork()) != 0)
                exit(0);                        /* parent terminates */

        /* 41st child continues */
        setsid();                               /* become session leader */

        signal(SIGHUP, SIG_IGN);
        if ( (pid = fork()) != 0)
                exit(0);                        /* 1st child terminates */

        chdir("/");                             /* change working directory */

        umask(0);                               /* clear our file mode creation mask */

    printf("chid pid = %d, pgrp = %d\n", getpid(), getpgrp());

    exit(1);
}


int main()
{
    daemon_init(NULL, 0);
    
    pause();
    return 0;
}

parent pid = 1981, pgrp = 1981
chid pid = 1983, pgrp = 1982
 
Old 06-24-2004, 02:18 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Hmm...interesting. Here's a simple program I wrote for testing:

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

void sig_handler(int signum)
{
  printf("Caught signal! My PID is: %d\n", getpid());
}

int main(void)
{
  signal(SIGUSR1, sig_handler);

  fork();
  getc(stdin);  // Just hang the parent and child for testing.

  return 0;
}
In another shell I send SIGUSR1 to either the parent or child and only one of them reports the signal.

Here's the output from shell #1:
itsme@dreams:~/C$ ./signal_share
Caught signal! My PID is: 4316
Caught signal! My PID is: 4316
Caught signal! My PID is: 4317
Caught signal! My PID is: 4317
Caught signal! My PID is: 4316

And here's the output from shell #2:
itsme@dreams:~$ ps auxw | grep signal_share
itsme 4316 0.1 0.0 1204 360 pts/2 S 11:16 0:00 ./signal_share
itsme 4317 0.0 0.0 1204 360 pts/2 S 11:16 0:00 ./signal_share
itsme 4319 0.0 0.0 1328 460 pts/0 S 11:16 0:00 grep signal_share
itsme@dreams:~$ kill -SIGUSR1 4316
itsme@dreams:~$ kill -SIGUSR1 4316
itsme@dreams:~$ kill -SIGUSR1 4317
itsme@dreams:~$ kill -SIGUSR1 4317
itsme@dreams:~$ kill -SIGUSR1 4316

PID 4316 is the parent and 4317 is the child. So it seems like there's no signal sharing going on in my program.
 
Old 06-24-2004, 02:25 PM   #7
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
taht's b/c you're in a different shell. the foreground process signal stuff is provided by the shell you are currently in. you are just sending signals via kill from a diff shell, you're not hitting Ctrl-C in the one the progs are running in.
 
Old 06-24-2004, 02:27 PM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
example:
Code:
[n00b@localho.outernet] cat sigtest.c 
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

void sig_handler(int signum)
{
  printf("Caught signal! My PID is: %d\n", getpid());
}

int main(void)
{
  signal(SIGINT, sig_handler);

  fork();
  getc(stdin);  // Just hang the parent and child for testing.

  return 0;
}
[n00b@localho.outernet] gcc sigtest.c 
[n00b@localho.outernet] ./a.out 
Caught signal! My PID is: 2042 i hit Ctrl-C once
Caught signal! My PID is: 2043
Caught signal! My PID is: 2042  and again 
Caught signal! My PID is: 2043
 
Old 06-24-2004, 02:32 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Interesting

As a side note, I always use this snippet of code to create a process that runs independent of the shell:

Code:
  if(fork())
    exit(0);
  setpgrp();
It throws it into the background, but output from the program still goes to that shell until I log off. Logging off doesn't kill the program or anything though. I guess it's getting inherited by init.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 04:04 AM
parent and child processes skora Programming 5 11-02-2003 11:41 AM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 07:07 AM
child and parent process error jdevanand Programming 1 04-29-2002 09:13 AM
child-parent-&-fork mukul Programming 2 03-24-2001 02:12 PM


All times are GMT -5. The time now is 03:29 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration