3 * mean - lower process priorities with more force than nice
5 * Created by Lucia Ballard on 9/16/09.
6 * Copyright 2009-2016 Apple Inc. All rights reserved.
10 #include <mach/mach.h>
11 #include <mach/task.h>
12 #include <mach/thread_act.h>
13 #include <mach/thread_policy.h>
25 fprintf(stderr
, "Usage: mean -[r|s|u] <pid>\n");
26 fprintf(stderr
, "\tLower <pid>'s priority.\n");
27 fprintf(stderr
, "\t-u: return <pid> to normal priority\n");
28 fprintf(stderr
, "\t-r: resume <pid>\n");
29 fprintf(stderr
, "\t-s: suspend <pid>\n");
34 main(int argc
, char **argv
)
39 thread_act_array_t threads
;
40 thread_precedence_policy_data_t policy
;
42 boolean_t do_high
= 0, do_resume
= 0, do_suspend
= 0;
48 while ((ch
= getopt(argc
, argv
, "rsu")) != -1)
66 argc
-= optind
; argv
+= optind
;
75 err
= task_for_pid(mach_task_self(), pid
, &task
);
77 fprintf(stderr
, "Failed to get task port (%d)\n", err
);
81 if (do_low
|| do_high
) {
83 err
= task_threads(task
, &threads
, &count
);
85 fprintf(stderr
, "Failed to get thread list (%d)\n", err
);
90 policy
.importance
= -100;
92 policy
.importance
= 0;
94 for (i
= 0; i
< count
; i
++) {
95 err
= thread_policy_set(threads
[i
],
96 THREAD_PRECEDENCE_POLICY
,
97 (thread_policy_t
) &policy
,
98 THREAD_PRECEDENCE_POLICY_COUNT
);
100 fprintf(stderr
, "Failed to set thread priority (%d)\n", err
);
105 printf("Process %d's threads set to %s priority.\n", pid
,
106 (do_low
? "lowest" : "highest"));
110 err
= task_suspend(task
);
112 fprintf(stderr
, "Failed to suspend task (%d)\n", err
);
114 printf("Process %d suspended.\n", pid
);
119 err
= task_resume(task
);
121 fprintf(stderr
, "Failed to resume task (%d)\n", err
);
123 printf("Process %d resumed.\n", pid
);