Created
September 16, 2021 15:37
-
-
Save lightdiscord/7e6ffe2ca47901f39d3e53bb608e4862 to your computer and use it in GitHub Desktop.
Observation on signal accumulation
This file contains 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 <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(); | |
} | |
} |
This file contains 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
$ 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