2016-10-14 48 views
-1

我有一個在system()調用中始終返回-1的進程,但是同一系統上的其他進程沒有此錯誤。我無法理解爲什麼這個過程總是在system()調用中返回-1。調用system()的命令也是成功的。只是它總是返回-1。system()調用總是失敗-1 -1

回答

0

該問題是由於該過程的信號(SIGCHLD,SIG_IGN)。 當SIGCHLD被忽略時,waipid不應該在fork之後調用。 但它看起來像系統調用總是調用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$