标签: sigaction

Sigating并将Linux代码移植到Windows

我正在尝试将caffe(为Linux开发)源代码移植到Windows环境中.问题出在sigaction结构处signal_handler.cppsignal_handler.h.源代码如下所示.我的查询是可以使用哪个库或代码替换来使其sigaction在Windows中正常工作.

///头文件

#ifndef INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_
#define INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_

#include "caffe/proto/caffe.pb.h"
#include "caffe/solver.hpp"

namespace caffe {

class SignalHandler {
 public:
  // Contructor. Specify what action to take when a signal is received.
  SignalHandler(SolverAction::Enum SIGINT_action,
                SolverAction::Enum SIGHUP_action);
  ~SignalHandler();
  ActionCallback GetActionFunction();
 private:
  SolverAction::Enum CheckForSignals() const;
  SolverAction::Enum SIGINT_action_;
  SolverAction::Enum SIGHUP_action_;
};

}  // namespace caffe

#endif  // INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_
Run Code Online (Sandbox Code Playgroud)

///源文件

    #include <boost/bind.hpp>
    #include <glog/logging.h>

    #include <signal.h>
    #include <csignal>

    #include "caffe/util/signal_handler.h"

    namespace {
      static volatile sig_atomic_t got_sigint = …
Run Code Online (Sandbox Code Playgroud)

c++ linux posix sigaction caffe

8
推荐指数
2
解决办法
7303
查看次数

为什么alarm()导致fgets()停止等待?

我正在玩C中的信号.我的主要功能基本上要求使用一些输入fgets(name, 30, stdin),然后坐在那里等待.我设置了一个闹钟alarm(3),我重新分配SIGALRM来调用一个myalarm简单调用的函数system("say PAY ATTENTION").但是在闹钟响起后,fgets()停止等待输入,我的主fn继续.即使我改为myalarm设置一些变量并且不做任何操作,也会发生这种情况.

void myalarm(int sig) {
    //system("say PAY ATTENTION");
    int x = 0;
}

int catch_signal(int sig, void (*handler)(int)) { // when a signal comes in, "catch" it and "handle it" in the way you want
    struct sigaction action;  // create a new sigaction
    action.sa_handler = handler;  // set it's sa_handler attribute to the function specified in the header
    sigemptyset(&action.sa_mask);  // "turn all the signals …
Run Code Online (Sandbox Code Playgroud)

c fgets alarm sigaction

8
推荐指数
1
解决办法
410
查看次数

如何从 C++ 调用 sigaction

我知道如何在 C 中使用它(使用 signal.h),但该<csignal>库是用 C++ 提供的,我想知道它是否包含 sigaction?我尝试运行它,但它说未找到。我想知道我是否做错了什么?

#include <iostream>
#include <string>
#include <cstdio>
#include <csignal>

namespace {
  volatile bool quitok = false;
  void handle_break(int a) {
    if (a == SIGINT) quitok = true;
  }
  std::sigaction sigbreak;
  sigbreak.sa_handler = &handle_break;
  sigbreak.sa_mask = 0;
  sigbreak.sa_flags = 0;
  if (std::sigaction(SIGINT, &sigbreak, NULL) != 0) std::perror("sigaction");
}

int main () {
  std::string line = "";
  while (!::quitok) {
    std::getline(std::cin, line);
    std::cout << line << std::endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

但由于某种原因它不起作用。编辑:“不起作用”,我的意思是编译器失败并表示没有 std::sigaction 函数或结构。

sigaction 是 …

c++ signals sigaction

5
推荐指数
1
解决办法
1万
查看次数

带有`SA_SIGINFO`的macOS`sigaction()`处理程序不包含`si_pid`

我正在尝试编写一个信号处理程序,它需要知道发送信号的进程的pid.从siginfo_t使用Xcode 10的macOS 10.14传递到我的处理程序中获得任何有用的东西,我没有运气.

我将代码缩减到以下最小样本以证明问题.在这个示例中,我生成了一个子进程来发送我想要测试的信号,该信号是默认的SIGTERM,但没有其他信号我尝试过更好.

假设你想在mac上构建和测试它,你可能想告诉lldb在接收信号时不要停止.你可以使用这个lldb命令:pro hand -p true -s false SIGTERM.

我也在编译C++,但我相信我已经删除了所有这些,示例代码现在应该是纯C.

请注意,如果信号源自子进程,终端或其他进程,则结果始终始终si_pid为0(以及除si_signo和之外的所有内容si_addr)无关紧要.我发送信号的次数无关紧要,因此它似乎不仅仅是竞争条件.

如何获得在macOS 10.14上发送信号的过程的pid?我不记得在10.12这个问题,这是我之前使用的.

这只是解决问题的一个示例,因此请忽略任何实际上不会导致问题的内容.

如果代码看起来应该像我期望的那样工作,那么我将有兴趣看到它所运行的系统的评论.

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

volatile sig_atomic_t histogram[3] = {0,0,0};
volatile sig_atomic_t signaled = 0;
const int testsig = SIGTERM;

void sigaction_handler(int sig, siginfo_t* info, void* context)
{
    switch (info->si_pid) {
        case 0:
        case 1:
            histogram[info->si_pid]++;
            break;

        default:
            histogram[2]++;
            break;
    }
    signaled = 1;
}

int main(int argc, const char …
Run Code Online (Sandbox Code Playgroud)

c macos signals sigaction

5
推荐指数
1
解决办法
253
查看次数

为什么 bash 后台任务会忽略 SIGINT?

我注意到sleep在产生时不能被 SIGINT 杀死:

(sleep 1000 &)
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会这样。
以下所有内容都被 SIGINT 杀死:

sleep 1000
sleep 1000 &
(sleep 1000)
( ( (sleep 1000) ) )
( ( (sleep 1000)& ) )
Run Code Online (Sandbox Code Playgroud)

所以我认为它必须与非交互式bash有关(进入子shell需要括号)并且任务必须在后台运行。

我写了一个简短的 C 程序来测试行为,发现 sa_handler 设置为 SIG_IGN —— 解释了现象,但为什么会这样呢?

我还没有找到任何关于它是否是预期功能的信息(尽管考虑到手册的长度,我可能只是错过了它),如果是,隐藏它的原因是什么。

我为那些感兴趣的人提供了 C 代码:

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

#include <signal.h>

int main() {
    struct sigaction oldact;
    if(sigaction(SIGINT, NULL, &oldact) != 0) {
        printf("Error in sigaction\n");
        exit(1);
    }

    if(oldact.sa_flags & SA_SIGINFO) {
        printf("Using sa_sigaction\n");
    } else {
        if(oldact.sa_handler == SIG_DFL) { …
Run Code Online (Sandbox Code Playgroud)

linux bash signals sigaction

5
推荐指数
1
解决办法
75
查看次数

sigaction既可以是结构又可以是函数吗?

我注意到它sigaction被定义为结构和函数(http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html):

    int sigaction(int, const struct sigaction *restrict,
       struct sigaction *restrict);
Run Code Online (Sandbox Code Playgroud)

使用它的一个例子是:

    struct sigaction sa;

    /* Set up handler */
    sa.sa_flags = SA_SIGINFO|SA_RESTART;
    sa.sa_sigaction = timer_expiry;

    /* Setup signal watchdog */
    if (sigaction(SIG_WDOG, &sa, NULL) == -1) {
       printf("ERROR: Failed to set wdog signal with %s",
           strerror(errno));
    }
Run Code Online (Sandbox Code Playgroud)

c unix linux sigaction

4
推荐指数
1
解决办法
582
查看次数

OS X sigaction错误地设置了sa_mask

在macbook(OSX 10.9.5(13F34))上有以下简单程序:

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

static void nop(int unused) { }

int
main(void) {
    struct sigaction sa, osa;
    sigset_t mask;

    sigemptyset(&sa.sa_mask);
    printf("Errno after sigempty sa_mask: %d\n", errno);
    sigemptyset(&osa.sa_mask);
    printf("Errno after sigempty oldsa_mask: %d\n", errno);
    sa.sa_flags = 0;
    sa.sa_handler = nop;

    sigprocmask(0, NULL, &mask);
    printf("Errno after sigprocmask mask: %d\n", errno);
    printf("%d\n", sigismember(&mask, SIGALRM));

    sigaction(SIGALRM, &sa, &osa);
    printf("Errno after sigaction sa osa: %d\n", errno);
    printf("%d\n", sigismember(&osa.sa_mask, SIGALRM));
    printf("%d\n", sigismember(&sa.sa_mask, SIGALRM));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

神秘印刷:

Errno after sigempty sa_mask: 0
Errno after …
Run Code Online (Sandbox Code Playgroud)

c macos sigprocmask language-lawyer sigaction

3
推荐指数
1
解决办法
680
查看次数

未在子进程中调用sigaction的信号处理程序

我有一个程序,为安装了一个信号处理程序SIGSEGV。在信号处理程序中(我尝试捕获崩溃),我重新启动了应用程序。

但是,当我的应用程序复活后,它将不再处理SIGSEGV

这是一个例子:

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

const char * app = 0;

void sig_handler(int signo)
{
    puts("sig_handler");

    const pid_t p = fork();

    if (p == 0)
    {
        printf("Running app %s\n", app);
        execl(app, 0);
    }

    exit(1);
}


int main(int argc, char** argv)
{
    app = argv[0];

    struct sigaction act;
    sigemptyset(&act.sa_mask);

    act.sa_handler = sig_handler;
    act.sa_flags = 0;

    const int status = sigaction(SIGSEGV, &act, 0) == 0;     
    printf("signaction = %d\n", status);

    sleep(5);

    int* a …
Run Code Online (Sandbox Code Playgroud)

c linux signals sigaction

3
推荐指数
1
解决办法
1115
查看次数

标签 统计

sigaction ×8

c ×5

linux ×4

signals ×4

c++ ×2

macos ×2

alarm ×1

bash ×1

caffe ×1

fgets ×1

language-lawyer ×1

posix ×1

sigprocmask ×1

unix ×1