Skip to content

Instantly share code, notes, and snippets.

@lightdiscord
Created September 16, 2021 15:37
Show Gist options
  • Save lightdiscord/7e6ffe2ca47901f39d3e53bb608e4862 to your computer and use it in GitHub Desktop.
Save lightdiscord/7e6ffe2ca47901f39d3e53bb608e4862 to your computer and use it in GitHub Desktop.
Observation on signal accumulation
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <stdio.h>
#include <sys/wait.h>
void handle_sender(pid_t receiver) {
for (int i = 0; i < 10; i++) {
usleep(100);
puts("Signal sent");
kill(receiver, SIGUSR1);
}
}
void sig_handler(int _) {
(void)_;
puts("Signal received");
usleep(500);
puts("Signal received [done]");
}
void handle_receiver(void) {
signal(SIGUSR1, sig_handler);
while (1) {
pause();
}
}
int main(void) {
pid_t pid = fork();
assert(pid != -1);
if (pid) {
int status;
handle_sender(pid);
waitpid(pid, &status, 0);
}
else {
handle_receiver();
}
}
$ gcc ./main.c -o ./main && ./main
Signal sent
Signal received
Signal sent
Signal sent
Signal sent
Signal sent
Signal received [done]
Signal received
Signal sent
Signal sent
Signal sent
Signal received [done]
Signal received
Signal sent
Signal sent
Signal received [done]
Signal received
Signal received [done]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment