1
0
mirror of http://www.pogo.org.uk/~mark/trx.git synced 2024-09-28 22:45:06 +02:00
trx/sched.c
Mark Hills 0c5ab0dfc1 Do not use the ambiguous sched_setscheduler()
POSIX specifies this is per-process, and GNU implemented it on the
thread. Musl realises the problem with this and so doesn't implement
it at all, forcing this more specific alternative to be used.
2023-05-12 16:31:03 +01:00

80 lines
1.6 KiB
C

/*
* Copyright (C) 2020 Mark Hills <mark@xwax.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <unistd.h>
#include "sched.h"
#define REALTIME_PRIORITY 80
int go_realtime(void)
{
int max_pri;
const struct sched_param sp = {
.sched_priority = REALTIME_PRIORITY,
};
max_pri = sched_get_priority_max(SCHED_FIFO);
if (sp.sched_priority > max_pri) {
fprintf(stderr, "Invalid priority (maximum %d)\n", max_pri);
return -1;
}
errno = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp);
if (errno) {
perror("pthread_setschedparam");
return -1;
}
return 0;
}
int go_daemon(const char *pid_file)
{
FILE *f;
if (daemon(0, 0) == -1) {
perror("daemon");
return -1;
}
if (!pid_file)
return 0;
f = fopen(pid_file, "w");
if (!f) {
perror("fopen");
return -1;
}
fprintf(f, "%d", getpid());
if (fclose(f) != 0) {
perror("fclose");
return -1;
}
return 0;
}