Get child list of parent process in C

linuser

I have a PID of process that may contain childs. How can I get the PID of all child processes? I make my own PTY handler, so when user run a shell in this handler he may run anymore programs ( directly from shell ), every ran program becomes a child of shell. So, when I press Ctrl+C I need to send signal to the most new process, so need to know PID of that last one.

Basile Starynkevitch

You should keep explicitly all the pids (result of fork(2)...) of your child processes (and remove a pid once you waited it successfully with wait(2) etc...)

It is up to you to choose the data structures to keep these pids.

Any other approach (e.g. using proc(5)... which is what ps and pstree are doing.) is not very portable and inefficient.

So the basic rule is that every time you call fork you should explicitly keep its result (and test for the 3 cases: 0 if in child process, >0 if in parent process, <0 on error) and use that at wait time.

Read Advanced Linux Programming; it has many pages relevant to that subject.

You might also be interested by process groups and sessions. See setpgrp(2), setsid(2), daemon(3), credentials(7) etc. Notice that with a negative or zero pid kill(2) can send a signal to a process group, and that you could also use killpg(2) for that purpose.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get child process from parent process

From Dev

C Programing : Parent & Child Process

From Dev

c variable value for parent and child process

From Dev

send signal from parent process to child in C

From Dev

Get child process operation result from parent process

From Dev

How to get all child process's PIDs when given the parent PID in C

From Dev

How to get a nested list of a child by Parent id?

From Dev

Can a child process wait for the parent process to terminate in Linux programming in C?

From Dev

How to find all child process of a parent process (in C)

From Dev

C# How to Get list of Ids from Parent-Child list

From Dev

Share linked list between child and parent in C

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

After starting process, how to get parent's PID in the child?

From Dev

How to get the child nodes of a parent and process it with jquery ajax

From Dev

How to get the id of a very short child process if the parent is known?

From Dev

How to get the child nodes of a parent and process it with jquery ajax

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

From Dev

How to get list of specific child instances from list of parent instances?

From Dev

Returning a value from the child process to its parent in C created with fork()

From Dev

Kill the parent of a child pipe process

From Dev

The file used by parent and child process

From Dev

How to know if a process is a parent or a child

From Dev

How to get List of Parent Entities with Child Count In Nhibernate QueryOver

From Dev

Rails Adjacency List - Parent Child Tree - Get root

From Dev

how to get the list of child nodes for each treeview parent

From Dev

Restrict child process 's cpu and ram usage by parent process using C/C++ in Linux

From Dev

Sorting a list by parent/child relations ship so I have definitely processed a parent before I try to process the child

From Dev

How to get list of all child process spawned by a script

Related Related

  1. 1

    How to get child process from parent process

  2. 2

    C Programing : Parent & Child Process

  3. 3

    c variable value for parent and child process

  4. 4

    send signal from parent process to child in C

  5. 5

    Get child process operation result from parent process

  6. 6

    How to get all child process's PIDs when given the parent PID in C

  7. 7

    How to get a nested list of a child by Parent id?

  8. 8

    Can a child process wait for the parent process to terminate in Linux programming in C?

  9. 9

    How to find all child process of a parent process (in C)

  10. 10

    C# How to Get list of Ids from Parent-Child list

  11. 11

    Share linked list between child and parent in C

  12. 12

    Using Debugger how to get child process's PID from Parent

  13. 13

    After starting process, how to get parent's PID in the child?

  14. 14

    How to get the child nodes of a parent and process it with jquery ajax

  15. 15

    How to get the id of a very short child process if the parent is known?

  16. 16

    How to get the child nodes of a parent and process it with jquery ajax

  17. 17

    Using Debugger how to get child process's PID from Parent

  18. 18

    If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

  19. 19

    How to get list of specific child instances from list of parent instances?

  20. 20

    Returning a value from the child process to its parent in C created with fork()

  21. 21

    Kill the parent of a child pipe process

  22. 22

    The file used by parent and child process

  23. 23

    How to know if a process is a parent or a child

  24. 24

    How to get List of Parent Entities with Child Count In Nhibernate QueryOver

  25. 25

    Rails Adjacency List - Parent Child Tree - Get root

  26. 26

    how to get the list of child nodes for each treeview parent

  27. 27

    Restrict child process 's cpu and ram usage by parent process using C/C++ in Linux

  28. 28

    Sorting a list by parent/child relations ship so I have definitely processed a parent before I try to process the child

  29. 29

    How to get list of all child process spawned by a script

HotTag

Archive