47
49
# define SCHED_IDLE 5
50
static void show_usage(int rc)
52
#if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
53
#define SCHED_RESET_ON_FORK 0x40000000
57
static void __attribute__((__noreturn__)) show_usage(int rc)
53
"\nchrt - manipulate real-time attributes of a process.\n"
59
FILE *out = rc == EXIT_SUCCESS ? stdout : stderr;
62
"\nchrt - manipulate real-time attributes of a process\n"
55
64
" chrt [options] <policy> <priority> {<pid> | <command> [<arg> ...]}\n"
57
" chrt [options] {<pid> | <command> [<arg> ...]}\n\n"
66
" chrt [options] {<pid> | <command> [<arg> ...]}\n"));
58
69
"\nScheduling policies:\n"
59
70
" -b | --batch set policy to SCHED_BATCH\n"
60
71
" -f | --fifo set policy to SCHED_FIFO\n"
61
72
" -i | --idle set policy to SCHED_IDLE\n"
62
73
" -o | --other set policy to SCHED_OTHER\n"
63
" -r | --rr set policy to SCHED_RR (default)\n"
74
" -r | --rr set policy to SCHED_RR (default)\n"));
76
#ifdef SCHED_RESET_ON_FORK
78
"\nScheduling flags:\n"
79
" -R | --reset-on-fork set SCHED_RESET_ON_FORK for FIFO or RR\n"));
65
83
" -h | --help display this help\n"
84
" -m | --max show min and max valid priorities\n"
66
85
" -p | --pid operate on existing given pid\n"
67
" -m | --max show min and max valid priorities\n"
68
86
" -v | --verbose display status information\n"
69
87
" -V | --version output version information\n\n"));
74
static void show_rt_info(const char *what, pid_t pid)
92
static void show_rt_info(pid_t pid, int isnew)
76
94
struct sched_param sp;
85
103
err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
87
printf(_("pid %d's %s scheduling policy: "), pid, what);
106
printf(_("pid %d's new scheduling policy: "), pid);
108
printf(_("pid %d's current scheduling policy: "), pid);
90
112
printf("SCHED_OTHER\n");
112
144
if (sched_getparam(pid, &sp))
113
145
err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
115
printf(_("pid %d's %s scheduling priority: %d\n"),
116
pid, what, sp.sched_priority);
148
printf(_("pid %d's new scheduling priority: %d\n"),
149
pid, sp.sched_priority);
151
printf(_("pid %d's current scheduling priority: %d\n"),
152
pid, sp.sched_priority);
119
155
static void show_min_max(void)
151
187
int main(int argc, char *argv[])
153
int i, policy = SCHED_RR, priority = 0, verbose = 0;
189
int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0;
154
190
struct sched_param sp;
163
199
{ "max", 0, NULL, 'm' },
164
200
{ "other", 0, NULL, 'o' },
165
201
{ "rr", 0, NULL, 'r' },
202
{ "reset-on-fork", 0, NULL, 'R' },
166
203
{ "verbose", 0, NULL, 'v' },
167
204
{ "version", 0, NULL, 'V' },
168
205
{ NULL, 0, NULL, 0 }
172
209
bindtextdomain(PACKAGE, LOCALEDIR);
173
210
textdomain(PACKAGE);
175
while((i = getopt_long(argc, argv, "+bfiphmorvV", longopts, NULL)) != -1)
212
while((i = getopt_long(argc, argv, "+bfiphmoRrvV", longopts, NULL)) != -1)
177
214
int ret = EXIT_FAILURE;
201
pid = strtol(argv[argc - 1], NULL, 10);
203
err(EXIT_FAILURE, _("failed to parse pid"));
243
pid = strtol_or_err(argv[argc - 1], _("failed to parse pid"));
206
246
policy = SCHED_RR;
222
262
show_usage(EXIT_FAILURE);
224
264
if ((pid > -1) && (verbose || argc - optind == 1)) {
225
show_rt_info(_("current"), pid);
265
show_rt_info(pid, FALSE);
226
266
if (argc - optind == 1)
227
267
return EXIT_SUCCESS;
231
priority = strtol(argv[optind], NULL, 10);
233
err(EXIT_FAILURE, _("failed to parse priority"));
271
priority = strtol_or_err(argv[optind], _("failed to parse priority"));
273
#ifdef SCHED_RESET_ON_FORK
275
if ((policy_flag & SCHED_RESET_ON_FORK) &&
276
!(policy == SCHED_FIFO || policy == SCHED_RR))
277
errx(EXIT_FAILURE, _("SCHED_RESET_ON_FORK flag is suppoted for "
278
"SCHED_FIFO and SCHED_RR policies only"));
281
policy |= policy_flag;