Re: waitpid(2) and CLONE_THREAD

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

 



Mulyadi, thanks for reply.

> > If a child is created by calling clone() with flags=0, it can
> > successfully be waitpid()'ed either by specifying its pid as returned
> > by clone() or by pid=-1.
> >
> > But if clone() is called with flags=(CLONE_THREAD | CLONE_SIGHAND |
> > CLONE_VM), waitpid() fails with ECHILD, no matter if it's called with
> > or without __WALL or __WCLONE flags, and even no matter if SIGCHLD is
> > OR'ed explicitly with clone()'s flags.
> 
> I think you've found the "why" :) clone() returns the PID, but in NPTL 
> environment, this means it returns thread group id, not the PID of the 
> new clone()-ed process. AFAIK, tgid==thread group's leader pid == 
> parent process, thus no wonder your waitpid() failed.

Actually, clone() returns "thread ID" (TID) (and man page also says so).

> Try to run your program and prefix it with LD_ASSUME_KERNEL (to disable 
> NPTL temporarily):
> $ LD_ASSUME_KERNEL=2.4.9 ./your-program

This doesn't help.

> >Is it possible to wait for a child from the same thread group (i.e.
> >having the same PID and different TID)?
> 
> You mean the other way around i.e "Is it possible to wait for a child 
> from the same thread group (i.e. having the same TGID but  different 
> PID)?"? technically I think yes, but since glibc call acts on TGID, it 
> is hard to do it unless we get a clue from kernel space on what the 
> real PID is (AFAIK it can be checked from /proc/≤pid>/status).

I suppose that PID is what is returned by getpid(), and, if so, PID==TGID.

What I observe is this:

with flags to clone() mentioned above, a child is born, whose PID (as returned
by getpid()) equals parent's PID (also as returned by getpid()), and whose TID
(the value returned by clone() call) is different. And this entirely matches my
expectations. But I cannot figure a way to wait for that child. The calls 

waitpid( -1, &status, __WALL );
waitpid( tid, &status, __WALL );

and the like all fail with ECHILD.

--
Sergey


--
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