c - 多次使用sigaction时出错

标签 c linux signals fork sigaction

每次我的子进程死亡时,我都试图调用一个函数。这是通过捕获 execve() 发送的 SIGCHLD 来完成的。 我的问题是我的处理程序函数并不总是被第二次调用。有时是这样,所以我有点困惑。

我的代码是这样的:

  #include <stdio.h>
  #include <stdlib.h>
  #include <signal.h> // sigaction()
  #include <time.h>
  #include <errno.h>
  #include <unistd.h>
  #define MAX_LENGTH 1000
  #define BILLION 1E9

  struct timespec start,stop;
  float totalTime = 0;

  void runCommand(int signo)
  {
    printf("Run command called\n");

    float processTime = 0;

    //Get the time when the process ended
    if(clock_gettime(CLOCK_MONOTONIC,&stop) == -1){
      perror(NULL);
    }

    processTime = stop.tv_sec - start.tv_sec + ( stop.tv_nsec - start.tv_nsec ) / BILLION;
    totalTime += processTime;
  }


  int main(int argc, char const *argv[]) {

    int counter = 0;
    int pid = 0;
    int status;
    char *args[4];
    char line[MAX_LENGTH];

    //Setup behaviour for SIGCHLD
    struct sigaction psa;
    psa.sa_handler = runCommand;
    sigaction(SIGCHLD, &psa, NULL);

    FILE *fp = fopen("script_file.txt","r");
    if(!fp)
    {
      return 0;
    }

    //Setup argsgcc
    args[0] = "/bin/sh";
    args[1] = "-c";
    args[3] = NULL;

    while(fgets(line, MAX_LENGTH, fp))
    {
      printf("The command number %d is %s \n", counter, line);

      //Get the time when the process started
      if(clock_gettime(CLOCK_MONOTONIC,&start) == -1){
        perror(NULL);
      }

      //Create a new process
      pid = fork();

      //Child process
      if(pid == 0)
      {
        args[2] = line;
        execve("/bin/sh", args, NULL);
      }
      //Parent process
      else
      {
        wait(&status);
      }

      counter++;
    }

    printf("The overall time was %f seconds.\n", totalTime);

    fclose(fp);


    return 0;
  }

当我尝试运行它时,有时我的结果是:

The command number 0 is mkdir test/

Run command called
The command number 1 is sleep 0.5

Run command called
The command number 2 is sleep 1

Run command called
The command number 3 is rmdir test/

Run command called
The overall time was 1.544909 seconds.

这似乎是正确的。 但在其他情况下,我的结果是:

The command number 0 is mkdir test/

Run command called
The command number 1 is sleep 0.5

The command number 2 is sleep 1

The command number 3 is rmdir test/

The overall time was 0.009810 seconds.

这让我相信 sigaction 并不总是收到我的 SIGCHLD 信号。 当我运行 valgrind 时,出现以下错误:

    ==5407== All heap blocks were freed -- no leaks are possible
    ==5407== 
    ==5407== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
    ==5407== 
    ==5407== 1 errors in context 1 of 2:
    ==5407== Syscall param rt_sigaction(act->sa_flags) points to uninitialised byte(s)
    ==5407==    at 0x4E6F5AE: __libc_sigaction (sigaction.c:62)
    ==5407==    by 0x40098B: main (iv.c:40)
    ==5407==  Address 0xffefff498 is on thread 1's stack
    ==5407==  Uninitialised value was created by a stack allocation
    ==5407==    at 0x400931: main (iv.c:29)
    ==5407== 
    ==5407== 
    ==5407== 1 errors in context 2 of 2:
    ==5407== Syscall param rt_sigaction(act->sa_mask) points to uninitialised byte(s)
    ==5407==    at 0x4E6F5AE: __libc_sigaction (sigaction.c:62)
    ==5407==    by 0x40098B: main (iv.c:40)
    ==5407==  Address 0xffefff4a8 is on thread 1's stack
    ==5407==  Uninitialised value was created by a stack allocation
    ==5407==    at 0x400931: main (iv.c:29)
    ==5407== 
    ==5407== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

但我不确定这是什么意思。

最佳答案

初始化 psa 的最简单方法是将其初始化为零:

struct sigaction psa = {0};

然而,也有微妙之处。您不能从信号处理程序调用异步信号不安全的函数,例如 printf。参见 signal safty对于允许的功能列表。您可以将 printf 替换为 write(2) 调用来避免这种情况:

write(STDOUT_FILENO, "Run command called\n", sizeof "Run command called\n" - 1);

此外,您还在每个子进程上执行 wait,然后再创建另一个子进程。因此,任何时候总是只有一个 child 在跑。 如果您修改您的程序以允许多个子进程同时运行并且这些子进程同时死掉,那么并不是所有的SIGCHLD 信号都可以被捕获。例如,如果信号处理程序正在处理前一个 SIGCHLD 时 SIGCHLD 到达,那么它将丢失。因为挂起信号将被丢弃,除非它们是不同信号,实时信号除外。例如,如果 SIGUSR1SIGCHLD 正在处理(由信号处理程序)时到达,它将被阻止;但另一个 SIGCHLD 将被忽略。

关于c - 多次使用sigaction时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49300920/

相关文章:

sql-server - 无法通过 Linux 上的 JDBC 连接到 SQL Server

c - Linux 上的服务器用 C 语言制作 - 双端口具有不同的行为

linux - 如何知道我使用的是哪个 Linux 发行版?

c++ - Qt 信号和插槽 - 没有任何反应

objective-c - cgo godefs 和 Objective-C

c - 检测到堆栈粉碎且没有 getenv 的来源

c - 家长和 child 通过信号同步的问题

c++ - 是否可以在进程中的所有线程上设置 pthread_sigmask?

c - 二维数组索引 - 未定义的行为?

c - Xilinx ARM 编译器不会提示 void 指针算术,并且会生成正确的结果。为什么?