Skip to content

Instantly share code, notes, and snippets.

@tivrfoa
Created September 9, 2024 23:43
Show Gist options
  • Save tivrfoa/50e550adba35f04d2a90b47eaea33e88 to your computer and use it in GitHub Desktop.
Save tivrfoa/50e550adba35f04d2a90b47eaea33e88 to your computer and use it in GitHub Desktop.
C: Sending signal to another process using SIGUSR1
#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;
}
#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