1 /* renice.c - unixish renice command for BeOs
2 * (c) 2001, 2002, François Revol (mmu_man) for OpenBeOS
3 * released under the MIT licence.
5 * How did I live without it before ??? ;)
8 * Cleanup for inclusion in OpenBeOS,
9 * Used the code to rewrite the 'prio' BeOS command for OpenBeOS.
11 * Added -f upon suggestion from Idéfix on BeShare
24 /usr/sbin/renice [-n increment] [-p] [-g | -u] ID ... /usr/sbin/renice priority [-p] pid ... [-g pgrp ...] [-u user ...]
28 (realtime) High Prio Default Low Prio
29 120 99 10 1 (0 only for idle_thread)
36 renice can be given more than one pid on the command line.
37 prio is locked into one pid, then the priority.
45 #define BZERO B_NORMAL_PRIORITY
46 #define BMIN (B_REAL_TIME_DISPLAY_PRIORITY-1)
47 //#define BMAX B_NORMAL_PRIORITY
50 // returns an equivalent UNIX priority for a given BeOS priority.
51 static int32
prio_be_to_unix(int32 prio
)
54 return NZERO
- ((prio
- BZERO
) * NZERO
) / (BMIN
- BZERO
);
55 return NZERO
+ ((BZERO
- prio
) * (NZERO
- 1)) / (BZERO
- BMAX
);
58 // returns an equivalent BeOS priority for a given UNIX priority.
59 static int32
prio_unix_to_be(int32 prio
)
62 return BZERO
- ((prio
- NZERO
) * (BZERO
- BMAX
)) / (NZERO
-1);
63 return BZERO
+ ((NZERO
- prio
) * (BMIN
- BZERO
)) / (NZERO
);
66 static status_t
renice_thread(int32 prio
, int32 increment
, bool use_be_prio
, thread_id th
)
71 get_thread_info(th
, &thinfo
);
72 prio
= thinfo
.priority
;
74 prio
= prio_be_to_unix(prio
);
77 prio
= prio_unix_to_be(prio
);
79 return set_thread_priority(th
, prio
);
82 int main(int argc
, char **argv
)
85 int32 prio
, increment
= 0;
87 bool use_be_prio
= false;
88 bool next_is_prio
= true;
89 bool next_is_increment
= false;
90 bool use_increment
= false;
91 bool find_by_name
= false;
99 prio
= NZERO
; // default UNIX priority for nice
100 // convert it to beos
102 prio
= prio_unix_to_be(prio
);
105 if (!strcmp(argv
[i
], "-p")) { // ignored option
106 } else if (!strcmp(argv
[i
], "-n")) {
107 next_is_prio
= false;
108 next_is_increment
= true;
109 use_increment
= true;
110 } else if (!strcmp(argv
[i
], "-b")) {
112 } else if (!strcmp(argv
[i
], "-f")) {
114 } else if (next_is_increment
) {
115 next_is_increment
= false;
116 sscanf(argv
[i
], "%ld", (long *)&increment
);
117 } else if (next_is_prio
) {
118 next_is_prio
= false;
119 sscanf(argv
[i
], "%ld", (long *)&prio
);
121 prio
= prio_unix_to_be(prio
);
124 sscanf(argv
[i
], "%ld", (long *)&th
);
125 return (renice_thread(prio
, increment
, use_be_prio
, th
) == B_OK
)?0:1;
128 while (get_next_team_info(&teamcookie
, &teaminfo
) == B_OK
) {
130 while (get_next_thread_info(teaminfo
.team
, &thcookie
, &thinfo
) == B_OK
) {
131 if (!strncmp(thname
, thinfo
.name
, B_OS_NAME_LENGTH
)) {
133 renice_thread(prio
, increment
, use_be_prio
, th
);
135 /* find another one */
144 puts("renice [-b] [-n increment | prio] [-f thname thname...|thid thid ...]");
145 puts(" -b : use BeOS priorities instead of UNIX priorities");
146 puts(" -n : adds increment to the current priority instead of assigning it");
147 puts(" -f : find threads by name instead of by id");