Problem using signal() and kill()!

Hi guys,

I'm new on c++ programming and I'm learning how the signal between processes work. (Btw didn't know if I was supposed to post on the beginners forum or in the linux... Admin feel free to move. )

My idea was to create a process and this process would send a signal to his father... But I had no success. I get a bunch of errors and warnings. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#define max 10

typedef void handler(int);
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
This part is a bit confusing and I don't think you need it.
1
2
3
4
typedef void handler(int);
void (*hand_pt)(int);

hand_pt = &handler;

If you remove the above lines and change line 30 to signal(SIGUSR1, handler); I think it should work.
Problem solved.

The thing was that I needed th declare hand_pt = &handler inside a function and not globally

declaring it inside main made it work.

Close topic and thanks to peter87 also. ;)
Hi guys, forum noob here. I've been poking through the forum to find an answer to a question I have that is very similar to pfpietro's so I thought I'd ask it here instead of starting another topic. Hope this was right.

Problem: My sighandler cannot 'see' another function that it needs to call.

App.h snippet:
1
2
3
4
5
6
7
8
9
10
class Application : public AbstractApp {
    protected:
        AbstractContainer * container;
        bool processArguments();
    public:
        Application(int arg, char ** args);
        virtual ~Application();
        void restartApp();
        void startApp();
};

And the problem code snippet from App.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Application::restartApp() {
    container->saveState();
    cout << "restarting App\n";
    delete container;
    execvp( argv[0], argv );
}
void signalhandler(int sig) {
    if(sig == SIGCHLD){
      int status;
      waitpid(-1, &status, WNOHANG|WUNTRACED);
    } else if ( sig == SIGHUP ) {
        restartApp();  //<--- PROBLEM LINE
    }else{
      _exit(1);
    }
}

The error is:
1
2
3
4
App.cpp: In function `void signalhandler(int)':
App.cpp:82: error: `restartApp' undeclared (first use this function)
App.cpp:82: error: (Each undeclared identifier is reported only once for each function it appears in.)
make: *** [App.o] Error 1

What can I do to make sighandler() 'see' restartApp()? I've tried every permutation I can think of and it just coughs more errors.
unless sighandler() is a member function of Application wouldn't you need to reference the instance of Application to call restartApp() ?

Application myApp;
....
myApp->restartApp();

?
Thanks for replying so quickly Texan40.

I tried that:
1
2
3
4
5
6
7
8
9
10
11
void signalhandler(int sig){
    Application myApp; //<--- new reference
    if ( sig == SIGCHLD ) {
        int status;
        waitpid(-1, &status, WNOHANG|WUNTRACED);
    } else if ( sig == SIGHUP ) {
        myApp->restartApp();
    }else{
      _exit(1);
    }
}

And what I got was:
1
2
3
4
5
6
App.cpp: In function `void signalhandler(int)':
App.cpp:79: error: no matching function for call to `Application::Application()'
App.h:39: note: candidates are: Application::Application(const Application&)
App.cpp:29: note:                 Application::Application(int, char**)
App.cpp:84: error: base operand of `->' has non-pointer type `Application'
make: *** [App.o] Error 1

Still mystified.
Hi Miven,

Line 79 is complaining about a missing default constructor, which is what you are calling (implicitly) on that line.

It should be myApp.restartApp(), since myApp is an object, not a pointer to one.
Hi Kooth. Thank you, but...

I tried that and it coughs:
1
2
3
4
5
App.cpp: In function `void signalhandler(int)':
App.cpp:79: error: no matching function for call to `Application::Application()'
App.h:39: note: candidates are: Application::Application(const Application&)
App.cpp:29: note:                 Application::Application(int, char**)
make: *** [App.o] Error 1


Hey, it didn't complain about the base operand. One less error.

I thought this was going to be a simple hack to a half built program, but now I see the code needs a lot of work. I might just rewrite the whole thing in C, using glib and stuff. A nice winter project :)

Anyway, some of the code is informative, and obviously a lot of work went into it, so I'll keep it around for reference. A bunch of usable Imlib2 and Xft stuff.

To Kooth and Texan40, Thank you for trying.
_ A method works on an instance of the class. That means that you need to use/modify the status
To make it clear
1
2
void Application::restartApp()
void restartApp( Application &self )
are equivalent.

_ To create an instance you need to call some constructor.
The problem is that you don't want to create another instance.
Because the handler doesn't allow you to pass it as parameter, you will need to make it global.
Topic archived. No new replies allowed.