• sleep


    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);
  • 相关阅读:
    set集合
    字典的使用与操作
    列表和字符串
    bzoj 4927: 第一题
    bzoj 3031: 理科男
    bzoj2263: Pku3889 Fractal Streets
    bzoj 1288: Neighbours
    bzoj2026: [SHOI2009]Coin
    bzoj3326: [Scoi2013]数数
    bzoj1494: [NOI2007]生成树计数
  • 原文地址:https://www.cnblogs.com/soul-stone/p/7955631.html
Copyright © 2020-2023  润新知