02_task_scheduling.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. Task Scheduling Interfaces
  2. **************************
  3. By default, NuttX performs strict priority scheduling: Tasks of higher
  4. priority have exclusive access to the CPU until they become blocked. At
  5. that time, the CPU is available to tasks of lower priority. Tasks of
  6. equal priority are scheduled FIFO.
  7. Optionally, a NuttX task or thread can be configured with round-robin or
  8. *sporadic* scheduler. The round-robin is similar to priority scheduling
  9. *except* that tasks with equal priority and share CPU time via
  10. *time-slicing*. The time-slice interval is a constant determined by the
  11. configuration setting ``CONFIG_RR_INTERVAL`` to a positive, non-zero
  12. value. Sporadic scheduling scheduling is more complex, varying the
  13. priority of a thread over a *replenishment* period. Support for sporadic
  14. scheduling is enabled by the configuration option
  15. ``CONFIG_SCHED_SPORADIC``.
  16. The OS interfaces described in the following paragraphs provide a POSIX-
  17. compliant interface to the NuttX scheduler:
  18. - :c:func:`sched_setparam`
  19. - :c:func:`sched_getparam`
  20. - :c:func:`sched_setscheduler`
  21. - :c:func:`sched_getscheduler`
  22. - :c:func:`sched_yield`
  23. - :c:func:`sched_get_priority_max`
  24. - :c:func:`sched_get_priority_min`
  25. - :c:func:`sched_get_rr_interval`
  26. Functions
  27. ---------
  28. .. c:function:: int sched_setparam(pid_t pid, FAR const struct sched_param *param)
  29. This function sets the priority of the task specified
  30. by pid input parameter.
  31. :param pid: The task ID of the task. If ``pid`` is zero, the priority of
  32. the calling task is set.
  33. :param param: A structure whose member ``sched_priority`` is the integer
  34. priority. The range of valid priority numbers is from
  35. ``SCHED_PRIORITY_MIN`` through ``SCHED_PRIORITY_MAX``.
  36. :return: On success, sched_setparam() returns 0 (``OK``). On
  37. error, -1 (``ERROR``) is returned, and ```errno`` <#ErrnoAccess>`__ is
  38. set appropriately.
  39. - ``EINVAL``. The parameter ``param`` is invalid or does not make sense
  40. for the current scheduling policy.
  41. - ``EPERM``. The calling task does not have appropriate privileges.
  42. - ``ESRCH``. The task whose ID is ``pid`` could not be found.
  43. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  44. name. Differences from the full POSIX implementation include:
  45. - The range of priority values for the POSIX call is 0 to 255. The
  46. priority 0 is the lowest priority and 255 is the highest priority.
  47. .. note:: Setting a task's priority to the same value has the similar effect
  48. to ``sched_yield()``: The task will be moved to after all other tasks
  49. with the same priority.
  50. .. c:function:: int sched_getparam(pid_t pid, FAR struct sched_param *param)
  51. This function gets the scheduling priority of the task
  52. specified by pid.
  53. :param pid: The task ID of the task. If pid is zero, the priority of the
  54. calling task is returned.
  55. :param param: A structure whose member ``sched_priority`` is the integer
  56. priority. The task's priority is copied to the ``sched_priority``
  57. element of this structure.
  58. :return: 0 (``OK``) if successful, otherwise -1 (``ERROR``).
  59. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  60. name.
  61. .. c:function:: int sched_setscheduler (pid_t pid, int policy, const struct sched_param *param)
  62. ``sched_setscheduler()`` sets both the scheduling
  63. policy and the priority for the task identified by ``pid``. If ``pid``
  64. equals zero, the scheduler of the calling thread will be set. The
  65. parameter ``param`` holds the priority of the thread under the new
  66. policy.
  67. :param pid: The task ID of the task. If ``pid`` is zero, the priority of
  68. the calling task is set.
  69. :param policy: Scheduling policy requested (either ``SCHED_FIFO`` or
  70. ``SCHED_RR``).
  71. :param param: A structure whose member ``sched_priority`` is the integer
  72. priority. The range of valid priority numbers is from
  73. ``SCHED_PRIORITY_MIN`` through ``SCHED_PRIORITY_MAX``.
  74. :return: On success, ``sched_setscheduler()`` returns ``OK``
  75. (zero). On error, ``ERROR`` (-1) is returned, and
  76. ``errno`` is set appropriately:
  77. - ``EINVAL``: The scheduling ``policy`` is not one of the recognized
  78. policies.
  79. - ``ESRCH``: The task whose ID is ``pid`` could not be found.
  80. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  81. name.
  82. .. c:function:: int sched_getscheduler (pid_t pid)
  83. ``sched_getscheduler()`` returns the scheduling policy
  84. currently applied to the task identified by ``pid``. If ``pid`` equals
  85. zero, the policy of the calling process will be retrieved.
  86. :param pid: The task ID of the task to query. If ``pid`` is zero, the
  87. calling task is queried.
  88. :return: On success, ``sched_getscheduler()`` returns the policy for the task
  89. (either ``SCHED_FIFO`` or ``SCHED_RR``). On error, ``ERROR`` (-1) is
  90. returned, and ``errno`` is set appropriately:
  91. - ``ESRCH``: The task whose ID is pid could not be found.
  92. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  93. name.
  94. .. c:function:: int sched_yield(void)
  95. This function forces the calling task to give up the
  96. CPU (only to other tasks at the same priority).
  97. :return: 0 (``OK``) or -1 (``ERROR``)
  98. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  99. name.
  100. .. c:function:: int sched_get_priority_max (int policy)
  101. This function returns the value of the highest possible
  102. task priority for a specified scheduling policy.
  103. :param policy: Scheduling policy requested.
  104. :return: The maximum priority value or -1 (``ERROR``).
  105. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  106. name.
  107. .. c:function:: int sched_get_priority_min (int policy)
  108. This function returns the value of the lowest possible
  109. task priority for a specified scheduling policy.
  110. :param policy: Scheduling policy requested.
  111. :return: The minimum priority value or -1 (``ERROR``)
  112. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  113. name.
  114. .. c:function:: int sched_get_rr_interval (pid_t pid, struct timespec *interval)
  115. ``sched_rr_get_interval()`` writes the timeslice
  116. interval for task identified by ``pid`` into the timespec structure
  117. pointed to by ``interval``. If pid is zero, the timeslice for the
  118. calling process is written into 'interval. The identified process should
  119. be running under the SCHED_RR scheduling policy.'
  120. :param pid: The task ID of the task. If pid is zero, the priority of the
  121. calling task is returned.
  122. :param interval: A structure used to return the time slice.
  123. :return: On success, sched_rr_get_interval() returns OK (0).
  124. On error, ERROR (-1) is returned, and ``errno`` is
  125. set to:
  126. - ``EFAULT``: Cannot copy to interval
  127. - ``EINVAL``: Invalid pid.
  128. - ``ENOSYS``: The system call is not yet implemented.
  129. - ``ESRCH``: The process whose ID is pid could not be found.
  130. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  131. name.