1
0
Fork 0
A test of the feasibility of hardware pipes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

113 lines
1.7 KiB

#define _GNU_SOURCE
#define _XOPEN_SOURCE (700)
#include <stdlib.h>
#include <stdint.h>
#include <sched.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <stdbool.h>
#include <semaphore.h>
#include <stdio.h>
#include "time.c"
#define CONSUMER(p) ((p) == 0)
#ifdef AFFINITY
#define STR "AFFINITY "
#else // AFFINITY
#define STR ""
#endif // AFFINITY
#define LIMIT ((size_t) 100000000)
#define LIMIT2 (LIMIT / 2)
unsigned int nsignals;
pid_t parent;
pid_t child;
sem_t sem;
void sighandler(int sig)
{
nsignals += 1;
if (CONSUMER(child))
{
kill(parent, SIGUSR1);
if (nsignals >= LIMIT2) sem_post(&sem);
}
else if (nsignals < LIMIT2) kill(child, SIGUSR1);
else sem_post(&sem);
}
int main()
{
cpu_set_t set;
TimeSpec start, end, diff;
struct sigaction sa;
int status;
parent = getpid();
nice(-20);
sigemptyset(&sa.sa_mask);
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
child = fork();
if (child < 0) err("fork failed", 1);
sem_init(&sem, 0, 0);
#ifdef AFFINITY
CPU_ZERO(&set);
if (CONSUMER(child)) CPU_SET(1, &set);
else CPU_SET(0, &set);
if (sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
{
err("could not set cpu affinity", 2);
}
#endif // AFFINITY
if (!CONSUMER(child))
{
ts_time(&start);
kill(child, SIGUSR1);
}
sem_wait(&sem);
if (!CONSUMER(child))
{
ts_time(&end);
waitpid(child, &status, 0);
if (WEXITSTATUS(status) != 0) err("child exited bad", 11);
diff = ts_diff(end, start);
printf(STR "SIGNALS: ");
ts_print(diff, LIMIT);
printf("\n");
}
sem_destroy(&sem);
return 0;
}