0

let me start by saying this is my first question on stackoverflow, and I am very new to coding as well, so I appolagize in advance for any shortcomings.

I am trying to find out how a child process can display the parent pid of their own parent process (the child process' grandparent pid)

Here is the code I have, I added a comment that I need to change:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
int pid, status, status2;
pid = fork();
switch (pid)
{
    case -1: // An error has occured during the fork process.
        printf("Fork error.");
        break;
    case 0:
        pid = fork();
        switch (pid)
        {
            case -1:
                printf("Fork error.");
                break;
            case 0:
                printf("I am the child process C and my pid is %d\n", getpid());
                printf("My parent P has pid %d\n", getppid());
                printf("My Grandparent G has pid %d\n", //what to put here );
                break;
            default:
                wait(&status);
                printf("I am the parent process P and my pid is %d\n", getpid());
                printf("My parent G has pid %d\n", getppid());
                break;
        }
        break;
    default:
        wait(&status2);
        printf("I am the Grandparent process G and my pid is %d\n", getpid());
        break;
    }
}

also, general tips would be appreciated

Nelco
  • 3
  • 2
  • 1
    Does this answer your question? [Programmatically get parent pid of another process?](https://stackoverflow.com/questions/1525605/programmatically-get-parent-pid-of-another-process) – n. m. could be an AI Mar 28 '21 at 09:28
  • Concerning `"%d"` for what should be `pid_t pid` [What is the correct printf specifier for printing pid_t](https://stackoverflow.com/q/20533606/2410359) – chux - Reinstate Monica Mar 28 '21 at 09:31

1 Answers1

1

You could just save the pid in grandparent.

int pid, status, status2;
int pppid = getpid();
pid = fork();
switch (pid) {
   ....
   printf("My parent G has pid %d\n", pppid);
}

or save the pid of getppid() in parent. There is no "standard" way of getting "parent pid of parent pid" so it's just the same as getting pid of any other process. You could inspect /proc/<pid>/stat, something along:

pid_t getppid_of_pid(pid_t pid) {
    intmax_t ret = -1;

    char *buf;
    int r = asprintf(&buf, "/proc/%jd/stat", (intmax_t)pid);
    if (r == -1) goto asprintf_err;

    FILE *f = fopen(buf, "r");
    if (f == NULL) return fopen_err;

    if (fscanf(f, "%*s %*s %*s %jd", &ret) != 1) return fscanf_err;
    
fscanf_err:
    fclose(f);
fopen_err:
    free(buf);
asprintf_err:
    return ret;
}
     

...
     printf("My Grandparent G has pid %jd\n", (intmax_t)getppid_of_pid(getppid()));

See man procfs for explanation of fields in /proc/../stat.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • awesome thanks, simple fix, but it works. I've taken a couple coding classes for other languages, but this is only my first assignment for C and the instructor has somewhat of an unconventional approach to teaching it... thanks again. – Nelco Mar 28 '21 at 11:25