views:

2273

answers:

5
+1  Q: 

Using SIGINT

According to this http://www.cplusplus.com/reference/clibrary/csignal/signal.html

SIGINT is generally used/cause by the user. How do i cause a SIGINT in c++? i seen an example using kill(pid, SIGINT); but i rather cause it another way. Also i am using windows.

+1  A: 

What other way are you thinking of? The kill() function is the only way the kernel offers to programmatically send a signal.

Actually, you mentioned you were using Windows. I'm not even sure what kill() does on Windows, since Windows doesn't have the same signal architecture that Unix-derived systems do. Win32 does offer the TerminateProcess function, which may do what you want. There is also the GenerateConsoleCtrlEvent function, which applies to console programs and simulates a Ctrl+C or Ctrl+Break.

Greg Hewgill
A: 

"Signals" in this regard are a Unix/POSIX concept. Windows has no direct equivalent.

unwind
+2  A: 

You cause a SIGINT by pressing Ctrl+C.

Example code:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param)
{
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

When run:

$ ./a.out 
^CUser pressed Ctrl+C
$

(Note that this is pure C code, should work in C++ though)

Edit: The only way I know of to send SIGINT apart from interactively pressing Ctrl+C is using kill(pid, SIGINT) as you said...

Jon
Minor gripe, you should not be calling either "printf" or "exit" in the body of the signal handler: http://stackoverflow.com/questions/103280/portable-way-to-catch-signals-and-report-problem-to-the-user
Richard Corden
+4  A: 

C89 and C99 define raise() in signal.h:

#include <signal.h>

int raise(int sig);

This function sends a signal to the calling process, and is equivalent to

kill(getpid(), sig);

If the platform supports threads, then the call is equivalent to

pthread_kill(pthread_self(), sig);

The return value is 0 on success, nonzero otherwise.

João da Silva
A: 

I assume this is a Win32 app...

For a "controlled" or "safe" exit, if the app uses a message loop you can use the PostQuitMessage API from inside of it, or PostMessage outside of it. Otherwise you will need to get the thread/process ID and use the TerminateThread or TerminateProcess API, depending on if you want to kill just a thread or the entire process and all threads it has spawned. It is explained nicely by Microsoft (as with all API calls) on MSDN:

http://msdn.microsoft.com/en-us/library/aa450927.aspx

jheriko