LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2004, 06:39 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Catching a signal?


I have problem compiling examples for catching a signal.

I get: no matching converting function 'handler' to type void (*) (int)
on sa.sa_handler = handler;

the same error happens for signal(sig,handler) method.

This is om a mandrake 9.1 and I use gcc with kdevelop.

Are linux signals not compatible with unix or is it any thing else?

Any idees?

Example:
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

/* Simple signal handler */
void handler(int sig) {
if (sig == SIGINT)
printf("got SIGINT\n");
else
printf("got SIGTSTP\n");
};

int main(void) {
struct sigaction sa;
struct sigaction oldint;

/* Initialize the sa structure */
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;

/* Set up the signal handlers; remember the old
handler for SIGINT so we can restore it later */
sigaction(SIGINT, &sa, &oldint);
sigaction(SIGTSTP, &sa, NULL);

/* Sleep for five seconds, or until a signal wakes
us up*/
sleep(5);

/* Restore the behavior of SIGINT just to show it can
be done. */
sigaction(SIGINT, &oldint, NULL);

return 0;
};
 
Old 02-11-2004, 09:36 AM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Why don't you use `signal ()' istead? Its more easy to use I think. Take a look at the man page.
 
Old 02-11-2004, 11:12 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Works perfectly copied - pasted from your post.
Compiled on Debian with gcc 3.3.
 
Old 02-11-2004, 12:23 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya, I am with Hko... I usually use the signal function not sigaction, but your code runs just fine on my machine with gcc version 3.3.2 20040119 (Gentoo Linux 3.3.2-r7, propolice-3.3-7).
 
Old 02-12-2004, 06:24 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
I think the problem is kdevelop. I will try to compile in a shell.

signal() gives the same error.

Thanks!
 
Old 02-12-2004, 08:54 AM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Ok I solved the problem:

I tried to make the handler a class member function and it did not work. So I made a handler that calls a static function in my class since I only got one(desktop and WM).
It compiles but is it some design faults here?

I will use the signal() since all of you recommended it.

By the way. NICE spellchecker on this site.

My test prog.
catsig.h:

#ifndef CATSIG_H
#define CATSIG_H

class catsig {
public:
catsig();
~catsig();
static void amethod();
};

#endif

catsig.cpp:
catsig::catsig(){

struct sigaction sa;
struct sigaction oldint;

/* Initialize the sa structure */
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;

/* Set up the signal handlers; remember the old
handler for SIGINT so we can restore it later */
sigaction(SIGINT, &sa, &oldint);
sigaction(SIGTSTP, &sa, NULL);

/* Sleep for five seconds, or until a signal wakes
us up*/
sleep(5);

/* Restore the behavior of SIGINT just to show it can
be done. */
sigaction(SIGINT, &oldint, NULL);
}

catsig::~catsig(){}

void catsig::amethod(){}
 
Old 02-12-2004, 09:44 AM   #7
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
sorry the handler did not join the code:

Here is the code:

catsig.h
class catsig
{
public:
catsig();
~catsig();
static void amethod();
};


catsig.cpp
#include "catsig.h"
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

/* Simple signal handler */
void handler(int sig)
{
if (sig == SIGINT)
printf("got SIGINT\n");
else
printf("got SIGTSTP\n");

catsig::amethod();
};

catsig::catsig()
{
struct sigaction sa;
struct sigaction oldint;

/* Initialize the sa structure */
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;

/* Set up the signal handlers; remember the old handler for SIGINT so we can restore it later */
sigaction(SIGINT, &sa, &oldint);
sigaction(SIGTSTP, &sa, NULL);

/* Sleep for five seconds, or until a signal wakes us up*/
sleep(5);

/* Restore the behavior of SIGINT just to show it can be done. */
sigaction(SIGINT, &oldint, NULL);
}

catsig::~catsig(){}

void catsig::amethod(){}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
squid stops due to signal 6 and signal 25 simplyrahul Linux - Software 3 05-28-2011 01:05 AM
Bash signal catching meblost Programming 3 11-22-2004 07:38 PM
Catching a Hacker... Shr00mBoXx Linux - Security 14 06-30-2004 09:59 PM
Signal 6: Unknown Signal <=> Error with MAYA4.X under Linux SOLVED!!!! Faeroon Linux - Software 9 05-09-2003 01:57 PM
Signal: 6 (Unknown Signal) - Problems running a program Faeroon Linux - Software 86 01-14-2003 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration