C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-18-2006, 09:25 AM   #1
g_p
Registered User
 
Join Date: Dec 2006
Posts: 81
Question related to getpid and getppid

Good evening,

i would like to ask you sthg related to finding the parent's id of a process.

I need my C program to output e.g. 48 : 47 --> - 34 --> - 2 --> - 0

where 48 is the id of the process,47 is the parent's id,-34 is the parent's id of the parent of the process etc.

I do :

Code:
#include <unistd.h>    
#include <sys/types.h>  
#include <errno.h>      
#include <stdio.h>      
#include <sys/wait.h>   
#include <stdlib.h>     

int main() {

printf("The process id is %d\n",getpid());
printf("The processes parent's id is %d\n",getppid());
 
}
What shall i do in order to output the parent's,parent id and the other ids?
Thanks,in advance!
g_p is offline   Reply With Quote
Old 12-18-2006, 09:33 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
Use a pipe and when you fork, pass it to the child.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-18-2006, 09:40 AM   #3
g_p
Registered User
 
Join Date: Dec 2006
Posts: 81
Well,can't i use some other function like getpid and getppid in order to output the desirable result?
g_p is offline   Reply With Quote
Old 12-18-2006, 10:11 AM   #4
.
 
Join Date: Nov 2003
Posts: 293
Are you trying to trace "parentage" back possibly to init (modern UNIX init has pid=1)?
Or maybe as far as possible...

getpid and getppid work only in the context of the current process. You can't run them in your process and get the values for some other process. Sorry if I don't get you requirement.
quzah's advice works great when you are going down to children and "grand"children.
jim mcnamara is offline   Reply With Quote
Old 12-18-2006, 11:35 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,688
Parsing the output of the "ps" command is about the only easy way of figuring out what all the process IDs are.

And how come you're outputting negative PIDs?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 09:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22