-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4-priority.c
executable file
·131 lines (112 loc) · 3.68 KB
/
p4-priority.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "types.h"
#include "user.h"
#include "param.h"
#include "pdx.h"
// This tests the promotion facility.
//
// It is assumed that setpriority will return the appropriate success/failure codes
void
testPromotion(void)
{
int pid, prio, newPrio, rc;
// Test promotion
printf(1, "\n\nTesting promotion...\n");
pid = getpid();
rc = setpriority(pid, 0);
if (rc != 0) {
printf(2, "setpriority returned failure code!\n");
printf(2, "**** TEST FAILED ****\n\n\n");
return;
}
prio = getpriority(pid);
sleep(TICKS_TO_PROMOTE + 100);
newPrio = getpriority(pid);
if (newPrio != prio && newPrio > prio) {
printf(1, "Promotion has occurred.\n");
printf(1, "**** TEST PASSES ****\n");
} else {
printf(2, "Either promotion did not occur or an unexpected change in priority happened.\n");
printf(2, "**** TEST FAILED ****\n");
}
printf(1, "\n\n");
}
// Create a process and change the priority
void
checkPriority(void)
{
int pid;
int originalPriority;
int newPriority;
int rc;
printf(1, "Testing that process starts at MAXPRIO\n");
pid = getpid();
originalPriority = getpriority(pid);
printf(1, "Priority after program start is %d\n", originalPriority);
if (originalPriority != MAXPRIO) {
printf(2, "Process didn't start at MAXPRIO\n");
printf(2, "**** TEST FAILED ****\n\n\n");
} else {
printf(1, "**** TEST PASSED ****\n\n\n");
}
// Test that setting the priority
// 1. Returns an appropriate code on success
// 2. Actually changes the priority on success
rc = setpriority(pid, MAXPRIO);
if (rc != 0) {
printf(2, "setpriority(%d, %d) failed!\n", pid, MAXPRIO);
printf(2, "**** TEST FAILED ****\n\n\n");
} else {
// verify that the new priority is what it should be:
newPriority = getpriority(pid);
if (newPriority != MAXPRIO) {
printf(2, "setpriority(%d, %d) failed.\n", pid, MAXPRIO);
printf(2, "New priority is %d, but it should be %d.\n", newPriority, MAXPRIO);
printf(2, "**** TEST FAILED ****\n\n\n");
}
}
// Test that the priority cannot be set to a negative number:
printf(1, "Testing that a priority cannot be set to an out of range value.\n");
printf(1, " Testing setting priority to a negative number.\n");
originalPriority = getpriority(pid);
rc = setpriority(pid, -1);
printf(1, " setPriority(%d, -1) returned %d.\n", pid, rc);
if (rc != 0) {
printf(1, " **** TEST PASSED ****\n\n\n");
} else {
printf(2, " setPriority should have indicated failed.\n");
printf(2, " **** TEST FAILED ****\n\n");
newPriority = getpriority(pid);
if (newPriority != originalPriority) {
printf(2, " setPriority failed but the priority was changed.\n");
printf(2, " Original priority was %d; new priority is %d.\n", originalPriority, newPriority);
printf(2, " **** TEST FAILED ****\n\n");
}
}
// Test that the priority on a bogus PID cannot be set
printf(1, " Testing that a priority cannot be set on a non-existent PID.\n");
rc = setpriority(32767, MAXPRIO);
if (rc != 0) {
printf(1, " **** TEST PASSED ****\n\n\n");
} else {
printf(2, " Attempted to set the priority of PID 32767 to %d.\n", MAXPRIO);
printf(2, " This should have returned a non-zero value but returned %d.\n", rc);
printf(2, " **** TEST FAILED ****\n\n");
}
// Test the priority of a known PID:
pid = 1; // init
int prio = getpriority(pid);
printf(1, "Priority for pid %d is %d\n", pid, prio);
printf(1, "Press C-p to verify.\n");
sleep(5 * TPS);
}
int
main(int argc, char* argv[])
{
if (MAXPRIO == 0) {
printf(1, "MAXPRIO is 0. Change MAXPRIO and try again\n");
exit();
}
checkPriority();
testPromotion();
exit();
}