From 5fd83771641d15c418f747bd343ba6738d3875f7 Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Sun, 9 May 2021 14:20:58 -0400 Subject: Import macOS userland adv_cmds-176 basic_cmds-55 bootstrap_cmds-116.100.1 developer_cmds-66 diskdev_cmds-667.40.1 doc_cmds-53.60.1 file_cmds-321.40.3 mail_cmds-35 misc_cmds-34 network_cmds-606.40.1 patch_cmds-17 remote_cmds-63 shell_cmds-216.60.1 system_cmds-880.60.2 text_cmds-106 --- system_cmds/mean.tproj/mean.c | 128 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 system_cmds/mean.tproj/mean.c (limited to 'system_cmds/mean.tproj/mean.c') diff --git a/system_cmds/mean.tproj/mean.c b/system_cmds/mean.tproj/mean.c new file mode 100644 index 0000000..87ecdc2 --- /dev/null +++ b/system_cmds/mean.tproj/mean.c @@ -0,0 +1,128 @@ +/* + * mean.c + * mean - lower process priorities with more force than nice + * + * Created by Lucia Ballard on 9/16/09. + * Copyright 2009-2016 Apple Inc. All rights reserved. + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +void usage(void); + +void +usage(void) +{ + fprintf(stderr, "Usage: mean -[r|s|u] \n"); + fprintf(stderr, "\tLower 's priority.\n"); + fprintf(stderr, "\t-u: return to normal priority\n"); + fprintf(stderr, "\t-r: resume \n"); + fprintf(stderr, "\t-s: suspend \n"); + exit(0); +} + +int +main(int argc, char **argv) +{ + int pid, err, i, ch; + unsigned int count; + mach_port_t task; + thread_act_array_t threads; + thread_precedence_policy_data_t policy; + + boolean_t do_high = 0, do_resume = 0, do_suspend = 0; + boolean_t do_low = 1; + + if (argc < 2) + usage(); + + while ((ch = getopt(argc, argv, "rsu")) != -1) + switch (ch) { + case 'u': + do_high = 1; + do_low = 0; + continue; + case 'r': + do_resume = 1; + do_low = 0; + continue; + case 's': + do_suspend = 1; + do_low = 0; + continue; + default: + usage(); + } + + argc -= optind; argv += optind; + + if (argc == 0) + usage(); + + pid = atoi(*argv); + if (!pid) + usage(); + + err = task_for_pid(mach_task_self(), pid, &task); + if (err) { + fprintf(stderr, "Failed to get task port (%d)\n", err); + exit(0); + } + + if (do_low || do_high) { + + err = task_threads(task, &threads, &count); + if (err) { + fprintf(stderr, "Failed to get thread list (%d)\n", err); + exit(0); + } + + if (do_low) + policy.importance = -100; + else + policy.importance = 0; + + for (i = 0; i < count; i++) { + err = thread_policy_set(threads[i], + THREAD_PRECEDENCE_POLICY, + (thread_policy_t) &policy, + THREAD_PRECEDENCE_POLICY_COUNT); + if (err) { + fprintf(stderr, "Failed to set thread priority (%d)\n", err); + exit(0); + } + } + + printf("Process %d's threads set to %s priority.\n", pid, + (do_low ? "lowest" : "highest")); + } + + if (do_suspend) { + err = task_suspend(task); + if (err) { + fprintf(stderr, "Failed to suspend task (%d)\n", err); + } else { + printf("Process %d suspended.\n", pid); + } + } + + if (do_resume) { + err = task_resume(task); + if (err) { + fprintf(stderr, "Failed to resume task (%d)\n", err); + } else { + printf("Process %d resumed.\n", pid); + } + } + + return 0; +} -- cgit v1.2.3-56-ge451