Sigaction and porting Linux code to Windows

前端 未结 2 2186
感情败类
感情败类 2021-02-14 22:11

I am trying to port caffe (developed for Linux) source code to Windows environment. The problem is at sigaction structure at signal_handler.cpp and

相关标签:
2条回答
  • 2021-02-14 23:10

    Based on @nneonneo:

    void handle_signal(int signal) {
        switch (signal) {
    #ifdef _WIN32
        case SIGTERM:
        case SIGABRT:
        case SIGBREAK:
    #else
        case SIGHUP:
    #endif
          got_sighup = true;
          break;
        case SIGINT:
          got_sigint = true;
          break;
        }
      }
    void HookupHandler() {
        if (already_hooked_up) {
          LOG(FATAL) << "Tried to hookup signal handlers more than once.";
        }
        already_hooked_up = true;
    #ifdef _WIN32
        signal(SIGINT, handle_signal);
        signal(SIGTERM, handle_signal);
        signal(SIGABRT, handle_signal);
    #else
        struct sigaction sa;
        // Setup the handler
        sa.sa_handler = &handle_signal;
        // Restart the system call, if at all possible
        sa.sa_flags = SA_RESTART;
        // Block every signal during the handler
        sigfillset(&sa.sa_mask);
        // Intercept SIGHUP and SIGINT
        if (sigaction(SIGHUP, &sa, NULL) == -1) {
          LOG(FATAL) << "Cannot install SIGHUP handler.";
        }
        if (sigaction(SIGINT, &sa, NULL) == -1) {
          LOG(FATAL) << "Cannot install SIGINT handler.";
        }
    #endif
      }
     void UnhookHandler() {
        if (already_hooked_up) {
    #ifdef _WIN32
            signal(SIGINT, SIG_DFL);
            signal(SIGTERM, SIG_DFL);
            signal(SIGABRT, SIG_DFL);
    #else
          struct sigaction sa;
          // Setup the sighub handler
          sa.sa_handler = SIG_DFL;
          // Restart the system call, if at all possible
          sa.sa_flags = SA_RESTART;
          // Block every signal during the handler
          sigfillset(&sa.sa_mask);
          // Intercept SIGHUP and SIGINT
          if (sigaction(SIGHUP, &sa, NULL) == -1) {
            LOG(FATAL) << "Cannot uninstall SIGHUP handler.";
          }
          if (sigaction(SIGINT, &sa, NULL) == -1) {
            LOG(FATAL) << "Cannot uninstall SIGINT handler.";
          }
    #endif
    
          already_hooked_up = false;
        }
      }
    
    0 讨论(0)
  • 2021-02-14 23:12

    sigaction is part of the UNIX signals API. Windows provides only signal, which doesn't support SIGHUP or any flags (such as SA_RESTART). However, the very basic support is still there, so the code should still work reasonably correctly if you use just signal (and not sigaction).

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