2 * Copyright (c) 1996-1999
3 * HD Associates, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by HD Associates, Inc
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/tools/regression/p1003_1b/sched.c,v 1.1 2000/02/16 14:28:42 dufault Exp $
32 * $DragonFly: src/tools/regression/p1003_1b/sched.c,v 1.3 2007/06/26 23:30:05 josepht Exp $
36 /* XXX: The spec says that if _POSIX_C_SOURCE is defined then
37 * _POSIX_SOURCE is ignored. However, this is similar to
38 * the code in the O'Reilly "POSIX.4" book
41 #define _POSIX_VERSION 199309L
43 #define _POSIX_C_SOURCE 199309L
54 #include <sys/sched.h>
68 if ( (smin
= sched_get_priority_min(sched
)) == -1 && errno
)
69 quit("sched_get_priority_min");
71 if ( (smax
= sched_get_priority_max(sched
)) == -1 && errno
)
72 quit("sched_get_priority_max");
74 if (smax
- smin
+ 1 < 32 || smax
< smin
) {
75 fprintf(stderr
, "Illegal priority range for %s: %d to %d\n",
76 sched_text(sched
), smin
, smax
);
81 fprintf(verbose
, "%12s: sched_min %2d sched_max %2d\n",
82 sched_text(sched
), smin
, smax
);
85 /* Set "try_anyway" to quit if you don't want to go on when
86 * it doesn't look like something should work.
88 static void try_anyway(const char *s
)
91 fprintf(stderr
, "(trying anyway)\n");
95 static void q(int line
, int code
, const char *text
)
99 fprintf(stderr
, "Error at line %d:\n", line
);
105 int sched(int ac
, char *av
[])
107 int fifo_schedmin
, fifo_schedmax
;
109 struct sched_param rt_param
;
110 int n_instances
= 10;
115 #if _POSIX_VERSION < 199309
116 try_anyway("The _POSIX_VERSION predates P1003.1B\n");
119 #if !defined(_POSIX_PRIORITY_SCHEDULING)
121 "The environment does not claim to support Posix scheduling.\n");
124 /* Is priority scheduling configured?
127 if (sysconf(_SC_PRIORITY_SCHEDULING
) == -1) {
129 /* This isn't valid - may be a standard violation
131 quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)");
135 "The environment does not have run-time "
136 "support for Posix scheduling.\n");
140 /* Check that the priorities seem reasonable.
143 checkpris(SCHED_FIFO
);
145 checkpris(SCHED_OTHER
);
149 #if defined(SCHED_IDLE)
150 checkpris(SCHED_IDLE
);
153 fifo_schedmin
= sched_get_priority_min(SCHED_FIFO
);
154 fifo_schedmax
= sched_get_priority_max(SCHED_FIFO
);
156 /* Make sure we can do some basic schedule switching:
159 struct sched_param orig_param
, shouldbe
;
160 int orig_scheduler
= sched_is(__LINE__
, &orig_param
, -1);
164 "The original scheduler is %s and the priority is %d.\n",
165 sched_text(orig_scheduler
), orig_param
.sched_priority
);
167 /* Basic check: Try to set current settings:
169 q(__LINE__
, sched_setscheduler(0, orig_scheduler
, &orig_param
),
170 "sched_setscheduler: Can't set original scheduler");
172 rt_param
.sched_priority
= fifo_schedmin
;
174 q(__LINE__
, sched_setscheduler(0, SCHED_FIFO
, &rt_param
),
175 "sched_setscheduler SCHED_FIFO");
177 (void)sched_is(__LINE__
, 0, SCHED_FIFO
);
179 q(__LINE__
, sched_getparam(0, &shouldbe
), "sched_getparam");
181 if (shouldbe
.sched_priority
!= fifo_schedmin
)
182 quit("sched_setscheduler wrong priority (min)");
184 rt_param
.sched_priority
= fifo_schedmin
;
186 q(__LINE__
, sched_setparam(0, &rt_param
),
187 "sched_setparam to fifo_schedmin");
189 rt_param
.sched_priority
= fifo_schedmin
+ 1;
191 q(__LINE__
, sched_setparam(0, &rt_param
),
192 "sched_setparam to fifo_schedmin + 1");
194 q(__LINE__
, sched_getparam(0, &shouldbe
),
197 if (shouldbe
.sched_priority
!= fifo_schedmin
+ 1)
198 quit("sched_setscheduler wrong priority (min + 1)");
200 q(__LINE__
, sched_setscheduler(0, SCHED_RR
, &rt_param
),
201 "sched_setscheduler SCHED_RR");
203 (void)sched_is(__LINE__
, 0, SCHED_RR
);
205 q(__LINE__
, sched_setscheduler(0, orig_scheduler
, &orig_param
),
206 "sched_setscheduler restoring original scheduler");
208 (void)sched_is(__LINE__
, 0, orig_scheduler
);
213 #define NAM "P1003_1b_schedXXXX"
220 if (tmpnam(nam
) != nam
)
221 q(__LINE__
, errno
, "tmpnam " NAM
);
222 q(__LINE__
, (fd
= open(nam
, O_RDWR
|O_CREAT
, 0666)),
229 write(fd
, &p
, sizeof(p
));
231 q(__LINE__
, (int)(lastrun
= mmap(0, sizeof(*lastrun
), PROT_READ
|PROT_WRITE
,
232 MAP_SHARED
, fd
, 0)), "mmap");
234 /* Set our priority at the highest:
237 rt_param
.sched_priority
= fifo_schedmax
;
238 q(__LINE__
, sched_setscheduler(0, sched
, &rt_param
),
239 "sched_setscheduler sched");
241 for (i
= 0; i
< n_instances
; i
++)
245 /* XXX This is completely bogus. The children never run.
247 if ((me
= fork()) != 0)
251 (void)sched_is(__LINE__
, 0, sched
);
253 /* Lower our priority:
255 rt_param
.sched_priority
--;
257 q(__LINE__
, sched_setscheduler(0, sched
, &rt_param
),
258 "sched_setscheduler sched");
262 q(__LINE__
, sched_getparam(0, &rt_param
), "sched_getparam");
264 rt_param
.sched_priority
--;
267 if (rt_param
.sched_priority
< fifo_schedmin
)
271 q(__LINE__
, sched_setparam(0, &rt_param
), "sched_setparam");
275 /* The child will run twice
278 if (!me
|| rt_param
.sched_priority
!= 0)
281 "ran process %ld twice at priority %d\n",
282 (long)me
, rt_param
.sched_priority
+ 1);
293 #ifdef STANDALONE_TESTS
294 int main(int argc
, char *argv
[]) { return sched(argc
, argv
); }