[Webinar] Learn how to a build a cloud-first strategyRegister Now


Finding group id from PID

Posted on 2009-02-11
Medium Priority
Last Modified: 2013-12-20
I'm trying to determine a list of process ids that are associated with a particular group (i.e., the owner of the process belongs to the group) That is, if I look at a PID, I can determine the UID.  I then want to know if that UID is in a particular group.

As best I can tell I have to:
- get the UID from the PID using getprocs64
- get the username from the UID using getpwuid(uid)

then, given the groupname I need to call getgrnam(groupName)
which will give me a group structure.  grp->gr_mem is then an array of strings, which Il will have to parse looking for the username.

Then I have to repeat that for every process in the system.

Surely there is a less pedestrian way.
Question by:jjrrww
  • 2

Author Comment

ID: 23619188
Rather than explain what I'm trying to do... this is what I am trying to accomplish:

Given the set of processes currently running on the system, I need to identify those processes (pids) which belong to a group.  My application allows a user to collect metrics about a subset of processes.  That subset is defined by one or more of several criteria:

username (which maps to UID, which is readily gleaned from the process)
group (the problem at hand)
command (the program being run)
or matching a token on the command line.

Except for group, all are readily available on the procsinfo64 structure returned by getprocs64.

It would appear that I have to go on a scavenger hunt to identify the processes that are "in" a particular group.

Is there a better way that I'm missing?
LVL 14

Accepted Solution

sjm_ee earned 2000 total points
ID: 23649040
I can't see a better way - I can't see any gid anywhere in /usr/include/sys/proc.h. Did you try at http://www.ibm.com/developerworks/?

Expert Comment

ID: 23687490
This sounds like something a "ps" command can get you.

On AIX,  try "ps -aef" will get you all that and pipe it to a "grep" command to select your specific need.


Author Closing Comment

ID: 31545848
I guess the final understanding of this is that a group is not in any way a process attribute, but rather a username attribute, with a many-many realtionship.  As a result, chasing after those relationships is the only way to do it.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question