LinuxQuestions.org
Help answer threads with 0 replies.
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 06-02-2006, 08:09 PM   #1
oceanfun
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Rep: Reputation: 0
Help on calling signal with SIGUSR1 signum


Hi guys,
I have a problem in my program when calling system function 'signal' with SIGUSR1. Something like this:

void SignalHandle(int iSigNo) {
printf("Got SIGUSR1 handled.\n");
}

int main() {
signal(SIGUSR1, SignalHandle);

ClassA aobj;
aobj.start();

return 0;
}

ClassA {
void start();
};

ClassA::start() {
while(true) {
// something got done in here
}
}

Goal: I want to print out the "Got SIGUSR1 handled" line when executed command "kill -SIGUSR1 <pid>" without terminating my application (which I expect what it should be.)

Problem: The program printed out the line, but it's also killed (which I don't want to). Anyone see what I am doing wrong here? How to fix it please.

Thanks in advance for your help.

OceanFun
 
Old 06-04-2006, 03:29 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Hello, and welcome to LQ.

Firstly, when posting code, please enclose it in code tags:
Code:
[ code ] like this, but without spaces [ /code ]
Secondly, I've tested your program, with "// something got done in here" replaced by "while (true) sleep(1);". I then went on to do "while true; do killall -USR1 a.out; done", and it happily printed out a message ad infinitum.

That leads me to believe that the error is a sympton of whatever it is you have replaced by "//something got done in here". Please post the code here, or--if it's more than, say, 20 lines--in a pastebin or on your website, so we can have a look at it.
 
Old 06-05-2006, 05:33 PM   #3
oceanfun
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jonaskoelker
Hello, and welcome to LQ.

Firstly, when posting code, please enclose it in code tags:
Code:
[ code ] like this, but without spaces [ /code ]
Secondly, I've tested your program, with "// something got done in here" replaced by "while (true) sleep(1);". I then went on to do "while true; do killall -USR1 a.out; done", and it happily printed out a message ad infinitum.

That leads me to believe that the error is a sympton of whatever it is you have replaced by "//something got done in here". Please post the code here, or--if it's more than, say, 20 lines--in a pastebin or on your website, so we can have a look at it.

Thanks for your respond. So your replacement would be like this:

Code:
while(true) {
while(true)
  sleep(1);
}
Due to the complication of the project policies, I can't post the whole code. But it would do something like this:

Code:
while(true) {
  int iResult = select(maxFd+1, readFd, NULL, NULL, NULL); // where maxFd and readFd are ClassA variables
  if (iResult == -1) {
     printf("Error at select statement.\n");
     exit(1);
  }
  if (FD_ISSET(fd1, &readFd)) {
     // do something
  }
  if (FD_ISSET(fd2, &readFd)) {
     // do other thing
  }
}
Anycomment would be appreciated.
OceanFun

Last edited by oceanfun; 06-05-2006 at 07:01 PM.
 
Old 06-05-2006, 07:30 PM   #4
oceanfun
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Additional information:

It printed:

Got SIGUSR1 handled.
Error at select statement.

then the program exited. I am wondering it is because of the select statement returned with -1. What is going on here? Thanks.
 
Old 06-05-2006, 08:25 PM   #5
oceanfun
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Never mind. I got it resolved. Just in case someone has the same problem. Here is the solution. Since the select statement got the errno = 4 (EINTR), I need to handle that error case. Instead of exiting I have to reset the fd set and continue to loop back to call the select statement.

Code:
while(true) {
  setFds(); // function to set fds
  int iResult = select(maxFd+1, readFd, NULL, NULL, NULL); // where maxFd and readFd are ClassA variables
  if (iResult == -1) {
     printf("Error at select statement.\n");
     if (errno != EINTR) {
        exit(1);
     }
     continue;
  }
  if (FD_ISSET(fd1, &readFd)) {
     // do something
  }
  if (FD_ISSET(fd2, &readFd)) {
     // do other thing
  }
}
Regards,
OceanFun.
 
  


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
How to use SIGUSR1 and SIGUSR2 YaK. Programming 3 12-11-2005 05:18 PM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04: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 01:15 PM.

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