#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>#include <sched.h>
#include <sys/resource.h>
typedef void (*sighandler_t)(int);
sighandler_t sighdlr(int);
/* Signal handler */
sighandler_t sighdlr(int sig) {
if ( sig == EINTR ) // It's just a signal, try again
return NULL;
exit(sig);
}
static const char * const msg = "Hello World!\n";
int main() {
int s;
int max_prio;
size_t lenstr;
size_t remaining;
ssize_t res;
const char *begin;
const char *end;
struct sched_param shd;
/* set prio */
max_prio = sched_get_priority_max(SCHED_OTHER);
shd.sched_priority = max_prio;
if (sched_setscheduler(0, SCHED_OTHER, &shd) != 0)
exit(0);
errno = 0;
lenstr = strlen(msg);
if (errno != 0) {
switch (errno) {
case EINVAL:
case EFAULT:
case EFBIG: /* Too big */
case EAGAIN:
default:
return (errno);
}
}
/* Unbuffered out */
errno = 0;
s = setvbuf(stdout, (char *) NULL, _IOLBF, 0);
if (s != 0 || errno != 0)
return (errno);
/* Run signal handler */
for (s = 1; s < (_NSIG / 2); s++) {
if (s != SIGKILL || s != SIGSTOP) {
errno = 0;
signal(s, (sighandler_t) sighdlr);
if (errno != 0 && errno == EINVAL) {
continue; /* signul is invalid */
}
} else
continue;
}
begin = msg;
end = begin + lenstr;
while (begin < end) {
remaining = end - begin;
errno = 0;
res = write(STDOUT_FILENO, begin, remaining);
if (errno != 0) {
switch (errno) {
case EBADF:
case EINVAL:
case EFAULT:
case EFBIG:
case EPIPE:
case EAGAIN:
case ENOSPC:
case EIO:
default:
return (errno);
}
}
if (res >= 0) {
begin += res;
continue; // Let's send the remaining part of this message
} else {
return (EXIT_FAILURE); // It's a real error
}
}
return (EXIT_SUCCESS); // OK! Let's celebrate and drink some beer!
}