假设我有以下代码。
int main(int argc, char* argv[])
{
char* program[3] = {"/bin/ls", NULL, NULL};
pid_t pid = fork();
if (pid == 0) {
char* envp[1] = {NULL};
execve(program[0], program, envp);
}
int status;
printf("Going to wait for process with pid: %d\n", pid);
waitpid(pid, &status, 0);
return 0;
}
如果我执行它,它就会终止。
但是,如果我做了" ls -l" (只是更改程序数组)而不是" ls"它会永远停止。所以这个程序
int main(int argc, char* argv[])
{
char* program[3] = {"/bin/ls", "-l", NULL};
pid_t pid = fork();
if (pid == 0) {
char* envp[1] = {NULL};
execve(program[0], program, envp);
}
int status;
printf("Going to wait for process with pid: %d\n", pid);
waitpid(pid, &status, 0);
return 0;
}
有关为何发生这种情况的任何想法?