2.2.7. What Is The Interface

As illustrated by the individual examples, the interface to the scheduler is mostly determined by the scheduler itself.

2.2.7.1. Example: Linux Scheduler API

Linux offers the old style scheduler interface where priority is a single integer, ranging roughly from -20 to +20. This used to be the nice value in the old style scheduler algorithm. Meaning in the new style scheduler algorithms varies. The interface allows setting the priority for a single process, all processes in a group, and all processes owned by a user. Note the interface design error where return value can be either legal priority or error code.

int getpriority (int which, int who);
int setpriority (int which, int who, int prio);

The old style scheduler interface is accompanied by a new style scheduler interface that allows changing scheduling type. The supported types are SCHED_OTHER, SCHED_BATCH and SCHED_IDLE for time sharing processes and SCHED_FIFO and SCHED_RR for processes with static priorities. For time sharing processes, the nice value set through the old style interface still applies. For processes with static priorities, the priority value can be set through the new style interface.

int sched_setscheduler (
  pid_t pid,
  int policy,
  const struct sched_param *param);
int sched_getscheduler (pid_t pid);

int sched_setparam (pid_t pid, const struct sched_param *param);
int sched_getparam (pid_t pid, struct sched_param *param);

struct sched_param
{
  ...
  int sched_priority;
  ...
};

int sched_yield (void);

A process can also be constrained to run only on certain processors.

int sched_setaffinity (
  pid_t pid,
  size_t cpusetsize,
  cpu_set_t *mask);
int sched_getaffinity (
  pid_t pid,
  size_t cpusetsize,
  cpu_set_t *mask);

2.2.7.2. Example: Windows Scheduler API

BOOL SetPriorityClass (
  HANDLE hProcess,
  DWORD dwPriorityClass);
DWORD GetPriorityClass (
  HANDLE hProcess);

BOOL SetThreadPriority (
  HANDLE hThread,
  int nPriority);
int GetThreadPriority (
  HANDLE hThread);

BOOL SetProcessPriorityBoost (
  HANDLE hProcess,
  BOOL DisablePriorityBoost);
BOOL SetThreadPriorityBoost (
  HANDLE hThread,
  BOOL DisablePriorityBoost);

BOOL SetProcessAffinityMask (
  HANDLE hProcess,
  DWORD_PTR dwProcessAffinityMask);
DWORD_PTR SetThreadAffinityMask (
  HANDLE hThread,
  DWORD_PTR dwThreadAffinityMask);

Windows also provides an interface that implements the thread pool scheduling pattern, where a pool of threads with predefined minimum and maximum size is used to handle incoming work requests.

Figure 2.12. Windows Thread Pool Calls

PTP_POOL CreateThreadpool (
  PVOID reserved);
VOID CloseThreadpool (
  PTP_POOL ptpp);

BOOL SetThreadpoolThreadMinimum (
  PTP_POOL ptpp,
  DWORD cthrdMic);
VOID SetThreadpoolThreadMaximum (
  PTP_POOL ptpp,
  DWORD cthrdMost);

VOID SubmitThreadpoolWork (
  PTP_WORK pwk);

It is also possible to query various information on process timing.

Figure 2.13. Windows Process Timing Call

BOOL GetProcessTimes (
  HANDLE hProcess,
  LPFILETIME lpCreationTime,
  LPFILETIME lpExitTime,
  LPFILETIME lpKernelTime,
  LPFILETIME lpUserTime);