Hi guys,

I'm facing a new problem. Now I'm learning how to send signals to process in order to make them communicate. I was trying to create a process and when this process is created, send a signal to his father... But I had no success. I get a bunch of errors and warnings. Here's the code:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#define max 10

void handler(int signo);
void (*hand_pt)(int);

hand_pt = &handler;

int ppid, pid[max], i, j, k, n;

// So I created a pointer to a funcion (hand_pt)
// and this pointer is going to point to the funcion (handler)



void handler(int signo)
{

printf("It worked");
}


main(int argc, char **argv)
{

signal(SIGUSR1, hand_pt);
//this process will wait for the signal SIGUSR1
//and will be handled by hand_pt -> handler 

pid[0]=fork();

if(pid[0]==0)
    {
    printf("Sending signal:\n\n");
    ppid=getppid();
    kill(ppid, SIGUSR1);
    exit(0);
    }
return(0);
}

Maybe I didn't understand how the signal function works... But as far as I know, to use a handler function I have to create a pointer to that function (in this case void (*hand_pt)(int); ) and assign to this pointer the address of the function that will handle the signal ( hand_pt = &handler; ). Is my line of tought correct ?

After that I'd be able to send a signal with the kill() fcn . What am I doing wrong ?

Thanks

But I had no success. I get a bunch of errors and warnings

Could you post them here?

pubuntu@pubuntu:~$ gcc teste.c -o teste
teste.c:11: warning: data definition has no type or storage class
teste.c:11: error: conflicting types for ‘hand_pt’
teste.c:9: note: previous declaration of ‘hand_pt’ was here
teste.c:11: error: expected expression before ‘handler’
teste.c:20: error: ‘handler’ redeclared as different kind of symbol
teste.c:8: note: previous declaration of ‘handler’ was here
teste.c: In function ‘main’:
teste.c:30: warning: passing argument 2 of ‘signal’ makes pointer from integer without a cast
/usr/include/signal.h:101: note: expected ‘__sighandler_t’ but argument is of type ‘int’

Thanks, myk45
Putting hand_pt = &handler inside the main() I can compile and everyithing. But it still does not work. All I get is the program stuck ( I think after sending th signal to the father.

He doesn't go further... What Im I doing wrong ?
I declared the pointer, and inside the child process code I use kill(ppid, SIGUSR1) but he doesn't get the signal...

I don't understand why, since in the first lines in main() I used signal(SIGUSR1, hand_pt)

Forget it... was the exit(0); after the kill instruction... Don't know why tough

Sleep a bit before exit(0). I think you are running into a race condition and your parent is getting a SIGCHLD signal (you dieing) about the same time it gets the SIGUSR1 signal.

FWIW, using signals to communicate between processes is very dangerous and error-prone. It's use is common enough, but it is very easy to get into bad situations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.