Skip to content

Instantly share code, notes, and snippets.

@dvyukov
Created November 16, 2015 21:39
Show Gist options
  • Save dvyukov/75fd37f9ad6747abfe5f to your computer and use it in GitHub Desktop.
Save dvyukov/75fd37f9ad6747abfe5f to your computer and use it in GitHub Desktop.
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <pthread.h>
void *thr(void *arg) {
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_signo = SIGURG;
timer_t timerid;
if (syscall(SYS_timer_create, CLOCK_REALTIME, &sev, &timerid))
exit(printf("timer_create: %d\n", errno));
itimerspec new_value;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
new_value.it_value.tv_sec = 0;
new_value.it_value.tv_nsec = 500;
if (syscall(SYS_timer_settime, timerid, TIMER_ABSTIME, &new_value, NULL))
exit(printf("timer_settime: %d\n", errno));
exit(1);
return 0;
}
int main() {
int fd[2];
if (pipe(&fd[0]))
exit(printf("pipe failed: %d\n", errno));
int pid = fork();
if (pid < 0)
exit(printf("fork failed: %d\n", errno));
if (pid == 0) {
int pid2 = fork();
if (pid2 < 0)
exit(printf("fork failed: %d\n", errno));
if (pid2 == 0) {
pthread_t th;
pthread_create(&th, 0, thr, 0);
for (;;)
sleep(1);
}
for (;;)
sleep(1);
}
close(fd[1]);
usleep(100000);
kill(pid, SIGKILL);
char tmp;
int n = read(fd[0], &tmp, 1);
printf("read=%d/%d\n", n, errno);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment