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 tarball for details.
11 #ifndef _POSIX_PRIORITY_SCHEDULING
12 #warning your system may be foobared
18 {SCHED_OTHER, "SCHED_OTHER"},
19 {SCHED_FIFO, "SCHED_FIFO"},
20 {SCHED_RR, "SCHED_RR"}
23 static void show_min_max(int pol)
25 const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
27 max = sched_get_priority_max(pol);
28 min = sched_get_priority_min(pol);
29 if (max >= 0 && min >= 0)
30 printf(fmt, policies[pol].name, min, max);
33 printf(fmt, policies[pol].name);
43 int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int chrt_main(int argc UNUSED_PARAM, char **argv)
48 struct sched_param sp;
50 char *priority = priority; /* for compiler */
51 const char *current_new;
52 int policy = SCHED_RR;
54 /* at least 1 arg; only one policy accepted */
55 opt_complementary = "-1:r--fo:f--ro:r--fo";
56 opt = getopt32(argv, "+mprfo");
63 if (opt & OPT_m) { /* print min/max */
64 show_min_max(SCHED_FIFO);
65 show_min_max(SCHED_RR);
66 show_min_max(SCHED_OTHER);
67 fflush_stdout_and_exit(EXIT_SUCCESS);
73 if (*argv) { /* "-p <priority> <pid> [...]" */
77 /* else "-p <pid>", and *argv == NULL */
78 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
85 current_new = "current\0new";
89 pol = sched_getscheduler(pid);
91 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
92 printf("pid %d's %s scheduling policy: %s\n",
93 pid, current_new, policies[pol].name);
94 if (sched_getparam(pid, &sp))
95 bb_perror_msg_and_die("can't get pid %d's attributes", pid);
96 printf("pid %d's %s scheduling priority: %d\n",
97 pid, current_new, sp.sched_priority);
99 /* Either it was just "-p <pid>",
100 * or it was "-p <priority> <pid>" and we came here
101 * for the second time (see goto below) */
108 /* from the manpage of sched_getscheduler:
109 [...] sched_priority can have a value in the range 0 to 99.
110 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
111 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
113 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
115 if (sched_setscheduler(pid, policy, &sp) < 0)
116 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
118 if (!*argv) /* "-p <priority> <pid> [...]" */
121 BB_EXECVP(*argv, argv);
122 bb_simple_perror_msg_and_die(*argv);