[Previous]
[Contents]
[Next]

sched_yield()

yield to other READY processes at the same priority

Synopsis:

#include <sys/sched.h>
int sched_yield( void );

Description:

The sched_yield() function checks to see if other processes at the same priority as that of the calling process are READY to run. If so, the calling process yields to them and places itself at the end of the READY process queue. The sched_yield() function never yields to a lower priority process.

A higher priority process always forces a lower priority process to yield (that is, the higher priority process preempts the lower priority process) the instant the higher priority process becomes READY to run, without the need for the lower priority process to give up the processor by calling the sched_yield() or Yield() functions.

The sched_yield() function calls the kernel function Yield(), and may be more portable across realtime POSIX systems.


Note: You should avoid designing programs that contain busy wait loops. If it's unavoidable, sched_yield() can be used to reduce the system load at a given priority level. Note that a program that calls sched_yield() in a tight loop will spend a great deal of time in the kernel, which will have a small effect on interrupt latency.

Returns:

This function always succeeds and returns zero.

Examples:

#include <stdio.h>
#include <sys/sched.h>

void main()
  {
    int i;

    for( ;; ) {
      /* Process something... */
      for( i = 0 ; i < 1000 ; ++i )
    fun();

      /* Yield to anyone else at the same priority */
      sched_yield();
    }
  }

int fun()
  {
    int i;

    for( i = 0 ; i < 10 ; ++i )
      i += i;

    return( i );
  }

Classification:

POSIX 1003.4

Safety:
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

getprio(), qnx_scheduler(), sched_getparam(), sched_getscheduler(), sched_setparam(), sched_setscheduler(), setprio(), sleep(), Yield()


[Previous]
[Contents]
[Next]