For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2006 > Terminating all programs running on a shell









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Terminating all programs running on a shell
Frank Meerkötter

2006-07-25, 7:00 pm

Hi,

i'm having a program which uses a popen() like construct to start
some programs and read their output. I also maintain a list
of the pid of all childs i've spawned using my version of popen().

The reason i'm doing this is that it need to be able to shutdown
all my children when receiving a given signal. I do by sending
a SIGKILL to all childs.

My problem is that sometimes childs are left behind. It
seem that i'm only terminating the shell on which
the program in question is running and not the program
itself (i'm using fork() and exel()).

How can i make sure that all programs running on a shell
are terminated when the shell terminates?

Should i use SIGTERM so the shell has a chance to shutdown?

Are processgroups or sessions a direction to investigate further?

Regards,
Frank

Barry Margolin

2006-07-26, 4:01 am

In article <1153848573.306573.185800@m73g2000cwd.googlegroups.com>,
"Frank Meerkötter" <frank@betaversion.net> wrote:

> Hi,
>
> i'm having a program which uses a popen() like construct to start
> some programs and read their output. I also maintain a list
> of the pid of all childs i've spawned using my version of popen().
>
> The reason i'm doing this is that it need to be able to shutdown
> all my children when receiving a given signal. I do by sending
> a SIGKILL to all childs.
>
> My problem is that sometimes childs are left behind. It
> seem that i'm only terminating the shell on which
> the program in question is running and not the program
> itself (i'm using fork() and exel()).
>
> How can i make sure that all programs running on a shell
> are terminated when the shell terminates?
>
> Should i use SIGTERM so the shell has a chance to shutdown?


Yes.

>
> Are processgroups or sessions a direction to investigate further?


Yes, this is what process groups are for. When popen() forks the child
process, it should make itself a process group leader. Then you can
kill the whole process group by using the negative of the PID in the
kill() call. This should kill all the descendant processes unless the
application you run creates its own process groups, which isn't common.

Don't use sessions. A session is supposed to contain all the processes
associated with a particular interactive login instance.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com