Unix to Windows Porting Dictionary for HPC |
|
|
RSS
LinksFunction List
|
Table of Contents The priority of a process determines how much CPU time it receives, when it will receive its allocated time and the order it will run in relation to other processes. The method of expressing a process priority is different between Unix and Windows. Unix process priority is traditionally called a "nice value". Nice values range from -20 (highest) to +20 (lowest) with the typical default nice value being 0 (zero). Most Windows users are used to a descriptive label such as "High" or "Normal" but the Windows priority is actually a numeric value. Numeric value range from 0 (lowest) to 31 (highest). Typically Windows processes are grouped into priority classes. Each priority class then has several priority levels that correspond to actual priority values. Extreme care must be taken if you decide to use the high priority or real-time priority classes because of the severe effects on the overall system operation that can happen. The GetPriorityClass() function returns priority classes rather than priority levels directly. The Unix nice value of 0 (zero) maps to the Windows priority value of 8 (Normal priority class, thread priority normal). Note that a higher numeric value with Windows denotes a higher priority while Unix denotes this as a lower priority. Further Windows priorities do not deal with negative values. The Unix function getpriority() has the unusual situation that it can return the value 0 (zero) as a priority level and as an error indicator. To distinguish between these two results the variable 'errno' must be set to EZERO before the call is made and then 'errno' tested after. This situation does not occur with Windows GetPriorityClass() since none of the priority classes returned equal zero. As such the code can be simplified when written for Windows as shown in the example below. The five possible Windows priority classes are:
#include <math_u.h>
#ifdef defined(_MSC_VER) && !defined(__INTERIX)
# define MY_LOW_PRIORITY BELOW_NORMAL_PRIORITY_CLASS
# define MY_NORMAL_PRIORITY NORMAL_PRIORITY_CLASS
# define MY_HIGH_PRIORITY ABOVE_NORMAL_PRIORITY_CLASS
#else /* _MSC_VER && !__INTERIX */
# define MY_LOW_PRIORITY 3
# define MY_NORMAL_PRIORITY 0
# define MY_HIGH_PRIORITY -2
#endif /* _MSC_VER && !__INTERIX */
#ifdef defined(_MSC_VER) && !defined(__INTERIX)
myprio = GetPriorityClass(GetCurrentProcess());
if (myprio == 0) {
printf("error: could not get priority\n");
}
#else /* _MSC_VER && !__INTERIX */
errno = EZERO;
myprio = getpriority(PRIO_PROCESS, 0);
if (myprio == MY_NORMAL_PRIORITY && errno == EZERO) {
printf("info: process priority is normal\n");
}
else (myprio == 0) {
printf(error: could not get priority\n");
}
#endif /* _MSC_VER && !__INTERIX */
|
|
|
|