#define _POSIX_VERSION 199309L
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <stdio.h>
#define __XSI_VISIBLE 1
#include <stdlib.h>
#undef __XSI_VISIBLE
#include <string.h>
#include <unistd.h>
#include "prutil.h"
static FILE *verbose;
static void
checkpris(int sched)
{
int smin;
int smax;
errno = 0;
if ( (smin = sched_get_priority_min(sched)) == -1 && errno)
quit("sched_get_priority_min");
if ( (smax = sched_get_priority_max(sched)) == -1 && errno)
quit("sched_get_priority_max");
if (smax - smin + 1 < 32 || smax < smin) {
fprintf(stderr, "Illegal priority range for %s: %d to %d\n",
sched_text(sched), smin, smax);
exit(-1);
}
if (verbose)
fprintf(verbose, "%12s: sched_min %2d sched_max %2d\n",
sched_text(sched), smin, smax);
}
static void try_anyway(const char *s)
{
fputs(s, stderr);
fprintf(stderr, "(trying anyway)\n");
errno = 0;
}
static void q(int line, int code, const char *text)
{
if (code == -1)
{
fprintf(stderr, "Error at line %d:\n", line);
perror(text);
exit(errno);
}
}
int sched(int ac, char *av[])
{
int fifo_schedmin, fifo_schedmax;
int i;
struct sched_param rt_param;
int n_instances = 10;
int sched;
verbose = 0;
#if _POSIX_VERSION < 199309
try_anyway("The _POSIX_VERSION predates P1003.1B\n");
#endif
#if !defined(_POSIX_PRIORITY_SCHEDULING)
try_anyway(
"The environment does not claim to support Posix scheduling.\n");
#endif
errno = 0;
if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) {
if (errno != 0) {
quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)");
}
else {
try_anyway(
"The environment does not have run-time "
"support for Posix scheduling.\n");
}
}
checkpris(SCHED_FIFO);
checkpris(SCHED_RR);
checkpris(SCHED_OTHER);
#if defined(SCHED_IDLE)
checkpris(SCHED_IDLE);
#endif
fifo_schedmin = sched_get_priority_min(SCHED_FIFO);
fifo_schedmax = sched_get_priority_max(SCHED_FIFO);
{
struct sched_param orig_param, shouldbe;
int orig_scheduler = sched_is(__LINE__, &orig_param, -1);
if (verbose)
fprintf(verbose,
"The original scheduler is %s and the priority is %d.\n",
sched_text(orig_scheduler), orig_param.sched_priority);
q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
"sched_setscheduler: Can't set original scheduler");
rt_param.sched_priority = fifo_schedmin;
q(__LINE__, sched_setscheduler(0, SCHED_FIFO, &rt_param),
"sched_setscheduler SCHED_FIFO");
(void)sched_is(__LINE__, 0, SCHED_FIFO);
q(__LINE__, sched_getparam(0, &shouldbe), "sched_getparam");
if (shouldbe.sched_priority != fifo_schedmin)
quit("sched_setscheduler wrong priority (min)");
rt_param.sched_priority = fifo_schedmin;
q(__LINE__, sched_setparam(0, &rt_param),
"sched_setparam to fifo_schedmin");
rt_param.sched_priority = fifo_schedmin + 1;
q(__LINE__, sched_setparam(0, &rt_param),
"sched_setparam to fifo_schedmin + 1");
q(__LINE__, sched_getparam(0, &shouldbe),
"sched_getparam");
if (shouldbe.sched_priority != fifo_schedmin + 1)
quit("sched_setscheduler wrong priority (min + 1)");
q(__LINE__, sched_setscheduler(0, SCHED_RR, &rt_param),
"sched_setscheduler SCHED_RR");
(void)sched_is(__LINE__, 0, SCHED_RR);
q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
"sched_setscheduler restoring original scheduler");
(void)sched_is(__LINE__, 0, orig_scheduler);
}
{
char nam[] = "P1003_1b_schedXXXXXX";
int fd;
pid_t p;
pid_t *lastrun;
fd = mkstemp(nam);
if (fd == -1)
q(__LINE__, errno, "mkstemp failed");
(void)unlink(nam);
p = (pid_t)0;
write(fd, &p, sizeof(p));
q(__LINE__, (int)(lastrun = mmap(0, sizeof(*lastrun), PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0)), "mmap");
sched = SCHED_FIFO;
rt_param.sched_priority = fifo_schedmax;
q(__LINE__, sched_setscheduler(0, sched, &rt_param),
"sched_setscheduler sched");
for (i = 0; i < n_instances; i++)
{
pid_t me;
if ((me = fork()) != 0)
{
(void)sched_is(__LINE__, 0, sched);
rt_param.sched_priority--;
q(__LINE__, sched_setscheduler(0, sched, &rt_param),
"sched_setscheduler sched");
while (1)
{
q(__LINE__, sched_getparam(0, &rt_param), "sched_getparam");
rt_param.sched_priority--;
if (rt_param.sched_priority < fifo_schedmin)
exit(0);
*lastrun = me;
q(__LINE__, sched_setparam(0, &rt_param), "sched_setparam");
if (*lastrun == me)
{
if (!me || rt_param.sched_priority != 0)
{
fprintf(stderr,
"ran process %ld twice at priority %d\n",
(long)me, rt_param.sched_priority + 1);
exit(-1);
}
}
}
}
}
}
return 0;
}
#ifdef STANDALONE_TESTS
int main(int argc, char *argv[]) { return sched(argc, argv); }
#endif