Reputation: 329
I want my program to wait for a signal which will be SIGUSR1, but currently does not work.
The class that will catches the signal:
#include<signal.h>
#include<unistd.h>
int main(int argc, char *argv[]){
int pid;
pid=getpid();
printf("PID: %d\n", pid);
pause();
void my_handler(int signum)
{
if (signum == SIGUSR1)
{
printf("Received SIGUSR1!\n");
}
}
signal(SIGUSR1, my_handler);
}
Program that will give the signal:
#include<stdlib.h>
#include<signal.h>
#include<unistd.h>
#include<time.h>
int main(int argc, char *argv[]){
int i;
printf("Filename: %s, Receiver PID: %s\n", argv[1], argv[2]);
FILE *fp;
fp=fopen(argv[1], "w");
kill(atol(argv[1]),SIGUSR1);
}
You would first run the receiver like ./receiver 1
then after you find the PID(ex 3375) of the receiver you would run ./signaler 1 3375
Pause is the function i'm using to wait but it's not working, would sigwaitinfo or sigsuspend work?
Upvotes: 1
Views: 8660
Reputation: 3484
I agree that the formatting makes checking this difficult, but it looks like you are calling signal() after pause(). You need to move the signal call to the top of main, or at least before pause.
Upvotes: 1