Author Topic: signals and exit(0) aren't interpreted correctly  (Read 10102 times)

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
signals and exit(0) aren't interpreted correctly
« on: June 01, 2006, 02:01:48 pm »
If I set up a signal catcher like this:

signal(SIGINT, sigDestroy);

then in the signal catcher I call exit(0), when I press ctrl c, codeblocks still thinks the process terminated with status -282848 (or whatever it actually is). The other thing wierd that happens is if I ignore the signal from within the signal catcher like this:

signal(SIGINT, SIG_IGN);

codeblocks thinks the program terminated, but the program is still running withing console_runner.

Is this a bug?

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: signals and exit(0) aren't interpreted correctly
« Reply #1 on: June 02, 2006, 12:13:50 pm »
Hello,

If you give C::B revision and OS, it would be easier to help you.

Best wishes,
Michael

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #2 on: June 03, 2006, 02:59:04 am »
oh ok,

windows xp, I just downloaded the june 1st nightly build and it still does it.

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #3 on: June 12, 2006, 05:45:40 am »
bump

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: signals and exit(0) aren't interpreted correctly
« Reply #4 on: June 12, 2006, 09:04:56 am »
windows xp, I just downloaded the june 1st nightly build and it still does it.
Yes it is a bug. In your code though, not in Code::Blocks.
You cannot set signal handlers under Windows. This is a no-op. Therefore it still crashes, and hence the exit code.

EDIT:
Strictly speaking, the above is not correct, you can set signal handlers. However, this is a Windows XP feature which is not backwards-compatible, and you have to use Windows API calls for that. POSIX calls don't do anything (unless maybe you have this special Unix compatibility toolkit from Microsoft, I don't know about that).
The MSVC compiler has some builtin functionality that installs structured exception handlers behind the scenes, but this again only works under Windows XP and is non-standard (look on MSDN if you're interested in that).
« Last Edit: June 12, 2006, 09:18:01 am by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #5 on: June 13, 2006, 02:09:17 pm »
That's wierd, because it works fine.
I'm using gcc and haven't installed anything special.

If I ignore the signal in the signal handler and press ctrl c while the program is running, it actually doesn't terminate. It keeps running as though nothing happened. The funny thing though is that codeblocks thinks it stopped. It is clearly still running, the console is open and I can still type stuff in and it keeps running as usual.
It works fine in linux, but if what you say is right then it shouldn't work in windows, but it does.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: signals and exit(0) aren't interpreted correctly
« Reply #6 on: June 13, 2006, 02:53:51 pm »
That's wierd, because it works fine.

Can you post a minimal example?
Be patient!
This bug will be fixed soon...

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: signals and exit(0) aren't interpreted correctly
« Reply #7 on: June 13, 2006, 03:53:02 pm »
That's wierd, because it works fine.
[...]
It works fine in linux, but if what you say is right then it shouldn't work in windows, but it does.
Well, I have been trying to catch SIGFPE, SIGILL, and SIGSEGV before, all of these do nothing, the program crashes as if there was no handler at all.

As far as SIGINT is concerned, MSDN says:
Quote
SIGINT is not supported for any Win32 application, including Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application such as UNIX, to become multithreaded, resulting in unexpected behavior.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #8 on: June 15, 2006, 01:23:41 pm »
Code
#include <iostream>
#include <signal.h>

void sigCatch(int sig) {
   signal(SIGINT, sigCatch);
   std::cout << "caught signal" << std::endl;
}

int main(int argc, char *argv[])
{
   int i = 0;
signal(SIGINT, sigCatch);
while (1) {
   i++;
   if (i == 100000000) {
      std::cout << "You can't close me" << std::endl;
      i = 0;
   }
}
return 0;
}

I need to call signal(SIGINT, sigCatch) each time a SIGINT is thrown because the signal gets reset each time. When you run this it will just keep printing "You can't close me", then when you press ctrl-c it prints "signal caught" and just keeps going. But, codeblocks thinks it has quit after the first ctrl-c.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: signals and exit(0) aren't interpreted correctly
« Reply #9 on: June 15, 2006, 02:03:50 pm »
I need to call signal(SIGINT, sigCatch) each time a SIGINT is thrown because the signal gets reset each time.
That is normal System V  / Linux glibc behaviour. Under BSD and Linux glibc2, the signal is not reset.

However, you were originally asking about Windows, and Windows explicitely does not support SIGINT (see above quote from MSDN).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #10 on: June 16, 2006, 04:20:01 am »
I don't care what msdn says, if you run the code you'll see what happens, it works, it handles the signal.
Maybe the writers of mingw implemented it.
« Last Edit: June 16, 2006, 04:37:27 am by jarro_2783 »

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: signals and exit(0) aren't interpreted correctly
« Reply #11 on: June 16, 2006, 11:07:41 am »
I looked into this and it has to do with how wx handles signals under windows. You can find more about this in wx docs for ::wxKill().

Quote from: wx docs
int wxKill(long pid, int sig = wxSIGTERM, wxKillError *rc = NULL, int flags = 0)

wxSIGNONE, wxSIGKILL and wxSIGTERM have the same meaning under both Unix and Windows but all the other signals are equivalent to wxSIGTERM under Windows.

Not much can be done.
OTOH, running in the debugger works as expected but that's gdb.
Be patient!
This bug will be fixed soon...

Offline jarro_2783

  • Multiple posting newcomer
  • *
  • Posts: 99
    • Project Freedom
Re: signals and exit(0) aren't interpreted correctly
« Reply #12 on: June 17, 2006, 12:25:15 pm »
so does that mean my code is fine, and it's a problem with codeblocks (codeblocks using wx) and it can't be fixed?
It doesn't really matter because it thinks the program terminated when it didn't and gives the wrong return code. But that's fine because my program still works, so I don't really care what codeblocks thinks it did. It was just intriguing me as to why it did it.