Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Linux, how can I kill all processes in a session (with same SID) using system calls?

I am trying to figure how to kill all processses in a session (with the same SID) using system calls with C. I am not interested in to just kill all with a specific PGID since not all processes I am interested about does not have the same PGID, but they have the same SID.

My research have only found this, there Graeme made an excellent answer for scripts: https://unix.stackexchange.com/questions/124127/kill-all-descendant-processes

I would be pleased to get an answer for how it would be possible to kill all direct descendant children and even more pleased how I could kill all children within the session.

Or is what I am asking possible? I am not intrested in a solution there I am simply listing the PIDs of the parents descendant.

like image 205
Tobias Johansson Avatar asked Apr 11 '26 13:04

Tobias Johansson


1 Answers

You can always use the /proc/ file system to query processes (see proc(5) for more). In particular you can then scan the /proc/PID/ directories (where PID is some numerical name like 1234, it is the relevant pid, so process of pid 1234 is described in /proc/1234/ pseudo-directory; hence you could readdir the /proc/ directory and find every numerical name inside it) and check which processes have a defined parent pid. You'll read sequentially pseudo-files like /proc/1234/status (and its PPid: line). See also this answer and that one.

like image 184
Basile Starynkevitch Avatar answered Apr 14 '26 01:04

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!