1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1996-1999 5 * HD Associates, Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by HD Associates, Inc 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * $FreeBSD$ 34 * 35 */ 36 37 /* XXX: The spec says that if _POSIX_C_SOURCE is defined then 38 * _POSIX_SOURCE is ignored. However, this is similar to 39 * the code in the O'Reilly "POSIX.4" book 40 */ 41 42 #define _POSIX_VERSION 199309L 43 #define _POSIX_SOURCE 44 #define _POSIX_C_SOURCE 199309L 45 46 #include <sys/mman.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <limits.h> 50 #include <sched.h> 51 #include <stdio.h> 52 #define __XSI_VISIBLE 1 53 #include <stdlib.h> 54 #undef __XSI_VISIBLE 55 #include <string.h> 56 #include <unistd.h> 57 58 #include "prutil.h" 59 60 static FILE *verbose; 61 62 static void 63 checkpris(int sched) 64 { 65 int smin; 66 int smax; 67 68 errno = 0; 69 70 if ( (smin = sched_get_priority_min(sched)) == -1 && errno) 71 quit("sched_get_priority_min"); 72 73 if ( (smax = sched_get_priority_max(sched)) == -1 && errno) 74 quit("sched_get_priority_max"); 75 76 if (smax - smin + 1 < 32 || smax < smin) { 77 fprintf(stderr, "Illegal priority range for %s: %d to %d\n", 78 sched_text(sched), smin, smax); 79 exit(-1); 80 } 81 82 if (verbose) 83 fprintf(verbose, "%12s: sched_min %2d sched_max %2d\n", 84 sched_text(sched), smin, smax); 85 } 86 87 /* Set "try_anyway" to quit if you don't want to go on when 88 * it doesn't look like something should work. 89 */ 90 static void try_anyway(const char *s) 91 { 92 fputs(s, stderr); 93 fprintf(stderr, "(trying anyway)\n"); 94 errno = 0; 95 } 96 97 static void q(int line, int code, const char *text) 98 { 99 if (code == -1) 100 { 101 fprintf(stderr, "Error at line %d:\n", line); 102 perror(text); 103 exit(errno); 104 } 105 } 106 107 int sched(int ac, char *av[]) 108 { 109 int fifo_schedmin, fifo_schedmax; 110 int i; 111 struct sched_param rt_param; 112 int n_instances = 10; 113 int sched; 114 115 verbose = 0; 116 117 #if _POSIX_VERSION < 199309 118 try_anyway("The _POSIX_VERSION predates P1003.1B\n"); 119 #endif 120 121 #if !defined(_POSIX_PRIORITY_SCHEDULING) 122 try_anyway( 123 "The environment does not claim to support Posix scheduling.\n"); 124 #endif 125 126 /* Is priority scheduling configured? 127 */ 128 errno = 0; 129 if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) { 130 if (errno != 0) { 131 /* This isn't valid - may be a standard violation 132 */ 133 quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)"); 134 } 135 else { 136 try_anyway( 137 "The environment does not have run-time " 138 "support for Posix scheduling.\n"); 139 } 140 } 141 142 /* Check that the priorities seem reasonable. 143 */ 144 145 checkpris(SCHED_FIFO); 146 checkpris(SCHED_RR); 147 checkpris(SCHED_OTHER); 148 149 /* BSD extensions? 150 */ 151 #if defined(SCHED_IDLE) 152 checkpris(SCHED_IDLE); 153 #endif 154 155 fifo_schedmin = sched_get_priority_min(SCHED_FIFO); 156 fifo_schedmax = sched_get_priority_max(SCHED_FIFO); 157 158 /* Make sure we can do some basic schedule switching: 159 */ 160 { 161 struct sched_param orig_param, shouldbe; 162 int orig_scheduler = sched_is(__LINE__, &orig_param, -1); 163 164 if (verbose) 165 fprintf(verbose, 166 "The original scheduler is %s and the priority is %d.\n", 167 sched_text(orig_scheduler), orig_param.sched_priority); 168 169 /* Basic check: Try to set current settings: 170 */ 171 q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param), 172 "sched_setscheduler: Can't set original scheduler"); 173 174 rt_param.sched_priority = fifo_schedmin; 175 176 q(__LINE__, sched_setscheduler(0, SCHED_FIFO, &rt_param), 177 "sched_setscheduler SCHED_FIFO"); 178 179 (void)sched_is(__LINE__, 0, SCHED_FIFO); 180 181 q(__LINE__, sched_getparam(0, &shouldbe), "sched_getparam"); 182 183 if (shouldbe.sched_priority != fifo_schedmin) 184 quit("sched_setscheduler wrong priority (min)"); 185 186 rt_param.sched_priority = fifo_schedmin; 187 188 q(__LINE__, sched_setparam(0, &rt_param), 189 "sched_setparam to fifo_schedmin"); 190 191 rt_param.sched_priority = fifo_schedmin + 1; 192 193 q(__LINE__, sched_setparam(0, &rt_param), 194 "sched_setparam to fifo_schedmin + 1"); 195 196 q(__LINE__, sched_getparam(0, &shouldbe), 197 "sched_getparam"); 198 199 if (shouldbe.sched_priority != fifo_schedmin + 1) 200 quit("sched_setscheduler wrong priority (min + 1)"); 201 202 q(__LINE__, sched_setscheduler(0, SCHED_RR, &rt_param), 203 "sched_setscheduler SCHED_RR"); 204 205 (void)sched_is(__LINE__, 0, SCHED_RR); 206 207 q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param), 208 "sched_setscheduler restoring original scheduler"); 209 210 (void)sched_is(__LINE__, 0, orig_scheduler); 211 } 212 213 214 { 215 char nam[] = "P1003_1b_schedXXXXXX"; 216 int fd; 217 pid_t p; 218 pid_t *lastrun; 219 220 fd = mkstemp(nam); 221 if (fd == -1) 222 q(__LINE__, errno, "mkstemp failed"); 223 224 (void)unlink(nam); 225 226 p = (pid_t)0; 227 228 write(fd, &p, sizeof(p)); 229 230 q(__LINE__, (int)(lastrun = mmap(0, sizeof(*lastrun), PROT_READ|PROT_WRITE, 231 MAP_SHARED, fd, 0)), "mmap"); 232 233 /* Set our priority at the highest: 234 */ 235 sched = SCHED_FIFO; 236 rt_param.sched_priority = fifo_schedmax; 237 q(__LINE__, sched_setscheduler(0, sched, &rt_param), 238 "sched_setscheduler sched"); 239 240 for (i = 0; i < n_instances; i++) 241 { 242 pid_t me; 243 244 /* XXX This is completely bogus. The children never run. 245 */ 246 if ((me = fork()) != 0) 247 { 248 /* Parent. 249 */ 250 (void)sched_is(__LINE__, 0, sched); 251 252 /* Lower our priority: 253 */ 254 rt_param.sched_priority--; 255 256 q(__LINE__, sched_setscheduler(0, sched, &rt_param), 257 "sched_setscheduler sched"); 258 259 while (1) 260 { 261 q(__LINE__, sched_getparam(0, &rt_param), "sched_getparam"); 262 263 rt_param.sched_priority--; 264 265 266 if (rt_param.sched_priority < fifo_schedmin) 267 exit(0); 268 269 *lastrun = me; 270 q(__LINE__, sched_setparam(0, &rt_param), "sched_setparam"); 271 272 if (*lastrun == me) 273 { 274 /* The child will run twice 275 * at the end: 276 */ 277 if (!me || rt_param.sched_priority != 0) 278 { 279 fprintf(stderr, 280 "ran process %ld twice at priority %d\n", 281 (long)me, rt_param.sched_priority + 1); 282 exit(-1); 283 } 284 } 285 } 286 } 287 } 288 } 289 290 return 0; 291 } 292 #ifdef STANDALONE_TESTS 293 int main(int argc, char *argv[]) { return sched(argc, argv); } 294 #endif 295