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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* chrt.c - chrt
* Command-line utility for manipulating a task's real-time attributes
*
* Robert Love <rml@tech9.net>
* 27-Apr-2002: initial version
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, v2, as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Copyright (C) 2004 Robert Love
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
static void show_usage(const char *cmd)
{
fprintf(stderr, "chrt version " VERSION "\n");
fprintf(stderr, "usage: %s [options] [prio] [pid | cmd [args...]]\n",
cmd);
fprintf(stderr, "manipulate real-time attributes of a process\n");
fprintf(stderr, " -f, --fifo "
"set policy to SCHED_FF\n");
fprintf(stderr, " -p, --pid "
"operate on existing given pid\n");
fprintf(stderr, " -m, --max "
"show min and max valid priorities\n");
fprintf(stderr, " -o, --other "
"set policy to SCHED_OTHER\n");
fprintf(stderr, " -r, --rr "
"set policy to SCHED_RR (default)\n");
fprintf(stderr, " -h, --help "
"display this help\n");
fprintf(stderr, " -v, --verbose "
"display status information\n");
fprintf(stderr, " -V, --version "
"output version information\n\n");
fprintf(stderr, "You must give a priority if changing policy.\n\n");
fprintf(stderr, "Report bugs and send patches to <rml@tech9.net>\n");
}
static void show_rt_info(const char *what, pid_t pid)
{
struct sched_param sp;
int policy;
/* don't display "pid 0" as that is confusing */
if (!pid)
pid = getpid();
policy = sched_getscheduler(pid);
if (policy == -1) {
perror("sched_getscheduler");
fprintf(stderr, "failed to get pid %d's policy\n", pid);
exit(1);
}
printf("pid %d's %s scheduling policy: ", pid, what);
switch (policy) {
case SCHED_OTHER:
printf("SCHED_OTHER\n");
break;
case SCHED_FIFO:
printf("SCHED_FIFO\n");
break;
case SCHED_RR:
printf("SCHED_RR\n");
break;
default:
printf("unknown\n");
}
if (sched_getparam(pid, &sp)) {
perror("sched_getparam");
fprintf(stderr, "failed to get pid %d's attributes\n", pid);
exit(1);
}
printf("pid %d's %s scheduling priority: %d\n",
pid, what, sp.sched_priority);
}
static void show_min_max(void)
{
int max, min;
max = sched_get_priority_max(SCHED_FIFO);
min = sched_get_priority_min(SCHED_FIFO);
if (max >= 0 && min >= 0)
printf("SCHED_FIFO min/max priority\t: %d/%d\n", min, max);
else
printf("SCHED_FIFO not supported?\n");
max = sched_get_priority_max(SCHED_RR);
min = sched_get_priority_min(SCHED_RR);
if (max >= 0 && min >= 0)
printf("SCHED_RR min/max priority\t: %d/%d\n", min, max);
else
printf("SCHED_RR not supported?\n");
max = sched_get_priority_max(SCHED_OTHER);
min = sched_get_priority_min(SCHED_OTHER);
if (max >= 0 && min >= 0)
printf("SCHED_OTHER min/max priority\t: %d/%d\n", min, max);
else
printf("SCHED_OTHER not supported?\n");
}
int main(int argc, char *argv[])
{
int i, policy = SCHED_RR, priority = 0, verbose = 0;
struct sched_param sp;
pid_t pid = 0;
struct option longopts[] = {
{ "fifo", 0, NULL, 'f' },
{ "pid", 0, NULL, 'p' },
{ "help", 0, NULL, 'h' },
{ "max", 0, NULL, 'm' },
{ "other", 0, NULL, 'o' },
{ "rr", 0, NULL, 'r' },
{ "verbose", 0, NULL, 'v' },
{ "version", 0, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
while((i = getopt_long(argc, argv, "+fphmorvV", longopts, NULL)) != -1)
{
int ret = 1;
switch (i) {
case 'f':
policy = SCHED_FIFO;
break;
case 'm':
show_min_max();
return 0;
case 'o':
policy = SCHED_OTHER;
break;
case 'p':
errno = 0;
pid = strtol(argv[argc - 1], NULL, 10);
if (errno) {
perror("strtol");
fprintf(stderr, "failed to parse pid!\n");
return 1;
}
break;
case 'r':
policy = SCHED_RR;
break;
case 'v':
verbose = 1;
break;
case 'V':
printf("chrt version " VERSION "\n");
return 0;
case 'h':
ret = 0;
default:
show_usage(argv[0]);
return ret;
}
}
if ((pid && argc - optind < 1) || (!pid && argc - optind < 2)) {
show_usage(argv[0]);
return 1;
}
if (pid && (verbose || argc - optind == 1)) {
show_rt_info("current", pid);
if (argc - optind == 1)
return 0;
}
errno = 0;
priority = strtol(argv[optind], NULL, 10);
if (errno) {
perror("strtol");
fprintf(stderr, "failed to parse priority!\n");
return 1;
}
sp.sched_priority = priority;
if (sched_setscheduler(pid, policy, &sp) == -1) {
perror("sched_setscheduler");
fprintf(stderr, "failed to set pid %d's policy\n", pid);
return 1;
}
if (verbose)
show_rt_info("new", pid);
if (!pid) {
argv += optind + 1;
execvp(argv[0], argv);
perror("execvp");
fprintf(stderr, "failed to execute %s\n", argv[0]);
return 1;
}
return 0;
}
|