Linux kernel system calls Nanosleep and pause ()

Source: Internet
Author: User

Nanosleep () causes the process to go to sleep, specifying the time to wake the process, and sleep () based on its implementation
  
 
  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 () Implementation, only the signal can wake up
  
 
  1. asmlinkage int sys_pause(void)
  2. {
  3. current->state = TASK_INTERRUPTIBLE;//设置为睡眠状态
  4. schedule();//schdule运行调度
  5. return -ERESTARTNOHAND;
  6. }



From for notes (Wiz)

Linux kernel system calls Nanosleep and pause ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.