forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.16.c
65 lines (59 loc) · 1.34 KB
/
8.16.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <errno.h>
#include <sys/time.h>
#include "apue.h"
#if defined(SOLARIS)
#include <limits.h>
#elif defined(BSD)
#include <sys/param.h>
#endif
unsigned long long count;
struct timeval end;
void checktime(char *str) {
struct timeval tv;
gettimeofday(&tv, NULL);
if (tv.tv_sec >= end.tv_sec && tv.tv_usec >= end.tv_usec) {
printf("%s count = %lld\n", str, count);
exit(0);
}
}
int main(int argc, char *argv[]) {
pid_t pid;
char *s;
int nzero, ret;
int adj = 0;
setbuf(stdout, NULL);
#if defined(NZERO)
nzero = NZERO;
#elif defined(_SC_NZERO)
nzero = sysconf(_SC_NZERO);
#else
#error NZERO undefined
#endif
printf("NZERO = %d\n", nzero);
if (argc == 2) {
adj = strtol(argv[1], NULL, 10);
}
gettimeofday(&end, NULL);
end.tv_sec += 10;
if ((pid = fork()) < 0) {
err_sys("fork failed");
} else if (pid == 0) {
s = "child";
printf("current nice value in child is %d, adjusting by %d\n",
nice(0) + nzero, adj);
errno = 0;
if ((ret = nice(adj)) == -1 && errno != 0) {
err_sys("child set scheduling priority");
}
printf("now child nice value is %d\n", nice(0) + nzero);
} else {
s = "parent";
printf("current nice value in parent is %d\n", nice(0) + nzero);
}
for (;;) {
if (++count == 0) {
err_quit("%s counter wrap", s);
}
checktime(s);
}
}