Re: How to get the pid of all children, grand children..../... of a process - RELATED Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/31/06, Ashok Sharma <aks6d@xxxxxxxxxxx> wrote:

--- Taha Hafeez Siddiqi <tawushafeez@xxxxxxxxx> wrote:

> On Tue, 2006-05-30 at 14:33 +0100, Ashok Sharma
> wrote:
> > Here is a related question.
> > When I fork() from a process a parent process with
> > some id is created together with a child process
> with
> > pid same a id of parent and id (of child) is zero.
>
>
> (Please correct me if I am wrong or partially
> right:) )
> fork() system calls return twice from the kernel,
> once it returns for
> the parent and in this case fork has a return value
> as the pid of the
> child as there is no other way for the parent to get
> the pid of the
> child.
>
> The second return is for a new child (having
> different pid ofcourse).
> Here the return value is 0. It doesn't need the pid
> of the parent as it

Yes pid of child is same as id of parent and id of
child is zero.

Now if child forks() then grandchilds pid and id will
be what values? As id of child and pid of grandchild
must be same? Or does id of child change?

What is "id"? As Jim did, I assume you mean the return value of
fork(), which is usually called the "return value of fork()" not "id".
Only in the parent case does the return value of fork() correspond to
either of the two pids of the processes involved in fork().

That is to say, the value returned to the parent is the pid of the
child after a fork() call and the the value returned to the child is 0
(to get the child's pid, you would have to use the getpid() call). If
the child (which, again, is only differentiated from the parent in the
fork() call by the return value) then wishes to fork() again, and it
succeeds, the child will receive the pid of the grandchild as the
return value of fork() and the grandchild will receive 0 as the return
value of fork(). So, you can't know ahead of time what the pid (as
return to the parent in a fork() call) of the child will be, but you
do know that if fork() returns a positive value, then it's the pid of
the child.

In the child's case (which I think is what you're confused on), the
return value of fork() in the first fork() is 0, which has no
relevance to the return value of the second call to fork() (to create
the grandchild), beyond knowing that the current process is the child
process.

CMIIW, but I think you're just making things more complicated than you
need to. Please read the man pages and some basic programming books on
multithreaded programming. None of this is really a kernel question,
but a lack of understanding of userspace.

Thanks,
Nish

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux