Update
[apc.git] / hog.c
blob2b15d1defd753e4fad14c5d5ad51e2b666824fb1
1 /* cc -o hog hog.c */
2 #define _POSIX_PTHREAD_SEMANTICS
3 #include <time.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <limits.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <sys/time.h>
13 #define HIST 10
15 static volatile sig_atomic_t stop;
17 static void sighandler (int signr)
19 (void) signr;
20 stop = 1;
23 static unsigned long hog (unsigned long niters)
25 stop = 0;
26 while (!stop && --niters)
28 return niters;
31 static void err (int status, const char *fmt, ...)
33 va_list ap;
34 int errno_code = errno;
36 va_start (ap, fmt);
37 vfprintf (stderr, fmt, ap);
38 va_end (ap);
39 fprintf (stderr, ": %s\n", strerror (errno_code));
40 exit (status);
43 int main (void)
45 unsigned int i;
46 struct itimerval it;
47 struct sigaction act;
48 sigset_t set;
49 unsigned long v[HIST];
50 double tmp = 0.0;
51 unsigned long n;
53 act.sa_handler = sighandler;
54 if (sigemptyset (&act.sa_mask)) {
55 err (EXIT_FAILURE, "sigemptyset failed");
57 act.sa_flags = 0;
59 it.it_interval.tv_sec = 0;
60 it.it_interval.tv_usec = 1000000 / 1000;
61 it.it_value.tv_sec = 0;
62 it.it_value.tv_usec = 1000000 / 1000;
64 if (sigaction (SIGALRM, &act, NULL)) {
65 err (EXIT_FAILURE, "sigaction failed");
68 if (setitimer (ITIMER_REAL, &it, NULL)) {
69 err (EXIT_FAILURE, "setitimer failed");
72 hog (ULONG_MAX);
73 for (i = 0; i < HIST; ++i) {
74 v[i] = ULONG_MAX - hog (ULONG_MAX);
77 for (i = 0; i < HIST; ++i) {
78 printf ("%d = %ld\n", i, v[i]);
79 tmp += v[i];
81 tmp /= HIST;
82 n = tmp - (tmp / 3);
84 if (sigemptyset (&set)) {
85 err (EXIT_FAILURE, "sigemptyset failed");
88 if (sigaddset (&set, SIGALRM)) {
89 err (EXIT_FAILURE, "sigaddset failed");
92 for (;;) {
93 int signr;
95 hog (n);
96 if (sigwait (&set, &signr)) {
97 err (EXIT_FAILURE, "sigwait failed");