sleep

简介: 1、sleep()-------以秒为单位unsigned int sleep(unsigned int seconds); //#includesleep()非系统调用,sleep()是在库函数中实现的,它是通过alarm()来设定报警时间,使用sigsuspend()将进程挂起在信号SIGALARM上。

1、sleep()-------以秒为单位
unsigned int sleep(unsigned int seconds); //#include<unistd.h>
sleep()非系统调用,sleep()是在库函数中实现的,它是通过alarm()来设定报警时间,使用sigsuspend()将进程挂起在信号SIGALARM上
sleep()只能精确到秒级上。sleep()会令目前的进程暂停,直到达到参数seconds 所指定的时间,或是被信号所中断。
若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余秒数。

 

2、usleep()----以微秒为单位
unsigned int usleep(unsigned int useconds); //#include<unistd.h>
除了时间单位为微秒以外,在使用上与sleep()差不多。还有就是实现也是不同的,sleep因为是用alarm实现的,所以时间单位为s ,而usleep的时间单位为us
,那肯定不是由alarm实现的,所以说它们的实现不同,但都是linux用的,而window下不能用,因为都是sleep和usleep都是在unistd.h下定义的。
若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余微秒数。

int usleep (useconds_t useconds)
{
  struct timespec ts = { .tv_sec = (long int) (useconds / 1000000),
        .tv_nsec = (long int) (useconds % 1000000) * 1000ul };
  /* Note the usleep() is a cancellation point.  But since we call
     nanosleep() which itself is a cancellation point we do not have
     to do anything here.  */
  return __nanosleep (&ts, NULL);
}

 

3、nanosleep( )---------以纳秒为单位
struct timespec
{
time_t tv_sec; /* 秒seconds */
long tv_nsec; /* 纳秒nanoseconds */
};
int nanosleep(const struct timespec *req, struct timespec *rem); //#include<time.h>

这个函数功能是暂停某个进程直到你规定的时间后恢复,参数req就是你要暂停的时间,其中req->tv_sec是以秒为单位,而tv_nsec以毫微秒为单位(10的-9次方秒)。

由于调用nanosleep时进程进入TASK_INTERRUPTIBLE,这种状态是会相应信号而进入TASK_RUNNING状态的,这就意味着有可能会没有等到你规定的时间就因为其它信号而唤醒,此时函数返回-1,切还剩余的时间会被记录在rem中。

return: 若进程暂停到参数*req所指定的时间,成功则返回0,若有信号中断则返回-1,并且将剩余微秒数记录在*rem中。

 

long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,const enum hrtimer_mode mode, const clockid_t clockid)
{
    struct restart_block *restart;
    struct hrtimer_sleeper t;
    int ret = 0;
    unsigned long slack;
    slack = current->timer_slack_ns;
    if (rt_task(current))
        slack = 0;
    hrtimer_init_on_stack(&t.timer, clockid, mode);
    hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
    if (do_nanosleep(&t, mode))
        goto out;
    /* Absolute timers do not update the rmtp value and restart: */
    if (mode == HRTIMER_MODE_ABS) {
        ret = -ERESTARTNOHAND;
        goto out;
    }
    if (rmtp) {
        ret = update_rmtp(&t.timer, rmtp);
        if (ret <= 0)
            goto out;
    }
    restart = &current_thread_info()->restart_block;
    restart->fn = hrtimer_nanosleep_restart;
    restart->nanosleep.clockid = t.timer.base->clockid;
    restart->nanosleep.rmtp = rmtp;
    restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
    ret = -ERESTART_RESTARTBLOCK;
out:
    destroy_hrtimer_on_stack(&t.timer);
    return ret;
}
SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
        struct timespec __user *, rmtp)
{
    struct timespec tu;
    if (copy_from_user(&tu, rqtp, sizeof(tu)))
        return -EFAULT;
    if (!timespec_valid(&tu))
        return -EINVAL;
 
    return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);

 

目录
相关文章
|
1月前
|
Unix 程序员 编译器
C++ Sleep 函数
C++ Sleep 函数
|
3月前
|
监控 算法 Unix
Thread.sleep(0) 到底有什么用
Thread.sleep(0) 到底有什么用
24 1
|
8月前
|
Shell
sleep命令
sleep命令
34 1
|
9月前
sleep () 和 wait () 的区别
sleep () 和 wait () 的区别
42 0
|
10月前
|
Java 程序员
sleep 和 wait 的区别
Java 中,线程的 "sleep" 和 "wait" 方法区别
88 0
|
Java 调度 C++
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
|
调度 C++
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
|
消息中间件 安全 Java
|
Linux Windows
sleep
sleep
94 0
|
安全 Java 中间件
Thread.sleep(0)的作用
在源码中经常能看到sleep(0)的操作,今天来总结下sleep(0)的作用到底是啥
384 0