zoukankan      html  css  js  c++  java
  • linux内核之系统调用nanosleep与pause()

    nanosleep()使得进程进入睡眠状态,指定时候后唤醒进程,sleep()基于其实现
    1. asmlinkage long sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)//第一个指针rqtp指向给定所需睡眠时间的数据结构;第二个指针rmtp,指向返回剩余时间的数据结构,可能收到信号提前唤醒.
    2. {
    3. struct timespec t;//tv_sec单位秒,tv_nsec单位好微妙
    4. unsigned long expire;
    5. if(copy_from_user(&t, rqtp, sizeof(struct timespec)))//所需睡眠时间从用户空间复制到内核空间
    6. return -EFAULT;
    7. if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 || t.tv_sec < 0)
    8. return -EINVAL;
    9. if (t.tv_sec == 0 && t.tv_nsec <= 2000000L &&
    10. current->policy != SCHED_OTHER)//由于时钟中断只能达到10毫秒的精度,如果要求睡眠的时间小于2毫秒,而要求睡眠的进程又是个实时要求的进程,那就不能真的让这个进程进入睡眠,因为那样有可能10毫秒以后才能将其唤醒,对于实时进程来说是不能接受的
    11. {
    12. /*
    13. * Short delay requests up to 2 ms will be handled with
    14. * high precision by a busy wait for all real-time processes.
    15. *
    16. * Its important on SMP not to do this holding locks.
    17. */
    18. udelay((t.tv_nsec + 999) / 1000);//延迟两秒
    19. return 0;
    20. }
    21. expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);//将数据结构t中的数值换算成时钟中断的次数<span style="white-space:pre"> </span>
    22. current->state = TASK_INTERRUPTIBLE;//将当期进程的状态设置为TASK_INTERRUPTIBLE
    23. expire = schedule_timeout(expire);//让当期进程睡眠给定的时间;返回剩余的时钟中断次数,如果没有,返回0
    24. if (expire) {
    25. if (rmtp) {
    26. jiffies_to_timespec(expire, &t);//剩余的时钟中断次数转换成数据结构t的数值
    27. if (copy_to_user(rmtp, &t, sizeof(struct timespec)))//剩余时间从内核空间复制到用户空间
    28. return -EFAULT;
    29. }
    30. return -EINTR;
    31. }
    32. return 0;
    33. }


    pause()实现,只有信号才可以唤醒
    1. asmlinkage int sys_pause(void)
    2. {
    3. current->state = TASK_INTERRUPTIBLE;//设置为睡眠状态
    4. schedule();//schdule运行调度
    5. return -ERESTARTNOHAND;
    6. }





  • 相关阅读:
    rdesktop ERROR: CredSSP: Initialize failed, do you have correct kerberos tgt initialized ? Failed to connect, CredSSP required by server
    nginx 服务器重启命令,关闭
    Libvirt磁盘加密
    qemu-nbd使用教程
    交叉编译Spice-gtk
    通过Python调用Spice-gtk
    远程桌面连接KVM虚拟机
    虚拟创建失败之Dbus调试
    Libvirt代码架构
    Libvirt外部快照
  • 原文地址:https://www.cnblogs.com/zengyiwen/p/f2e1c4b5f9904a4270e302a13851c2f9.html
Copyright © 2011-2022 走看看