#include <stdio.h>
#include <fcntl.h>
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>

int main(void) {

char fname[256];
psinfo_t thedata;
int fd, nbytes;
pid_t the_pid;

the_pid = getpid();
do {
	sprintf(fname, "/proc/%ld/psinfo", the_pid);
	if ((fd = open(fname, O_RDONLY)) == -1) {
		perror(fname);
		return 1;
	}
	if (read(fd, &thedata, sizeof(psinfo_t)) < sizeof(psinfo_t)) {
		fprintf(stderr, "Could not read whole psinfo_t struct\n");
		close(fd);
		return 1;
	}
	close(fd);
	printf("Parent of %ld is %ld\n", (long)(thedata.pr_pid), (long)(thedata.pr_ppid));
	the_pid = thedata.pr_ppid;
} while (thedata.pr_ppid != thedata.pr_pid);
return 0;
}
