3 * Command-line utility for manipulating a task's real-time attributes
5 * Robert Love <rml@tech9.net>
6 * 27-Apr-2002: initial version
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, v2, as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Copyright (C) 2004 Robert Love
34 /* the SCHED_BATCH is supported since Linux 2.6.16
35 * -- temporary workaround for people with old glibc headers
37 #if defined (__linux__) && !defined(SCHED_BATCH)
38 # define SCHED_BATCH 3
41 /* the SCHED_IDLE is supported since Linux 2.6.23
42 * commit id 0e6aca43e08a62a48d6770e9a159dbec167bf4c6
43 * -- temporary workaround for people with old glibc headers
45 #if defined (__linux__) && !defined(SCHED_IDLE)
50 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
53 static void show_usage(int rc)
56 "\nchrt - manipulate real-time attributes of a process.\n"
58 " chrt [options] <policy> <priority> {<pid> | <command> [<arg> ...]}\n"
60 " chrt [options] {<pid> | <command> [<arg> ...]}\n\n"
61 "\nScheduling policies:\n"
62 " -b | --batch set policy to SCHED_BATCH\n"
63 " -f | --fifo set policy to SCHED_FIFO\n"
64 " -i | --idle set policy to SCHED_IDLE\n"
65 " -o | --other set policy to SCHED_OTHER\n"
66 " -r | --rr set policy to SCHED_RR (default)\n"
68 " -h | --help display this help\n"
69 " -p | --pid operate on existing given pid\n"
70 " -m | --max show min and max valid priorities\n"
71 " -v | --verbose display status information\n"
72 " -V | --version output version information\n\n"));
77 static void show_rt_info(const char *what, pid_t pid)
79 struct sched_param sp;
82 /* don't display "pid 0" as that is confusing */
86 policy = sched_getscheduler(pid);
88 err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
90 printf(_("pid %d's %s scheduling policy: "), pid, what);
93 printf("SCHED_OTHER\n");
96 printf("SCHED_FIFO\n");
100 printf("SCHED_IDLE\n");
104 printf("SCHED_RR\n");
108 printf("SCHED_BATCH\n");
112 printf(_("unknown\n"));
115 if (sched_getparam(pid, &sp))
116 err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
118 printf(_("pid %d's %s scheduling priority: %d\n"),
119 pid, what, sp.sched_priority);
122 static void show_min_max(void)
125 int policies[] = { SCHED_OTHER, SCHED_FIFO, SCHED_RR,
133 const char *names[] = { "OTHER", "FIFO", "RR",
142 for (i = 0; i < ARRAY_SIZE(policies); i++) {
143 int max = sched_get_priority_max(policies[i]);
144 int min = sched_get_priority_min(policies[i]);
146 if (max >= 0 && min >= 0)
147 printf(_("SCHED_%s min/max priority\t: %d/%d\n"),
150 printf(_("SCHED_%s not supported?\n"), names[i]);
154 int main(int argc, char *argv[])
156 int i, policy = SCHED_RR, priority = 0, verbose = 0;
157 struct sched_param sp;
160 struct option longopts[] = {
161 { "batch", 0, NULL, 'b' },
162 { "fifo", 0, NULL, 'f' },
163 { "idle", 0, NULL, 'i' },
164 { "pid", 0, NULL, 'p' },
165 { "help", 0, NULL, 'h' },
166 { "max", 0, NULL, 'm' },
167 { "other", 0, NULL, 'o' },
168 { "rr", 0, NULL, 'r' },
169 { "verbose", 0, NULL, 'v' },
170 { "version", 0, NULL, 'V' },
174 setlocale(LC_ALL, "");
175 bindtextdomain(PACKAGE, LOCALEDIR);
178 while((i = getopt_long(argc, argv, "+bfiphmorvV", longopts, NULL)) != -1)
180 int ret = EXIT_FAILURE;
185 policy = SCHED_BATCH;
200 policy = SCHED_OTHER;
204 pid = strtol(argv[argc - 1], NULL, 10);
206 err(EXIT_FAILURE, _("failed to parse pid"));
215 printf("chrt (%s)\n", PACKAGE_STRING);
224 if (((pid > -1) && argc - optind < 1) || ((pid == -1) && argc - optind < 2))
225 show_usage(EXIT_FAILURE);
227 if ((pid > -1) && (verbose || argc - optind == 1)) {
228 show_rt_info(_("current"), pid);
229 if (argc - optind == 1)
234 priority = strtol(argv[optind], NULL, 10);
236 err(EXIT_FAILURE, _("failed to parse priority"));
240 sp.sched_priority = priority;
241 if (sched_setscheduler(pid, policy, &sp) == -1)
242 err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid);
245 show_rt_info("new", pid);
249 execvp(argv[0], argv);
251 err(EXIT_FAILURE, _("failed to execute %s"), argv[0]);