奇怪的waitpid行为

时间:2016-12-06 19:24:12

标签: linux fork exec waitpid

假设我有以下代码。

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;
}

有关为何发生这种情况的任何想法?

0 个答案:

没有答案