123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include <sched.h>
- #include "libbb.h"
- static const struct {
- int policy;
- char name[sizeof("SCHED_OTHER")];
- } policies[] = {
- {SCHED_OTHER, "SCHED_OTHER"},
- {SCHED_FIFO, "SCHED_FIFO"},
- {SCHED_RR, "SCHED_RR"}
- };
- static void show_min_max(int pol)
- {
- const char *fmt = "%s min/max priority\t: %u/%u\n";
- int max, min;
- max = sched_get_priority_max(pol);
- min = sched_get_priority_min(pol);
- if ((max|min) < 0)
- fmt = "%s not supported\n";
- printf(fmt, policies[pol].name, min, max);
- }
- #define OPT_m (1<<0)
- #define OPT_p (1<<1)
- #define OPT_r (1<<2)
- #define OPT_f (1<<3)
- #define OPT_o (1<<4)
- int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int chrt_main(int argc UNUSED_PARAM, char **argv)
- {
- pid_t pid = 0;
- unsigned opt;
- struct sched_param sp;
- char *pid_str;
- char *priority = priority;
- const char *current_new;
- int policy = SCHED_RR;
-
- opt = getopt32(argv, "^+" "mprfo" "\0" "r--fo:f--ro:o--rf");
- if (opt & OPT_m) {
- show_min_max(SCHED_FIFO);
- show_min_max(SCHED_RR);
- show_min_max(SCHED_OTHER);
- fflush_stdout_and_exit(EXIT_SUCCESS);
- }
- if (opt & OPT_r)
- policy = SCHED_RR;
- if (opt & OPT_f)
- policy = SCHED_FIFO;
- if (opt & OPT_o)
- policy = SCHED_OTHER;
- argv += optind;
- if (!argv[0])
- bb_show_usage();
- if (opt & OPT_p) {
- pid_str = *argv++;
- if (*argv) {
- priority = pid_str;
- pid_str = *argv;
- }
-
- pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
- } else {
- priority = *argv++;
- if (!*argv)
- bb_show_usage();
- }
- current_new = "current\0new";
- if (opt & OPT_p) {
- int pol;
- print_rt_info:
- pol = sched_getscheduler(pid);
- if (pol < 0)
- bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
- printf("pid %d's %s scheduling policy: %s\n",
- pid, current_new, policies[pol].name);
- if (sched_getparam(pid, &sp))
- bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
- printf("pid %d's %s scheduling priority: %d\n",
- (int)pid, current_new, sp.sched_priority);
- if (!*argv) {
-
- return EXIT_SUCCESS;
- }
- *argv = NULL;
- current_new += 8;
- }
-
- sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
- if (sched_setscheduler(pid, policy, &sp) < 0)
- bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
- if (!argv[0])
- goto print_rt_info;
- BB_EXECVP_or_die(argv);
- }
|