Print ancestor list in c (name and PID)


 
Thread Tools Search this Thread
Top Forums Programming Print ancestor list in c (name and PID)
# 1  
Old 10-24-2011
Print ancestor list in c (name and PID)

I am trying to write a C program that prints its ancestor name and PID

For example:
Lets say my program name is prog1 then the output should be
prog1 with PID: 2345
bash with PID: 4567
....
init with PID: 1

This is just a scratch work.
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int child, parent;
char command [100];
int GetPID (int);

int main()
{
child = getpid();
printf ("pid is %d\n", child);

parent = getppid();
printf ("parent pid is %d\n", parent);
GetPID (parent);

sprintf (command, "ps -p %d -o pid,cmd", child);
system (command);
sprintf (command, "ps -p %d -o pid,cmd", parent);
system (command);

return (0);
}

int GetPID (child)
{
child  = getpid();
printf ("grandparent pid \n");
sprintf (command, "ps -p %d -o ", child);
system (command);
return child;
}

Any help is appreciated!!! Smilie

Last edited by jim mcnamara; 10-24-2011 at 06:05 PM.. Reason: add code tags
# 2  
Old 10-24-2011
The output of ps is highly system-dependent, and there's probably better ways to get the information depending on your system. What is your system?
# 3  
Old 10-24-2011
I am using UNIX (putty).
# 4  
Old 10-24-2011
PUTTY is not UNIX the same way Firefox is not a webpage. They show you things from somewhere else.

If you don't know your system, try uname -a
# 5  
Old 10-24-2011
system is LINUX.

is there a way to use 'ps' command to get the ancestor name and pid?
# 6  
Old 10-24-2011
Since you have Linux, you can more easily get the information directly from /proc/.

Code:
$ cat pppid.c

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


int main(void)
{
        FILE *fp;
        int pid;
        char buf[128];
 int n=1;

        printf("pid=%d\n", (int)getpid());
        printf("ppid=%d\n", (int)getppid());

 pid=getppid();

 while(pid>1)
 {
         sprintf(buf, "/proc/%d/stat", (int)pid);
         fp=fopen(buf, "r");
  if(fp == NULL) break;

  // Ignore one number, then two strings, then read a number
         fscanf(fp, "%*d %*s %*s %d", &pid);
         fclose(fp);
         printf("gp%d id=%d\n", n++, (int)pid);
 }
}

$ gcc pppid.c
$ ./a.out
pid=11034
ppid=5249
gp1 id=5248
gp2 id=5245
gp3 id=4000
gp4 id=1
$

Linux is not UNIX by the way.

---------- Post updated at 02:46 PM ---------- Previous update was at 02:42 PM ----------

Oh, you can read the name from /proc/pid/comm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list all outbound sockets given a PID?

I used netstat -tp <pid> to list all Foreign Addresses i.e OutBound sockets on Linux. Likewise, i wish to list all Foreign Addresses on Sparc Solaris. I get illegal option -- t when i try this command on Solaris. The second query i have is that in the output of netstat command some... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. AIX

UNIX ksh - To print the PID number and repeat count

This question is asked in an interview today that I have to return output with each PID number and the count of each PID number logged today. Here is the script that I have written. Can you confirm if that would work or not. The interviewer didn't said if my answer is correct or not. Can someone... (5 Replies)
Discussion started by: Subodh Kumar
5 Replies

3. Shell Programming and Scripting

Portable way to print list in columns

so if i have a list of file names, on linux system, we can use the column command to split them up into columns. sadly, the "columns" command does not exist on some OSes. so i found that the pr command can also work. but, pr tends to truncate the names. There's a way around that on... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Print list to row using awk

Hello, can somebody help me on this please. I have a list of numbers and I want to print them in one line seprated by a comma except the last one using awk 34 12 56 76 88 output 34,12,56,76,88 Thanks Sara (15 Replies)
Discussion started by: Sara_84
15 Replies

5. Shell Programming and Scripting

Unix Script -- Suggestions to list and kill PID's sequentially

Hi, I'm trying to write a script where i'm trying to grep the PID and the associated file and list them. Then execute the KILL command sequentially on the listed PID's for ".tra" files ==================================================== ps -aux | grep mine adm 27739 0.2 0.8 1131588... (12 Replies)
Discussion started by: murali1687
12 Replies

6. Shell Programming and Scripting

list of all predecessors and successors for given PID

Dear experts, I'm moving from SunOS to Linux. Linux has no ptree, but it has pstree. Anyway, pstree doesn't show what ptree used to print with output nice to grep. My Linux distribution doesn't have proctree either. This forum has few posts touching ptree/pstree topic, but I didn't... (2 Replies)
Discussion started by: bzk
2 Replies

7. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

8. Shell Programming and Scripting

How to print a directory list to a file?

I have directory of files listed with stats. I need to print only files with the same stats to a file. I tried this, and it printed a blank document. *1556 > tmp/list it did not work any suggestions? (3 Replies)
Discussion started by: rocinante
3 Replies

9. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

10. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies
Login or Register to Ask a Question