Finding group id from PID

Posted on 2009-02-11
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
    LVL 1

    Author Comment

    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

    I can't see a better way - I can't see any gid anywhere in /usr/include/sys/proc.h. Did you try at
    LVL 2

    Expert Comment

    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.

    LVL 1

    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    Unix / Linux grid computing 5 95
    maven archtype selection in eclipse 1 47
    Virtualizing TAPE on dual VIOS 3 55
    Image not there 4 0
    Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
    About FreeBSD Jails In FreeBSD, jails are a way of doing operating system level virtualization.  The basis of FreeBSD jails is chroot (, which changes the root directory of processes.  As a…
    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…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now