声明char数组会导致execv()无效

我编写了以下代码,以便在c unix中使用管道:

#include  #include  #include  #include  #include  int main () { int fds[2]; pid_t pid; /* Create a pipe. File descriptors for the two ends of the pipe are placed in fds. */ pipe (fds); /* Fork a child process. */ pid = fork (); if (pid == (pid_t) 0) { //char abc[10]; - **Uncommenting this cause the program not to work.** /* This is the child process. Close our copy of the write end of the file descriptor. */ close (fds[1]); // Read params FILE * stream; stream = fdopen (fds[0], "r"); char* args[4]={"avg3.out","4","3","5"}; /* Replace the child process with the “avg3” program. */ execv("avg3.out", args); } else { /* This is the parent process. */ FILE* stream; /* Close our copy of the read end of the file descriptor. */ close (fds[0]); /* Convert the write file descriptor to a FILE object, and write to it. */ dup2(fds[0], STDOUT_FILENO); stream = fdopen (fds[1], "w"); fprintf (stream, "5 4 3"); fflush (stream); close (fds[1]); /* Wait for the child process to finish. */ waitpid (pid, NULL, 0); } return 0; } 

avg3.out是我之前编译的文件。 它只是计算发送给它的3个参数的平均值。

输出是4,但是当我试图从流中读取时,我为char buffer[10]添加了一个声明char buffer[10]代码停止工作。 也就是说,没有提供输出。 我试图重命名它,将decleration移动到if语句的开头。 但没有任何效果。

那么,为什么程序在添加数组声明时会停止工作?

调用exec*()的参数数组需要(char*)NULL -terminated。

这条线

 char* args[4]={"avg3.out","4","3","5"}; 

应该

 char* args[] = {"avg3.out", "4", "3", "5", NULL}; 

因为它不在您的代码中, exec()可能会丢失搜索它。

运气不好,堆栈可能已经干净( 0填充)代码版本没有声明aexecv()在指针指向"5"后发现NULL 。 在堆栈上创建然后更改堆栈的内容,这使得execv()丢失搜索NULL


此外,值得一提的是,OP的代码错过了对大多数相关系统调用的错误检查。

通过使用对perror()调用,这样做以及对错误原因的详细描述可能导致通过提供相关信息来解决此问题。

特别是在调用execv()之后放置它很快就会明白出错了:

  execv("avg3.out", args); perror("execv() failed");