1

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?

1
  • 2
    Is this a poor cut/paste or do you have your signal handler nested within main()? Among other problems, argv[2] would be the PID to pass to kill, not argv[1]. Commented Feb 28, 2013 at 20:01

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.