#include <sys/types.h> #include <sys/priocntl.h> #include <sys/rtpriocntl.h> #include <sys/tspriocntl.h>
long priocntl(idtype_t idtype, id_t id, int cmd, /* arg */ ...);
LWPs fall into distinct classes with a separate scheduling policy applied to each class. The two classes currently supported are the real-time class and the time-sharing class. The characteristics of these classes are described under the corresponding headings below. The class attribute of an LWP is inherited across the fork(2) , exec(2) and _lwp_create(2) system calls. priocntl() can be used to dynamically change the class and other scheduling parameters associated with a running LWP or set of LWPs given the appropriate permissions as explained below.
In the default configuration, a runnable real-time LWP runs before any other LWP. Therefore, inappropriate use of real-time LWP can have a dramatic negative impact on system performance.
priocntl() provides an interface for specifying a process, set of processes or an LWP to which the function is to apply. The priocntlset(2) system call provides the same functions as priocntl(), but allows a more general interface for specifying the set of LWPs to which the function is to apply.
For priocntl(), the idtype and id arguments are used together to specify the set of LWPs. The interpretation of id depends on the value of idtype. The possible values for idtype and corresponding interpretations of id are as follows:
An id value of P_MYID can be used in conjunction with the idtype value to specify the calling LWP’s LWP ID , parent process ID , process group ID , session ID , class ID , user ID , or group ID .
In order to change the scheduling parameters of an LWP (using the PC_SETPARMS command as explained below) the real or effective user ID of the LWP calling priocntl() must match the real or effective user ID of the receiving LWP or the effective user ID of the calling LWP must be super-user. These are the minimum permission requirements enforced for all classes. An individual class may impose additional permissions requirements when setting LWPs to that class and/or when setting class-specific scheduling parameters.
A special sys scheduling class exists for the purpose of scheduling the execution of certain special system processes (such as the swapper process). It is not possible to change the class of any LWP to sys. In addition, any processes in the sys class that are included in a specified set of processes are disregarded by priocntl(). For example, an idtype of P_UID and an id value of zero would specify all processes with a user ID of zero except processes in the sys class and (if changing the parameters using PC_SETPARMS) the init(1M) process.
The init process is a special case. In order for a priocntl() call to change the class or other scheduling parameters of the init process (process ID 1), it must be the only process specified by idtype and id. The init process may be assigned to any class configured on the system, but the time-sharing class is almost always the appropriate choice. (Other choices may be highly undesirable; see the for more information.)
The data type and value of arg are specific to the type of command specified by cmd.
A structure with the following members is used by the PC_GETCID and PC_GETCLINFO commands.
id_t pc_cid; /* Class id */ char pc_clname[PC_CLNMSZ]; /* Class name */ long pc_clinfo[PC_CLINFOSZ]; /* Class information */
pc_cid is a class ID returned by priocntl() PC_GETCID. pc_clname is a buffer of size PC_CLNMSZ (defined in <sys/priocntl.h>) used to hold the class name (RT for real-time or TS for time-sharing).
pc_clinfo is a buffer of size PC_CLINFOSZ
(defined in <sys/priocntl.h>) used to return data describing the attributes
of a specific class. The format of this data is class-specific and is described
under the appropriate heading (REAL-TIME CLASS
or TIME-SHARING CLASS
) below.
A structure with the following elements is used by the PC_SETPARMS and PC_GETPARMS commands.
id_t pc_cid; /* LWP class */ long pc_clparms[PC_CLPARMSZ]; /* Class-specific params */
pc_cid is a class ID (returned by priocntl() PC_GETCID). The special class ID PC_CLNULL can also be assigned to pc_cid when using the PC_GETPARMS command as explained below.
The pc_clparms buffer holds class-specific scheduling parameters. The format of this parameter data for a particular class is described under the appropriate heading below. PC_CLPARMSZ is the length of the pc_clparms buffer and is defined in <sys/priocntl.h>.
The real-time class has a range of real-time priority (rt_pri) values that may be assigned to an LWP within the class. Real-time priorities range from 0 to x, where the value of x is configurable and can be determined for a specific installation by using the priocntl() PC_GETCID or PC_GETCLINFO command.
The real-time scheduling policy is a fixed priority policy. The scheduling priority of a real-time LWP is never changed except as the result of an explicit request by the user/application to change the rt_pri value of the LWP.
For an LWP in the real-time class, the rt_pri value is, for all practical purposes, equivalent to the scheduling priority of the LWP. The rt_pri value completely determines the scheduling priority of a real-time LWP relative to other LWPs within its class. Numerically higher rt_pri values represent higher priorities. Since the real-time class controls the highest range of scheduling priorities in the system it is guaranteed that the runnable real-time LWP with the highest rt_pri value is always selected to run before any other LWPs in the system.
In addition to providing control over priority, priocntl() provides for control over the length of the time quantum allotted to the LWP in the real-time class. The time quantum value specifies the maximum amount of time an LWP may run assuming that it does not complete or enter a resource or event wait state (sleep). Note that if another LWP becomes runnable at a higher priority, the currently running LWP may be preempted before receiving its full time quantum.
The system’s process scheduler keeps the runnable real-time LWPs on a set of scheduling queues. There is a separate queue for each configured real-time priority and all real-time LWPs with a given rt_pri value are kept together on the appropriate queue. The LWPs on a given queue are ordered in FIFO order (that is, the LWP at the front of the queue has been waiting longest for service and receives the CPU first). Real-time LWPs that wake up after sleeping, LWPs which change to the real-time class from some other class, LWPs which have used their full time quantum, and runnable LWPs whose priority is reset by priocntl() are all placed at the back of the appropriate queue for their priority. An LWP that is preempted by a higher priority LWP remains at the front of the queue (with whatever time is remaining in its time quantum) and runs before any other LWP at this priority. Following a fork(2) or _lwp_create(2) system call by a real-time LWP, the parent LWP continues to run while the child LWP (which inherits its parent’s rt_pri value) is placed at the back of the queue.
A structure with the following members (defined in <sys/rtpriocntl.h>) defines the format used for the attribute data for the real-time class.
short rt_maxpri; /* Maximum real-time priority */
The priocntl() PC_GETCID and PC_GETCLINFO commands return real-time class attributes in the pc_clinfo buffer in this format.
rt_maxpri specifies the configured maximum rt_pri value for the real-time class (if rt_maxpri is x, the valid real-time priorities range from 0 to x).
A structure with the following members (defined in <sys/rtpriocntl.h>) defines the format used to specify the real-time class-specific scheduling parameters of an LWP.
short rt_pri; /* Real-Time priority */ ulong rt_tqsecs; /* Seconds in time quantum */ long rt_tqnsecs; /* Additional nanoseconds in quantum */
When using the priocntl() PC_SETPARMS or PC_GETPARMS commands, if pc_cid specifies the real-time class, the data in the pc_clparms buffer is in this format.
The above commands can be used to set the real-time priority to the specified value or get the current rt_pri value. Setting the rt_pri value of an LWP that is currently running or runnable (not sleeping) causes the LWP to be placed at the back of the scheduling queue for the specified priority. The LWP is placed at the back of the appropriate queue regardless of whether the priority being set is different from the previous rt_pri value of the LWP. Note that a running LWP can voluntarily release the CPU and go to the back of the scheduling queue at the same priority by resetting its rt_pri value to its current real-time priority value. In order to change the time quantum of an LWP without setting the priority or affecting the LWP’s position on the queue, the rt_pri field should be set to the special value RT_NOCHANGE (defined in <sys/rtpriocntl.h>). Specifying RT_NOCHANGE when changing the class of an LWP to real-time from some other class results in the real-time priority being set to zero.
For the priocntl() PC_GETPARMS command, if pc_cid specifies the real-time class and more than one real-time LWP is specified, the scheduling parameters of the real-time LWP with the highest rt_pri value among the specified LWPs are returned and the LWP ID of this LWP is returned by the priocntl() call. If there is more than one LWP sharing the highest priority, the one returned is implementation-dependent.
The rt_tqsecs and rt_tqnsecs fields are used for getting or setting the time quantum associated with an LWP or group of LWPs. rt_tqsecs is the number of seconds in the time quantum and rt_tqnsecs is the number of additional nanoseconds in the quantum. For example setting rt_tqsecs to 2 and rt_tqnsecs to 500,000,000 (decimal) would result in a time quantum of two and one-half seconds. Specifying a value of 1,000,000,000 or greater in the rt_tqnsecs field results in an error return with errno set to EINVAL . Although the resolution of the tq_nsecs field is very fine, the specified time quantum length is rounded up by the system to the next integral multiple of the system clock’s resolution. The maximum time quantum that can be specified is implementation-specific and equal to LONG_MAX ticks (defined in <limits.h>). Requesting a quantum greater than this maximum results in an error return with errno set to ERANGE (although infinite quantums may be requested using a special value as explained below). Requesting a time quantum of zero (setting both rt_tqsecs and rt_tqnsecs to 0) results in an error return with errno set to EINVAL .
The rt_tqnsecs field can also be set to one of the following special values (defined in <sys/rtpriocntl.h>), in which case the value of rt_tqsecs is ignored.
- RT_TQINF
- Set an infinite time quantum.
- RT_TQDEF
- Set the time quantum to the default for this priority (see rt_dptbl(4) ).
- RT_NOCHANGE
- Do nott set the time quantum. This value is useful when you wish to change the real-time priority of an LWP without affecting the time quantum. Specifying this value when changing the class of an LWP to real-time from some other class is equivalent to specifying RT_TQDEF.
In order to change the class of an LWP to real-time (from any other class) the LWP invoking priocntl() must have super-user privileges. In order to change the priority or time quantum setting of a real-time LWP, the LWP invoking priocntl() must have super-user privileges or must itself be a real-time LWP whose real or effective user ID matches the real of effective user ID of the target LWP.
The real-time priority and time quantum are inherited across the fork(2) and exec(2) system calls.
The time-sharing class has a range of time-sharing user priority (see ts_upri below) values that may be assigned to LWPs within the class. A ts_upri value of zero is defined as the default base priority for the time-sharing class. User priorities range from -x to +x where the value of x is configurable and can be determined for a specific installation by using the priocntl() PC_GETCID or PC_GETCLINFO command.
The purpose of the user priority is to provide some degree of user/application control over the scheduling of LWPs in the time-sharing class. Raising or lowering the ts_upri value of an LWP in the time-sharing class raises or lowers the scheduling priority of the LWP. It is not guaranteed, however, that an LWP with a higher ts_upri value will run before one with a lower ts_upri value. This is because the ts_upri value is just one factor used to determine the scheduling priority of a time-sharing LWP. The system may dynamically adjust the internal scheduling priority of a time-sharing LWP based on other factors such as recent CPU usage.
In addition to the system-wide limits on user priority (returned by the PC_GETCID and PC_GETCLINFO commands) there is a per LWP user priority limit (see ts_uprilim below), which specifies the maximum ts_upri value that may be set for a given LWP; by default, ts_uprilim is zero.
A structure with the following members (defined in <sys/tspriocntl.h>) defines
the format used for the attribute data for the time-sharing class.
short ts_maxupri; /* Limits of user priority range */
The priocntl() PC_GETCID and PC_GETCLINFO commands return time-sharing class attributes in the pc_clinfo buffer in this format.
ts_maxupri specifies the configured maximum user priority value for the time-sharing class. If ts_maxupri is x, the valid range for both user priorities and user priority limits is from -x to +x.
A structure with the following members (defined in <sys/tspriocntl.h>) defines the format used to specify the time-sharing class-specific scheduling parameters of an LWP.
short ts_uprilim; /* Time-Sharing user priority limit */ short ts_upri; /* Time-Sharing user priority */
When using the priocntl() PC_SETPARMS or PC_GETPARMS commands, if pc_cid specifies the time-sharing class, the data in the pc_clparms buffer is in this format.
For the priocntl() PC_GETPARMS command, if pc_cid specifies the time-sharing class and more than one time-sharing LWP is specified, the scheduling parameters of the time-sharing LWP with the highest ts_upri value among the specified LWPs is returned and the LWP ID of this LWP is returned by the priocntl() call. If there is more than one LWP sharing the highest user priority, the one returned is implementation-dependent.
Any time-sharing LWP may lower its own ts_uprilim (or that of another LWP with the same user ID ). Only a time-sharing LWP with super-user privileges may raise a ts_uprilim. When changing the class of an LWP to time-sharing from some other class, super-user privileges are required in order to set the initial ts_uprilim to a value greater than zero. Attempts by a non-super-user LWP to raise a ts_uprilim or set an initial ts_uprilim greater than zero fail with a return value of -1 and errno set to EPERM .
Any time-sharing LWP may set its own ts_upri (or that of another LWP with the same user ID ) to any value less than or equal to the LWP’s ts_uprilim. Attempts to set the ts_upri above the ts_uprilim (and/or set the ts_uprilim below the ts_upri) result in the ts_upri being set equal to the ts_uprilim.
Either of the ts_uprilim or ts_upri fields may be set to the special value TS_NOCHANGE (defined in <sys/tspriocntl.h>) in order to set one of the values without affecting the other. Specifying TS_NOCHANGE for the ts_upri when the ts_uprilim is being set to a value below the current ts_upri causes the ts_upri to be set equal to the ts_uprilim being set. Specifying TS_NOCHANGE for a parameter when changing the class of an LWP to time-sharing (from some other class) causes the parameter to be set to a default value. The default value for the ts_uprilim is 0 and the default for the ts_upri is to set it equal to the ts_uprilim which is being set.
The time-sharing user priority and user priority limit are inherited across the fork and exec functions.