11

I saw that there is a question about pthread sleep linux

However, when I looked up the man page on my Linux machine, I see the following:

SYNOPSIS #include <unistd.h>

   unsigned int sleep(unsigned int seconds);

DESCRIPTION sleep() makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not ignored.

So my question is that I would like to know which man page I should follow to put the thread sleep? In addition, if both are true, how can I control that?

I can probably write some code to test it but I want to make sure to hear some feedback from other people as well.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
istudy0
  • 1,313
  • 5
  • 14
  • 22

3 Answers3

10

The wording in your man page is likely wrong. Trust the standard and trust the man page on kernel.org. Write to the maintainer of the documentation for your distro and tell them to update the manual pages.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 1
    +1 for updating your answer to point to kernel.org. Retracting my answer which did the same. However, as for "trust the standard": only do that after reading the Linux docs, since Linux sometimes deliberately deviates from POSIX behavior. – Fred Foo May 31 '11 at 19:49
  • Thank you for your input. I will do what you suggested. – istudy0 May 31 '11 at 20:01
  • Better yet - write to the maintainer of the of the above code and ask them to fix the bug and conform to POSIX – Good Person Nov 06 '12 at 18:14
1

The man page referenced by @cnicutar says that sleep is not thread-safe (maybe that's new since 2011?). Interestingly, Dave Butenhof's 1997 book ('Programming with Posix Threads') does include an example which sleeps a thread with sleep (p18). This is an old thread (the other sort) on comp.programming.threads in which Butenhof and others discuss nanosleep in the context of pthreads.

In short, nanosleep is, I think, Ok, but sleep isn't. The nanosleep man page at kernel.org doesn't say whether nanosleep is thread-safe, but the gcc sleeping docs say that it is.

EML
  • 9,619
  • 6
  • 46
  • 78
1

There are two man pages regarding sleep function on my Linux box:

$ man -k sleep
sleep (3)   - Sleep for the specified number of seconds
sleep (3p)  - suspend execution for an interval of time

The 1st one says "the current process" as does yours.
The 2nd one says "the calling thread" but its preamble states:

This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

So i conclude that sleep(3) describes the actual behaviour and sleep(3p) is only there for reference.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152