Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: process group id and session id #697

Open
davidmankin opened this issue Oct 8, 2015 · 9 comments
Open

Feature request: process group id and session id #697

davidmankin opened this issue Oct 8, 2015 · 9 comments

Comments

@davidmankin
Copy link

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

@giampaolo
Copy link
Owner

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.

@knoguchi
Copy link

knoguchi commented Dec 25, 2016

@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.

@giampaolo
Copy link
Owner

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.

@orodbhen
Copy link

orodbhen commented Jun 8, 2018

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.

@seblu
Copy link

seblu commented Jan 24, 2019

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.

@giampaolo giampaolo reopened this Jan 24, 2019
@giampaolo
Copy link
Owner

giampaolo commented Jan 24, 2019

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).

@seblu
Copy link

seblu commented Jan 24, 2019

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.

@giampaolo
Copy link
Owner

giampaolo commented Jan 24, 2019

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).

@seblu
Copy link

seblu commented Jan 24, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants