Created
September 9, 2024 23:43
-
-
Save tivrfoa/50e550adba35f04d2a90b47eaea33e88 to your computer and use it in GitHub Desktop.
C: Sending signal to another process using SIGUSR1
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 <stdio.h> | |
#include <signal.h> | |
#include <unistd.h> | |
void sigusr1_handler(int sig) { | |
printf("------>>> Received SIGUSR1 signal\n"); | |
} | |
int main() { | |
signal(SIGUSR1, sigusr1_handler); | |
pid_t pid = getpid(); | |
printf("My PID is: %d\n", pid); | |
while (1) { | |
sleep(1); | |
printf("Waiting SIGUSR signal...\n"); | |
} | |
return 0; | |
} |
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 <stdio.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s <number>\n", argv[0]); | |
return 1; | |
} | |
int pid = atoi(argv[1]); | |
printf("It will send signal to pid: %d\n", pid); | |
sleep(3); | |
kill(pid, SIGUSR1); | |
printf("Sent SIGUSR1 signal\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/6168636/how-to-trigger-sigusr1-and-sigusr2