3 * mean - lower process priorities with more force than nice
5 * Created by Lucia Ballard on 9/16/09.
6 * Copyright 2009 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>
26 fprintf(stderr
, "Usage: mean -[r|s|u] <pid>\n");
27 fprintf(stderr
, "\tLower <pid>'s priority.\n");
28 fprintf(stderr
, "\t-u: return <pid> to normal priority\n");
29 fprintf(stderr
, "\t-r: resume <pid>\n");
30 fprintf(stderr
, "\t-s: suspend <pid>\n");
35 main(int argc
, char **argv
)
40 thread_act_array_t threads
;
41 thread_precedence_policy_data_t policy
;
43 boolean_t do_high
= 0, do_resume
= 0, do_suspend
= 0;
49 while ((ch
= getopt(argc
, argv
, "rsu")) != -1)
67 argc
-= optind
; argv
+= optind
;
76 err
= task_for_pid(mach_task_self(), pid
, &task
);
78 fprintf(stderr
, "Failed to get task port (%d)\n", err
);
82 if (do_low
|| do_high
) {
84 err
= task_threads(task
, &threads
, &count
);
86 fprintf(stderr
, "Failed to get thread list (%d)\n", err
);
91 policy
.importance
= -100;
93 policy
.importance
= 0;
95 for (i
= 0; i
< count
; i
++) {
96 err
= thread_policy_set(threads
[i
],
97 THREAD_PRECEDENCE_POLICY
,
98 (thread_policy_t
) &policy
,
99 THREAD_PRECEDENCE_POLICY_COUNT
);
101 fprintf(stderr
, "Failed to set thread priority (%d)\n", err
);
106 printf("Process %d's threads set to %s priority.\n", pid
,
107 (do_low
? "lowest" : "highest"));
111 err
= task_suspend(task
);
113 fprintf(stderr
, "Failed to suspend task (%d)\n", err
);
115 printf("Process %d suspended.\n", pid
);
121 err
= task_resume(task
);
123 fprintf(stderr
, "Failed to resume task (%d)\n", err
);
125 printf("Process %d resumed.\n", pid
);