1 /* vi: set sw=4 ts=4: */
3 * chrt - manipulate real-time attributes of a process
4 * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //usage:#define chrt_trivial_usage
10 //usage: "[-prfom] [PRIO] [PID | PROG ARGS]"
11 //usage:#define chrt_full_usage "\n\n"
12 //usage: "Change scheduling priority and class for a process\n"
13 //usage: "\n -p Operate on PID"
14 //usage: "\n -r Set SCHED_RR class"
15 //usage: "\n -f Set SCHED_FIFO class"
16 //usage: "\n -o Set SCHED_OTHER class"
17 //usage: "\n -m Show min/max priorities"
19 //usage:#define chrt_example_usage
20 //usage: "$ chrt -r 4 sleep 900; x=$!\n"
21 //usage: "$ chrt -f -p 3 $x\n"
22 //usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
26 #ifndef _POSIX_PRIORITY_SCHEDULING
27 #warning your system may be foobared
32 char name[sizeof("SCHED_OTHER")];
34 {SCHED_OTHER, "SCHED_OTHER"},
35 {SCHED_FIFO, "SCHED_FIFO"},
36 {SCHED_RR, "SCHED_RR"}
43 static void show_min_max(int pol)
45 const char *fmt = "%s min/max priority\t: %u/%u\n";
48 max = sched_get_priority_max(pol);
49 min = sched_get_priority_min(pol);
51 fmt = "%s not supported\n";
52 printf(fmt, policies[pol].name, min, max);
61 int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62 int chrt_main(int argc UNUSED_PARAM, char **argv)
66 struct sched_param sp;
68 char *priority = priority; /* for compiler */
69 const char *current_new;
70 int policy = SCHED_RR;
72 /* only one policy accepted */
73 opt_complementary = "r--fo:f--ro:o--rf";
74 opt = getopt32(argv, "+mprfo");
75 if (opt & OPT_m) { /* print min/max and exit */
76 show_min_max(SCHED_FIFO);
77 show_min_max(SCHED_RR);
78 show_min_max(SCHED_OTHER);
79 fflush_stdout_and_exit(EXIT_SUCCESS);
93 if (*argv) { /* "-p <priority> <pid> [...]" */
97 /* else "-p <pid>", and *argv == NULL */
98 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
105 current_new = "current\0new";
109 pol = sched_getscheduler(pid);
111 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
112 printf("pid %d's %s scheduling policy: %s\n",
113 pid, current_new, policies[pol].name);
114 if (sched_getparam(pid, &sp))
115 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
116 printf("pid %d's %s scheduling priority: %d\n",
117 (int)pid, current_new, sp.sched_priority);
119 /* Either it was just "-p <pid>",
120 * or it was "-p <priority> <pid>" and we came here
121 * for the second time (see goto below) */
128 /* from the manpage of sched_getscheduler:
129 [...] sched_priority can have a value in the range 0 to 99.
130 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
131 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
133 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
135 if (sched_setscheduler(pid, policy, &sp) < 0)
136 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
138 if (!argv[0]) /* "-p <priority> <pid> [...]" */
141 BB_EXECVP_or_die(argv);