Skip to content

Instantly share code, notes, and snippets.

@felixge

felixge/main-linux.c

Last active Jun 25, 2020
Embed
What would you like to do?
// Example program showing that signal delivery from setitimer ends up in
// "random" threads of the executing program.
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void sighandler(int);
void thread_loop();
int main(int argc, char *argv[]) {
// Setup SIGPROF handler
if (signal(SIGPROF, sighandler) == SIG_ERR) {
perror("Unable to install sighandler");
exit(1);
}
// Spawn a thread
pthread_t thread;
if(pthread_create(&thread, NULL, (void *)thread_loop, NULL)) {
perror("Unable to create thread");
return 1;
}
// Setup itimer to fire every 1s
struct itimerval it_val;
it_val.it_value.tv_sec = 1;
it_val.it_value.tv_usec = 0;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_PROF, &it_val, NULL) == -1) {
perror("error calling setitimer()");
exit(1);
}
pthread_t tid = pthread_self();
printf("hello from main: %ld\n", tid);
while (1) {}
}
void thread_loop() {
pthread_t tid = pthread_self();
printf("hello from thread: %ld\n", tid);
while (1) {}
}
void sighandler(int signo) {
struct timeval t;
gettimeofday(&t, NULL);
pthread_t tid = pthread_self();
printf("%ld.%ld received signal: %d in thread: %ld\n", t.tv_sec, t.tv_usec, signo, tid);
}
$ gcc -o main-linux main-linux.c -lpthread && ./main-linux
hello from main: 140078699435840
hello from thread: 140078699431680
1593088870.606221 received signal: 27 in thread: 140078699435840
1593088871.106247 received signal: 27 in thread: 140078699435840
1593088871.606259 received signal: 27 in thread: 140078699435840
1593088872.106086 received signal: 27 in thread: 140078699431680
1593088872.607236 received signal: 27 in thread: 140078699435840
1593088873.107239 received signal: 27 in thread: 140078699435840
1593088873.607236 received signal: 27 in thread: 140078699435840
1593088874.107203 received signal: 27 in thread: 140078699435840
1593088874.607278 received signal: 27 in thread: 140078699435840
1593088875.107090 received signal: 27 in thread: 140078699435840
1593088875.607041 received signal: 27 in thread: 140078699435840
1593088876.107234 received signal: 27 in thread: 140078699431680
1593088876.574450 received signal: 27 in thread: 140078699431680
1593088877.74682 received signal: 27 in thread: 140078699435840
1593088877.574719 received signal: 27 in thread: 140078699435840
1593088878.76611 received signal: 27 in thread: 140078699435840
1593088878.577650 received signal: 27 in thread: 140078699435840
1593088879.77635 received signal: 27 in thread: 140078699435840
1593088879.577668 received signal: 27 in thread: 140078699435840
1593088880.77569 received signal: 27 in thread: 140078699435840
1593088880.578492 received signal: 27 in thread: 140078699435840
1593088881.79661 received signal: 27 in thread: 140078699435840
1593088881.579681 received signal: 27 in thread: 140078699435840
1593088882.79661 received signal: 27 in thread: 140078699435840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment