- 论坛徽章:
- 0
|
学习下呵呵
#include <errno.h>
#include <time.h>
#include <signal.h>
static void signal_int(int signo);
static int better_sleep (double sleep_time);
int main(int argc, char* argv[])
{
signal(SIGINT, signal_int);
better_sleep(10);
}
static int better_sleep (double sleep_time){
struct timespec tv;
struct timespec tv_rval;
int rval;
int rval;
/* Construct the timespec from the number of whole seconds... */
tv.tv_sec = (time_t) sleep_time;
/* ... and the remainder in nanoseconds. */
tv.tv_nsec = (long) ((sleep_time - tv.tv_sec) * 1e+9);
printf("tv.tv_sec = %d\n", tv.tv_sec);
printf("tv.tv_nsec = %ld\n", tv.tv_nsec);
while (1){
/* Sleep for the time specified in tv. If interrupted by a
signal, place the remaining time left to sleep back into tv. */
int rval = nanosleep (&tv, &tv_rval);
if (rval == 0){
/* Completed the entire sleep time; all done. */
printf("have passed sleep time.\n");
return 0;
}
else if (errno == EINTR){
/* Interrupted by a signal. Try again. */
printf("have received by interruped signal.\n");
printf("tv_rval.tv_sec = %d\n", tv_rval.tv_sec);
printf("tv_rval.tv_nsec = %ld\n");
return rval;
}
else{
/* Some other error; bail out. */
perror("nanosleep");
return rval;
}
}
return 0;
}
static void signal_int(int signo)
{
printf("signal:%d\n", signo);
}
简单修改了一下代码,调试学习下呵呵.
现在有个问题:gcc test.c -lrt后运行程序,在还没有到10s的时候,ctrl+c中断程序,程序将会打印tv_rval结构体的成员值,为什么打印的结果始终都是tv_rval.tv_sec等于tv_rval.tv_nsec的值呢?也就是说纳秒数打印不了.
另外:man nanosleep:
DESCRIPTION
The nanosleep() function causes the current thread
to be suspended from execution until either the time inter-
val specified by the rqtp argument has elapsed or a signal
is delivered to the calling thread and its action is to
invoke a signal-catching function or to terminate the pro-
cess. The suspension time may be longer than requested
because the argument value is rounded up to an integer mul-
tiple of the sleep resolution or because of the scheduling
of other activity by the system. But, except for the case of
being interrupted by a signal, the suspension time will not
be less than the time specified by rqtp, as measured by the
system clock, CLOCK_REALTIME.
The use of the nanosleep() function has no effect on the
action or blockage of any signal.
我英语不好,其中这一句说的是什么意思:
The suspension time may be longer than requested
because the argument value is rounded up to an integer mul-
tiple of the sleep resolution or because of the scheduling
of other activity by the system. |
|