Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';行政会议';导致我的程序崩溃-C?_C_Shell_Process_Fork - Fatal编程技术网

为什么';行政会议';导致我的程序崩溃-C?

为什么';行政会议';导致我的程序崩溃-C?,c,shell,process,fork,C,Shell,Process,Fork,在我完成shell之前的最后一件事是,有人能解释一下为什么当我运行emacs&时,它会导致我的shell程序崩溃,并由于某种原因错误读取输入。下面是我在后台运行新进程的代码,请注意,emacs&中的&已从命令中删除,并告诉我的shell在后台运行该命令。非常感谢您提供的任何帮助或建议 int execBgCmd(char **cmds, Command cmd) { pid_t pid1, pid2; int status; if (pid1 = fork()) { wa

在我完成shell之前的最后一件事是,有人能解释一下为什么当我运行
emacs&
时,它会导致我的shell程序崩溃,并由于某种原因错误读取输入。下面是我在后台运行新进程的代码,请注意,
emacs&
中的
&
已从命令中删除,并告诉我的shell在后台运行该命令。非常感谢您提供的任何帮助或建议

int execBgCmd(char **cmds, Command cmd) {

  pid_t pid1, pid2;
  int status;

  if (pid1 = fork()) {
    waitpid(pid1, &status, 1);
  } else if (!pid1) {
    if (pid2 = fork()) {
      exit(0);
    } else if (!pid2) {
      if (cmd.rstdin != NULL) {
        int fd0 = open(cmd.rstdin, O_RDONLY);
        dup2(fd0, STDIN_FILENO);
        close(fd0);
      }
      if (cmd.rstdout != NULL) {
        int fd1 = open(cmd.rstdout , O_CREAT | O_WRONLY | O_TRUNC, 0644) ;
        dup2(fd1, STDOUT_FILENO);
        close(fd1);
      }
      if (execvp(cmds[0], cmds) < 0) { 
        printf("\nCould not execute command, try again.\n"); 
      }

    } else {
      fprintf(stderr, "Fork Failure");
    }
  } else {
    fprintf(stderr, "Fork Failure");
  }
  wait(NULL);
}
int execBgCmd(字符**cmds,命令cmd){
pid_t pid1,pid2;
智力状态;
if(pid1=fork()){
waitpid(pid1和status,1);
}如果(!pid1){
if(pid2=fork()){
出口(0);
}如果(!pid2){
如果(cmd.rstdin!=NULL){
int fd0=打开(仅限cmd.rstdin,Ordu);
dup2(fd0,标准文件号);
关闭(fd0);
}
如果(cmd.rstdout!=NULL){
int fd1=打开(cmd.rstdout,O|u CREAT | O|u WRONLY | O|u TRUNC,0644);
dup2(fd1,标准文件号);
关闭(fd1);
}
如果(execvp(cmds[0],cmds)<0){
printf(“\n无法执行命令,请重试。\n”);
}
}否则{
fprintf(标准“叉故障”);
}
}否则{
fprintf(标准“叉故障”);
}
等待(空);
}

waitpid中的
1
应该替换为
WNOHANG
——它更便于携带和理解。
if(pid=/*…*/)else if(!pid)/*…*/else/*/
有一个多余的第二个
if
,第三个分支永远不会被执行。请将编译器的警告级别提高到最大,并清除所有错误和警告。另外,请提供一个。您的父程序是被终止还是挂起,
崩溃
不是很有帮助吗