【问题标题】:system() call always fail with -1system() 调用总是以 -1 失败
【发布时间】:2016-10-14 06:04:38
【问题描述】:

我有一个进程在调用system() 时总是返回-1,但同一系统上的其他进程没有此错误。我不知道为什么这个过程在system() 调用上总是返回-1。调用system() 的命令也成功。只是它总是返回-1。

【问题讨论】:

    标签: system


    【解决方案1】:

    问题是由于该进程的信号(SIGCHLD,SIG_IGN)。 当忽略 SIGCHLD 时,不应在分叉后调用 waipid。 但是看起来系统调用总是调用waitpid,这会导致系统返回-1。

    #include  <signal.h>
    #include  <errno.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
        int ret = 0;
        signal(SIGCHLD, SIG_IGN);
    
        ret =  system("echo hello!!");
        printf("ret=%d errno=%d error=%s\n",ret,errno,strerror(errno));
    
    }
    

    bash-3.2$ ./a.out 
    hello!!
    ret=-1 errno=10 error=No child processes
    bash-3.2$ 
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多