Process Priorities and Scheduling 745
Upon successful execution, sched_getscheduler() returns one of the policies
shown earlier in Table 35-1.
The program in Listing 35-3 uses sched_getscheduler() and sched_getparam() to
retrieve the policy and priority of all of the processes whose process IDs are given
as command-line arguments. The following shell session demonstrates the use of
this program, as well as the program in Listing 35-2:
$ su Assume privilege so we can set realtime policies
Password:
# sleep 100 & Create a process
[1] 2006
# ./sched_view 2006 View initial policy and priority of sleep process
2006: OTHER 0
# ./sched_set f 25 2006 Switch process to SCHED_FIFO policy, priority 25
# ./sched_view 2006 Verify change
2006: FIFO 25
Listing 35-3: Retrieving process scheduling policies and priorities
–––––––––––––––––––––––––––––––––––––––––––––––––––––– procpri/sched_view.c
#include <sched.h>
#include "tlpi_hdr.h"
int
main(int argc, char *argv[])
{
int j, pol;
struct sched_param sp;
for (j = 1; j < argc; j++) {
pol = sched_getscheduler(getLong(argv[j], 0, "pid"));
if (pol == -1)
errExit("sched_getscheduler");
if (sched_getparam(getLong(argv[j], 0, "pid"), &sp) == -1)
errExit("sched_getparam");
printf("%s: %-5s %2d\n", argv[j],
(pol == SCHED_OTHER)? "OTHER" :
(pol == SCHED_RR)? "RR" :
(pol == SCHED_FIFO)? "FIFO" :
#ifdef SCHED_BATCH / Linux-specific /
(pol == SCHED_BATCH)? "BATCH" :
#endif
#ifdef SCHED_IDLE / Linux-specific /
(pol == SCHED_IDLE)? "IDLE" :
#endif
"???", sp.sched_priority);
}
exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––– procpri/sched_view.c