Last active
January 27, 2023 03:18
-
-
Save KJTsanaktsidis/f7799c426ea3b681c76ccd3d2ed99e58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <sched.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/wait.h> | |
#include <time.h> | |
#include <unistd.h> | |
int main_parent(pid_t child_pid) { | |
sleep(1); | |
struct timespec tsig; | |
clock_gettime(CLOCK_MONOTONIC, &tsig); | |
union sigval sv; | |
sv.sival_int = 0; | |
int r = sigqueue(child_pid, SIGUSR1, sv); | |
if (r == -1) { | |
perror("sigqueue(2"); | |
exit(1); | |
} | |
printf("sent signal at: %ld %ld\n", tsig.tv_sec, tsig.tv_nsec); | |
return 0; | |
} | |
void handler(int signo, siginfo_t *info, void *ctx) { | |
struct timespec tsig; | |
clock_gettime(CLOCK_MONOTONIC, &tsig); | |
printf("received signal at: %ld %ld\n", tsig.tv_sec, tsig.tv_nsec); | |
exit(0); | |
} | |
int main_child(void) { | |
struct sigaction act = { 0 }; | |
act.sa_flags = SA_SIGINFO; | |
act.sa_sigaction = &handler; | |
int r = sigaction(SIGUSR1, &act, NULL); | |
if (r == -1) { | |
perror("sigaction(2)"); | |
exit(1); | |
} | |
for (;;) { | |
// try having or not having sched_yield in here; results are similar. | |
// sched_yield(); | |
} | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
pid_t pid = fork(); | |
if (pid == -1) { | |
perror("fork(2)"); | |
exit(1); | |
} else if (pid == 0) { | |
exit(main_child()); | |
} else { | |
int r = main_parent(pid); | |
int status; | |
pid_t ret = waitpid(pid, &status, 0); | |
if (ret == -1) { | |
perror("waitpid(2)"); | |
exit(1); | |
} | |
exit(r || status || 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment