This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: A patch for nanosleep


> 
> hjl@lucon.org (H.J. Lu) writes:
> 
> > Here is an optimized nanosleep.c for glibc 2.
> 
> I looked though the sleep, nanosleep, alarm, and SIGCHLD documentation
> in POSIX and Unix98 and haven't found a reason why there must be any
> change.
> 
> It is said that SIGCHLD has to be treated a bit special by the signal
> code as the SIG_IGN and SIG_DFL actions are so stupid.  So be it.
> 
> But where does this effect this case?  When I write a simple program
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> #include <unistd.h>
> #include <sys/types.h>
> 
> int
> main()
> {
>   pid_t p = fork();
>   if (p == 0)
>     {
>       sleep (1);
>       exit (0);
>     }
> 
>   sleep (5);
> 
>   return 0;
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> I do see something weird only when using strace to run the program.
> If I run it normally it just fine stops after five seconds.  When
> using strace it returns as soon as it gets the SIGCHLD signal, but is
> this a problem with the libc of strace?
> 
> 
> Anyhow, even if somebody can tell me that there is a problem with
> sleep (maybe for historical reasons) I'm not convinced there is a
> problem with nanosleep.  It seems to work as expected.  If the sleep()
> behaviour must change we must not change nanosleep.  HJ patch could
> then be applied to sleep.
> 
> 
> So please, enlighten me on this.  Thanks,
> 

The problem is signal(SIGCHLD, SIG_IGN). If you add it before
fork, you will see the difference. I am not sure where the bug
is. It seems that for SIGCHLD, SIG_DFL means nanosleep () won't
be interrupted, but you will get SIGCHLD. SIG_IGN means you
won't get SIGCHLD, but nanosleep will be interrupted. However,
POSIX says the default handling of SIGCHLD is ignored the signal.

signal is called with SA_RESTART. According to Linus, it is
impossible for nanosleep.


-- 
H.J. Lu (hjl@gnu.org)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]