TOC PREV NEXT INDEX

POSIX 1.c Migration Guide


Thread Scheduling

Thread Creation Scheduling Attributes

The thread creation scheduling attributes functions and definitions have changed considerably in POSIX.1c. The following table is a comparison of the revised interfaces and their corresponding POSIX.4a versions:

Thread Creation Scheduling Interface Changes  
POSIX.1c
POSIX.4a
int
pthread_attr_setscope
(pthread_attr_t *attr,
int contentionscope)
No equivalent
int
pthread_attr_getscope
(pthread_attr_t *attr,
int *contentionscope)
No equivalent
int
pthread_attr_setinheritsched
(pthread_attr_t *attr,
int inheritsched);
int
pthread_attr_setinheritsched
(pthread_attr_t attr)
int
pthread_attr_getinheritsched
(pthread_attr_t *attr,
int *inheritsched)
int
pthread_attr_getinheritsched
(pthread_attr_t attr)
int
pthread_attr_setschedpolicy
(pthread_attr_t *attr,int policy)
int
pthread_attr_setsched
(pthread_attr_t *attr,int
scheduler)
int
pthread_attr_getschedpolicy
(pthread_attr_t *attr,int
*policy)
int
pthread_attr_getsched
(pthread_attr_t attr)
int
pthread_attr_setschedparam
(pthread_attr_t *attr,const struct
sched_param *param)
int
pthread_attr_setprio
(pthread_attr_t *attr,
int priority)
int
pthread_attr_getschedparam
(pthread_attr_t *attr,
struct sched_param *param)
int
pthread_attr_getprio
(pthread_attr_t attr)
PTHREAD_SCOPE_SYSTEM
No equivalent
PTHREAD_SCOPE_PROCESS
No equivalent
PTHREAD_EXPLICIT_SCHED
PTHREAD_DEFAULT_SCHED
sched_get_priority_min()
PRIO_???_MIN macros
sched_get_priority_max()
PRIO_???_MAX macros

POSIX.4a does not directly support the concept of thread scheduling scope. This facility is provided in POSIX.1c by the pthread_attr_setscope() and pthread_attr_getscope() functions, and the associated PTHREAD_SCOPE_SYSTEM and PTHREAD_SCOPE_PROCESS symbolic constants.

Note: LynxOS supports only the default system thread scheduling scope (PTHREAD_SCOPE_SYSTEM). Attempts to set the scheduling scope to PTHREAD_SCOPE_PROCESS returns an ENOTSUP error.

The scheduling policy values (SCHED_FIFO, SCHED_RR, SCHED_OTHER)
are unchanged.

In addition to these scheduling policies, LynxOS implements non-preemptible scheduling policy SCHED_NONPREEMPT. See "Non-Preemptible Scheduling Policy" for a detailed description of this policy.

In POSIX.1c, a thread's scheduling priority attribute is set or retrieved using a sched_param structure, instead of specifying or retrieving it directly.

POSIX.4a uses the POSIX.4 Draft 9 scheduling minimum and maximum priority macros, such as PRIO_RR_MIN or PRIO_OTHER_MAX, for thread priority limits for each scheduling policy. These are replaced in POSIX.1c with the use of the POSIX.1b scheduling parameter limits retrieval functions as shown in the above table (see ISO/IEC 9945-1 1996 Section 13.3.6).

The following code fragments illustrate the differences in manipulating thread priority attributes between POSIX.4a and POSIX.1c:

POSIX.4a Code

main()
{
   int rv;
   int prio;
   pthread_attr_t attr;
   :
   /* Set the initial priority for the new thread */
   prio = PRIO_OTHER_MIN + 10;
   rv = pthread_attr_setprio(&attr, prio);
   :
}

Equivalent POSIX.1c Code

main()
{
   int rv;
   int prio;
   struct sched_param param;
   pthread_attr_t attr;
   :
   /* Set the initial priority for the new thread */
   prio = sched_get_priority_min(SCHED_OTHER);
   param.sched_priority = prio + 10;
   rv = pthread_attr_setschedparam(&attr, &param);
   :
}

Non-Preemptible Scheduling Policy

LynxOS implements a non-preemptible scheduling policy called SCHED_NONPREEMPT. A process running under this policy cannot be preempted by any other process until it voluntarily sleeps or blocks waiting for a semaphore or mutex object. An example of this policy is a garbage collector running with low priority and performing critical work so that it must not be interrupted. The sample code of the application is as follows:

void garbage_collector(arg)
void *arg;
{
...
while (1) { /* endless loop */
if (waste_ratio() > max_waste_ratio) {
/* garbage collection, critical area
*/
...
}
sleep(GC_TIMEOUT);
} /* end of loop */
}

main()
{
pthread_attr_t attr;
struct sched_param prio;
pthread_t tid;
...
pthread_attr_create(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_NONPREEMPT);
prio.sched_priority = PRIO_NONPREEMPT_MIN;
pthread_attr_setschedparam(&attr, &prio);
pthread_create(&tid, &attr, garbage_collector, NULL);
...
}

LynxOS also defines 2 constants for the SCHED_NONPREEMPT policy designating the maximum and minimum priorities for this policy. These constants are:

These priorities can also be obtained using the sched_get_priority_max() and sched_get_priority_min() functions.

Dynamic Thread Scheduling Parameters Access

The POSIX.1c thread scheduling parameter access functions are not one-to-one replacements for their POSIX.4a counterparts, as shown in the following table:

Thread Scheduling Parameter Access Function Changes  
POSIX.1c
POSIX.4a
int
pthread_setschedparam
(pthread_t thread,int policy,const
struct sched_param *param)
int
pthread_setscheduler
(pthread_t thread, int alg,int
prio)
int
pthread_setprio
(pthread_t thread, int prio)
int
pthread_getschedparam
(pthread_t thread,int
*policy,struct sched_param
*param)
int
pthread_getscheduler
(pthread_t thread)
int
pthread_getprio pthread_t thread)

Each of the POSIX.1c functions combines the functionality of two corresponding POSIX.4a functions. The following code fragments illustrate the usage differences:

POSIX.4a Code

main ()
{
   int rc;
   pthread_t thread;
   int prio;
   :
   /* Set the policy and priority of the thread */
   prio = PRIO_FIFO_MIN + 10;
   rc = pthread_setscheduler(thread, SCHED_FIFO, prio);
   :
   /* Now change only the thread's priority */
   prio = pthread_getprio(thread);
   rc = pthread_setprio(thread, prio + 5);
   :
}

POSIX.1c Code

main()
{
   int rc;
   pthread_t thread;
   int policy = SCHED_FIFO;
   struct sched_param param;
   :
   /* Set the policy and priority of the thread */
   prio = sched_get_priority_min(policy);
   param.sched_priority = prio + 10;
   rc = pthread_setschedparam(thread, policy, &param);
   :
   /* Now change only the thread's priority */
   rc = pthread_getschedparam(thread, &policy, &param);
   param_sched_priority += 5;
   rc = pthread_setschedparam(thread, policy, &param);
   :
}

As in "Thread Creation Scheduling Attributes", the use of the POSIX.4 Draft 9 scheduling minimum and maximum priority macros in POSIX.4a are replaced by the POSIX.1b scheduling parameter limits retrieval functions
in POSIX.1c.

Relinquishing the Processor

The POSIX.4a pthread_yield() function has been replaced in POSIX.1c by the POSIX.1b sched_yield() function.

Calls to sched_yield() on LynxOS always return 0.



LynuxWorks, Inc.
855 Branham Lane East
San Jose, CA 95138
http://www.lynuxworks.com
1.800.255.5969
TOC PREV NEXT INDEX