1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define FOR_chrt
28#include "toys.h"
29
30GLOBALS(
31 long p;
32)
33
34#ifndef _POSIX_PRIORITY_SCHEDULING
35
36
37
38#define sched_get_priority_min(policy) \
39 (int)syscall(SYS_sched_get_priority_min, (int)policy)
40#define sched_get_priority_max(policy) \
41 (int)syscall(SYS_sched_get_priority_max, (int)policy)
42#define sched_getparam(pid, param) \
43 syscall(SYS_sched_getparam, (pid_t)pid, (void *)param)
44#define sched_getscheduler(pid) \
45 syscall(SYS_sched_getscheduler, (pid_t)pid)
46#define sched_setscheduler(pid, scheduler, param) \
47 syscall(SYS_sched_setscheduler, (pid_t)pid, (int)scheduler, (void *)param)
48#endif
49
50static char *polnames[] = {
51 "SCHED_OTHER", "SCHED_FIFO", "SCHED_RR", "SCHED_BATCH", 0, "SCHED_IDLE",
52 "SCHED_DEADLINE"
53};
54
55void chrt_main(void)
56{
57 int pol, pri;
58
59
60 if (toys.optflags&FLAG_m) {
61 for (pol = 0; pol<ARRAY_LEN(polnames); pol++) if (polnames[pol])
62 printf("%s min/max priority\t: %d/%d\n", polnames[pol],
63 sched_get_priority_min(pol), sched_get_priority_max(pol));
64
65 return;
66 }
67
68
69 if (toys.optflags==FLAG_p && !*toys.optargs) {
70 char *s = "???", *R = "";
71
72 if (-1==(pol = sched_getscheduler(TT.p))) perror_exit("pid %ld", TT.p);
73 if (pol & SCHED_RESET_ON_FORK) R = "|SCHED_RESET_ON_FORK";
74 if ((pol &= ~SCHED_RESET_ON_FORK)<ARRAY_LEN(polnames)) s = polnames[pol];
75 printf("pid %ld's current scheduling policy: %s%s\n", TT.p, s, R);
76
77 if (sched_getparam(TT.p, (void *)&pri)) perror_exit("sched_getparam");
78 printf("pid %ld's current scheduling priority: %d\n", TT.p, pri);
79
80 return;
81 }
82
83 if (!*toys.optargs) help_exit("no PRIORITY");
84 if (!toys.optargs[1] == !(toys.optflags&FLAG_p))
85 help_exit("need 1 of -p or COMMAND");
86
87
88 if (-1==(pol = highest_bit(toys.optflags&0x2f))) pol = SCHED_RR;
89 pri = atolx_range(*toys.optargs, sched_get_priority_min(pol),
90 sched_get_priority_max(pol));
91 if (toys.optflags&FLAG_R) pol |= SCHED_RESET_ON_FORK;
92
93 if (sched_setscheduler(TT.p, pol, (void *)&pri))
94 perror_exit("sched_setscheduler");
95
96 if (*(toys.optargs+1)) xexec(toys.optargs+1);
97}
98