psutil icon indicating copy to clipboard operation
psutil copied to clipboard

Feature request: process group id and session id

Open davidmankin opened this issue 10 years ago • 10 comments

On Linux, I need to be able to tell if any process in a group (or session) is still alive. Unfortunately process_iter()'s Processes don't have this information.

This page says how to find the information on Linux; not sure about other platforms: http://unix.stackexchange.com/questions/132224/is-it-possible-to-get-process-group-id-from-proc

davidmankin avatar Oct 08 '15 21:10 davidmankin

Uhm... I am not sure these are portable across platforms; certainly not session id. What is session id by the way and why do you need it? Also why do you need the group id? I'm trying to understand the use case.

giampaolo avatar Oct 12 '15 09:10 giampaolo

@davidmankin you can get SID by import os; os.getsid(pid).

Session ID is a PID(PGID) of the session leader. The concept was standardized by POSIX.1. SVR4, and BSD adopted it. So it's not Linux specific. Session manages Process Groups, Process Group manages Processes. Normally the shell is the session leader that interacts with the controlling terminal. In case of daemon process, it gets its own session after forking so it's detached from the invoking session.

The use case of the group id is, say you want to group the processes that are piped together. They share the same PGID.

knoguchi avatar Dec 25 '16 20:12 knoguchi

OK, I read a bit about session/group IDs. As far as I understand this info is already available:

  • session ID can be retrieved with os.getsid(pid) as pointed out by @knoguchi
  • from group IDs we have `psutil.Process(pid).gids():
>>> psutil.Process().gids()
pgids(real=1000, effective=1000, saved=1000)

AFAIU effective should be the one of interest.

giampaolo avatar Dec 26 '16 23:12 giampaolo

This function doesn't actually show the pgid of the process. For example, my process group as returned by os.getpgid() is 8225, whereas the the gids() function returns 1000.

orodbhen avatar Jun 08 '18 16:06 orodbhen

pgid is about process groups where gids are about user group ids.

The pgid could be used to know if a process is kernel or userland. For example, if you want to set affinity of all process on the system, you cannot do it on kernel pids, so you could use pgid to filter.

seblu avatar Jan 24 '19 15:01 seblu

Is there only a single process group ID? Or there can be multiple like gids() which returns 3 (real, effective, saved)? I'm thinking in terms of API and what the new method should return. Would something likes this make sense?

>>> Process().pgid()
8225

Also, it would be good to understand whether "process groups" exist only on Linux or also on other POSIX platforms. I don't have time to investigate this right now (I'm traveling) so if somebody wants to at least steer direction that would be good (aka let's start a discussion before working on any implementation).

giampaolo avatar Jan 24 '19 17:01 giampaolo

As the first link suggest, it's an old UNIX concept. IIRC, the double fork for daemon is required to become the process group leader.

From Wikipedia:

In a POSIX-conformant operating system, a process group denotes a collection of one or more processes.

The link to to spec of getpgid in The Single UNIX ® Specification, Version 2: http://pubs.opengroup.org/onlinepubs/7990989775/xsh/getpgid.html

Have a nice trip.

seblu avatar Jan 24 '19 17:01 seblu

OK, so it seems it's about 1 value only. As it has been previously said it is already possible to do this in pure python on a per-PID basis:

>>> import os
>>> os.getpgid(os.getpid())
23948

...which leads to the question whether it makes sense to duplicate the functionality in psutil (I'm not sure - I'll have to think about it).

giampaolo avatar Jan 24 '19 17:01 giampaolo

Ok, I was misled by the answer of @orodbhen. Actually, as you said, os.getpgid could be used to get the pgid of any pid. So there is no requirement to use psutil to get it.

One interest to add it would be to benefit from psutil caching and being exported by as_dict to easily snapshot a process state. But, make also sense to not add it as you said. Your call.

seblu avatar Jan 24 '19 18:01 seblu

Yes, please implement, for caching, and to easily snapshot a process state in as_dict.

marcleblanc2 avatar Jun 30 '25 19:06 marcleblanc2