finding all process id's and parents/children

finding all process id's and parents/children

Post by Anita Hsiu » Sun, 15 May 1994 07:19:37



I have a program that forks, then does a system call to start up
another process.  For example:

        pid = fork();
        if ( pid == 0 )  /* in child */
          {
            system("xterm -e runprog");
            exit (0);
          }

So, I get 4 relevant pids (not counting my original pid):
        1.  The pid of the forked copy of my program
        2.  The sh (shell) created by the "system" call
        3.  The call to the "xterm" part of the system call
        4.  The program "runprog" executed by the "xterm" command.

Now, the only pid that I can find easily is pid #1 (from the "fork").
But, when I exit my original program, I want to kill all the
sub-processes and forked processes, i.e. #2, #3, #4.  What is the best
and cleanest way of determining forked children and their subsequent
"children"?

I've thought of using "popen" and sending a "getpid()" and/or
"getppid()" through the pipe, but I haven't been able to make it work.
Does anyone have other ideas?  Or help me expand on this one?

I'd be very grateful for any help!

-- Anita --

--
---------------------------------------------/------------------------------
| Anita Hsiung         | E<**\R. _____ * |.Q \ U .   \ K . |\    . .       |

| BBN Canoga Park, USA |. \/.  \--- H  --+----\  .*   /___/ ** V     V .   |

 
 
 

finding all process id's and parents/children

Post by Shung-I Y » Sun, 15 May 1994 17:57:30


:       pid = fork();
:       if ( pid == 0 )  /* in child */
:         {
:           system("xterm -e runprog");
:           exit (0);
:         }

   use execlp() to replace system().

--
=================================================

=================================================

 
 
 

finding all process id's and parents/children

Post by Doron Zifro » Thu, 19 May 1994 19:39:19


: I have a program that forks, then does a system call to start up
: another process.  For example:

:       pid = fork();
:       if ( pid == 0 )  /* in child */
:         {
:           system("xterm -e runprog");
:           exit (0);
:         }

: So, I get 4 relevant pids (not counting my original pid):
:       1.  The pid of the forked copy of my program
:       2.  The sh (shell) created by the "system" call
:       3.  The call to the "xterm" part of the system call
:       4.  The program "runprog" executed by the "xterm" command.

: Now, the only pid that I can find easily is pid #1 (from the "fork").
: But, when I exit my original program, I want to kill all the
: sub-processes and forked processes, i.e. #2, #3, #4.  What is the best
: and cleanest way of determining forked children and their subsequent
: "children"?

: I've thought of using "popen" and sending a "getpid()" and/or
: "getppid()" through the pipe, but I haven't been able to make it work.
: Does anyone have other ideas?  Or help me expand on this one?

: I'd be very grateful for any help!

: -- Anita --

Maybe you should look into the issue of "process group".
Usually, a process and all of its sons belong to the same process group.
You can send a signal to a process group, and it is delivered to all the
processes in the process group.
You can call setpgrp() in the forked child to set the process group of himself
and of its descendants to be the forked child process id.
Later on, send a signal to the group whose id is the process id of the
forked child.  See the kill() man page on how to send a signal to a process
group.  man 2 intro and man setpgrp() may also be of help.

--
Doron Zifrony                1 Oranim Str.            Tel: 972-3-5347521
Data General Technology      Givat Shmuel 51905       Fax: 972-3-5344499

Disclaimer: The opinions expressed hereby are my personal opinions.

 
 
 

1. Getting a child and child's child process-id

 Why not just have the parent fork twice ??? Saves a lot of trouble ...
--


#  Have you read your Book In Basic Life Education today ? You should !!  #
#  Any views I express are not necessarily those of Brighton Polytechnic  #
###################### "surivorter erutangis a ma I" ######################

2. 2.5.28: VM strict overcommit

3. Registers on Sun/SPARC

4. Can a child process change the parent process's directory?

5. Socks v5 Client

6. Solaris SMP machines and SIGALRM in child process 'propagated' to parent

7. LaserMate & WDH-7001C

8. Newbie. find and kill by parent process id

9. How to find parent process id?

10. Find widget id without having parent id????

11. redirect output from child process into parent's stdout

12. Solaris 9 - nohup'd background child process dying when ^C pressed in parent (/sbin/sh)