Wednesday 15 June 2011

C Program - How to get child's child pid in a parent [After fork] -



C Program - How to get child's child pid in a parent [After fork] -

parent-> fork ------------1st child(a)-> fork 1 time again -------------------->1st child's child(aa)

if i parent, how child's child(aa) pid in c program? or how pid within group?

there's not direct way kid pid parent pid (getppid()), but...

fork() returns 1 of 3 values: kid pid, 0, , -1.

child pid returned parent. 0 returned kid in parent's code. -1 returned when fork() had error(s).

... have child(rens) pid(s) before , after fork(). if want kid pid after execl, example, easiest way hang on when returned fork(), i.e.; store it.

if that's not efficient plenty there's possibility, commenter commented, set pipe() or 2 communicating parent.

int io[2]; pipe(io);

io[0] in; io[1] out. utilize write() , read().

c fork pid

No comments:

Post a Comment