1

I'm trying to get the pid, the ppid and the pgid and no matter what I do getpgid(pid) always gives me the pid.

At first, I was using it like this:

 printf("PGID: %d", getpgid());

Then I realized, getpgid was taking a pid as argument so I did this:

 printf("PGID: %d", getpgid(getpid()));

But this is still giving me the same number as the pid...

So I tried this:

 pid_t pid;  
 pid = getpid(); 
 printf("PGID: %d", getpgid(pid));

And this still doesn't work... Any idea why?

PS: I'm a beginner in c.

icedwater
  • 4,701
  • 3
  • 35
  • 50
Thomas Dupond
  • 81
  • 2
  • 8
  • It works - The shell is probably setting the process group id the same as the process id. Perhaps do a fork and try it in the child process – Ed Heal Feb 03 '16 at 10:08
  • 1
    First try describing in what situation the PID and PGID would be different. Then reproduce that before trying again. – Zan Lynx Feb 03 '16 at 10:09
  • Ok, I tried to fork, so this time : For the parent process : The pgid is the same as the pid... And for the child process the pgid is the same as the ppid... And sometimes, it just print one for the ppid. But I gess i did something wrong in the fork – Thomas Dupond Feb 03 '16 at 10:43
  • 1
    @ThomasDupond the ppid being 1 tells you that the parent process died before the child process, and the child process has been adopted by the init process (PID 1). – Snaipe Feb 05 '16 at 13:33
  • Minimal runnable `setpgid` and `getpgid` where they differ: https://stackoverflow.com/questions/6108953/how-does-ctrl-c-terminate-a-child-process/52042970#52042970 – Ciro Santilli OurBigBook.com Aug 28 '18 at 12:31

3 Answers3

2

The process group leader is always the one that has pid == pgid, so perhaps you are making always the same test (on a process group leader) and this is the reason of getting always the same pgid as the pid of the process. When a process becomes a group leader, the kernel assigns it it's own pid as the pgid, so perhaps you are always getting that if you are checking in only one process. Bash uses process groups to distinguish background jobs and do job control, so every command you execute from a bash(1) shell will have a new process group and one of these processes (the only one if you only launched one process) will have the same the pgid and the pid of one of the processes of the job you are requesting.

The reason of this logic (to use the pid of the process group leader as the pgid) is to not have to deal with active process groups as no more groups than processed can be in the system... so when a new group id is needed the nearest process available is the own process that triggers the new process group creation, so assigning its own pid as pgid is no harmful.

For a simple example... just try the same program you used to demonstrate the pid and pgid values in a long pipe by piping them (you should try to print the output to stderr instead of stdout or all the outputs will get lost in the input buffer of the next process in the pipe):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int pid = getpid();
    fprintf(stderr, "pid=%d; pgid=%d\n", pid, getpgid(pid));
    exit(EXIT_SUCCESS);
}

and then

$ testpgid | testpgid | testpgid
pid=3819; pgid=3819
pid=3820; pgid=3819
pid=3821; pgid=3819
$ testpgid | testpgid | testpgid
pid=3833; pgid=3833
pid=3835; pgid=3833
pid=3834; pgid=3833
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
0

getpid() returns the process ID of the current process. When a process is running, the kernel schedules its runtime - especially it assigns a PID to it - such information is stored inside the kernel address space, in data structures (e.g. inside a task struct). Thus, when a process calls the getpid() system call, the kernel just has to look in the task structure of the calling process.

msc
  • 33,420
  • 29
  • 119
  • 214
-2

It is a group ID of the process specified by pid.

Suppose if we consider pid as zero then the ID of the current process is used in it. getpgrp() and getpgid(0) are equivalent to each other.

e.g. pid_t getpgid(pid_t pid);

Bruno Gelb
  • 5,322
  • 8
  • 35
  • 50