BSD 3 development
[unix-history] / usr / src / cmd / renice.c
CommitLineData
1a51a8f3
BJ
1#include <sys/param.h>
2#include <sys/proc.h>
3#include <stdio.h>
4
5struct proc proc[NPROC];
6struct {
7 char name[8];
8 int type;
9 unsigned value;
10} nl[] = {
11 "_proc", 0, 0,
12 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
13};
14
15/*
16 * Change the running priority (nice) of a process which is already
17 * running.
18 *
19 * Author: Kurt Shoens
20 */
21
22main(argc, argv)
23 char **argv;
24{
25 register struct proc *pp;
26 int pid, nice;
27 int addr, mem, a1, a2, coreaddr;
28
29 if (argc != 2 && argc != 3) {
30 fprintf(stderr, "usage: renice pid [ priority ]\n");
31 exit(1);
32 }
33 if (geteuid()) {
34 fprintf(stderr, "NOT super user\n");
35 exit(1);
36 }
37 pid = atoi(argv[1]);
38 nice = atoi(argc == 3 ? argv[2] : "19");
39 if (nice > 20)
40 nice = 20;
41 if (nice < -20)
42 nice = -20;
43 nice += NZERO;
44 mem = open("/dev/kmem", 2);
45 if (mem < 0) {
46 perror("/dev/kmem");
47 exit(1);
48 }
49 nlist("/vmunix", nl);
50 addr = nl[0].value;
51 if (addr == 0) {
52 fprintf(stderr, "/vmunix: _proc not in namelist");
53 exit(1);
54 }
55 lseek(mem, addr, 0);
56 read(mem, &proc[0], sizeof proc);
57 for (pp = &proc[0]; pp < &proc[NPROC]; pp++)
58 if (pp->p_pid == pid)
59 break;
60 if (pp >= &proc[NPROC]) {
61 fprintf(stderr, "%d: process not found\n", pid);
62 exit(1);
63 }
64 fprintf(stderr, "%d: old nice = %d, new nice = %d\n",
65 pid,
66 pp->p_nice - NZERO,
67 nice - NZERO);
68 a1 = (int)&pp->p_nice;
69 a2 = (int)&proc[0];
70 coreaddr = a1-a2+addr;
71 lseek(mem, (long)coreaddr, 0);
72 write(mem, &nice, 1);
73}