Using SIGINT

前端 未结 6 1911
误落风尘
误落风尘 2020-12-18 15:24

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 SIG

相关标签:
6条回答
  • 2020-12-18 15:42

    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...

    0 讨论(0)
  • 2020-12-18 15:53

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

    0 讨论(0)
  • 2020-12-18 15:55
    void SendSIGINT( HANDLE hProcess )
    {
        DWORD pid = GetProcessId(hProcess);
        FreeConsole();
        if (AttachConsole(pid))
        {
            // Disable Ctrl-C handling for our program
            SetConsoleCtrlHandler(NULL, true);
    
            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // SIGINT
    
            //Re-enable Ctrl-C handling or any subsequently started
            //programs will inherit the disabled state.
            SetConsoleCtrlHandler(NULL, false);
    
            WaitForSingleObject(hProcess, 10000);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 16:00

    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

    0 讨论(0)
  • 2020-12-18 16:01

    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.

    0 讨论(0)
  • 2020-12-18 16:01

    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.

    0 讨论(0)
提交回复
热议问题