Re: Re: waitpid(2) and CLONE_THREAD

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

 



Hi Sergey...

Sorry, it took me a while to reply yours. 

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

Thanks for the correction. Actually, I was answering right out from my 
head, so some things might went wrong :(

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

Sure nothing is changing? To the best I know, setting env variable 
LD_ASSUME_KERNEL to "2.4.9" or lower will disable NPTL for the newly 
born session, thus NPTL isn't used. But, if you're sure nothing was 
changing, then certainly it didn't involve anything in user space. In 
other word, kernel space still behave the same.

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

Hm, just for comparison (maybe you can use it as clue...), I created a 
simple pthread based program on Redhat 9 (yes, old but already using 
NPTL). A master process fork two children and wait for them. Here is 
what I get from stracing them:

clone(child_stack=0x41036850, 
flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID|CLONE_DETACHED, 
[22673], {entry_number:6, base_addr:0x41036c40, limit:1048575, 
seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, 
seg_not_present:0, useable:1}) = 22673
write(1, "Thread 2 successfully forked\n", 29) = 29
futex(0x40836d08, FUTEX_WAIT, 22672, NULL) = 0
write(1, "Thread 1 is terminated successfu"..., 36) = ? ERESTARTSYS (To 
be restarted)
futex(0x41036c88, FUTEX_WAIT, 22673, NULL) = 0

Instead of using wait(), I use pthread_join(). And....seems like, 
pthread_join eventually use futex to signal when the child exits. In 
one of the child, I see this:
futex(0x4212e13c, FUTEX_WAKE, 1, {746153288, 1919448096})   = 0

My raw conclusion: maybe wait() is useless when we are facing NPTL based 
threaded program. As the alternative, we can rely on the 
semaphore/mutex/etc as the sign a child will exit.

As always, somebody please CMIIW.

regards,

Mulyadi


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