Reputation: 2034
I came across a method named do_nanosleep()
in C that I don't understand how it is used? One thing I know that it has to do with the suspending the execution of the calling thread, but that task is handled by nanosleep()
in C. If that's true, then what is the need of do_nanosleep()
here and how it is different from nanosleep()
?
For reference, this is what it does.
/* arguments are seconds and nanoseconds */
inline void
do_nanosleep(time_t tv_sec, long tv_nsec)
{
struct timespec req;
req.tv_sec = tv_sec;
req.tv_nsec = tv_nsec;
nanosleep(&req, NULL);
}
Upvotes: 0
Views: 698
Reputation: 754190
Since do_nanosleep()
is not a standard function, you will have to track it in your source code, or in the manuals for your system, to see what it does. It might be a portability wrapper which uses nanosleep()
when it is available, and something else (usleep()
or even sleep()
) when it is not. It might do something completely unrelated to sleeping, too — but it probably does do what its name suggests.
Google has not (yet — 5 minutes after it was asked) indexed your question, and it does not know anything about do_nanosleep()
. That suggests the code should be in your source somewhere, rather than in a system manual.
With the function definition in the question, we can see that instead of requiring the user to create a struct timespec
, they can call do_nanosleep()
with two arguments, the first for the seconds and the second for the fractions of a second (0..999,999,999 measured in nanoseconds). It then calls nanosleep()
. So, in the minds of the people who wrote the software, do_nanosleep()
presents a slightly more convenient interface to the underlying nanosleep()
function. Since it is inline, the declarations for struct timespec
must still be in scope, so I'm not convinced I agree with the authors, but it is not automatically a wrong decision.
Upvotes: 4
Reputation: 33447
It looks like it's just a simplified (and crippled) wrapper around POSIX nanosleep.
The first parameter is the number of seconds, and the second is the number of nanoseconds.
Like, do_nanosleep(3, 500000000)
would (hopefully) sleep for 3 and a half seconds.
Since the function completely ignores return values... Your mileage may vary.
Upvotes: 2