我正在使用C shell,当我运行/bin/ls
时,它会正确显示它,但会挂起来。我所拥有的相关代码如下所示,我觉得在父案例中缺少wait()语句会不得不这样做?
for(p = j->first_process; p; p = p->next) {
if (!strcmp(p->argv[0], "exit" )) exit(0);
switch (pid = fork()) {
case -1: /* fork failure */
perror("fork");
exit(EXIT_FAILURE);
case 0: /* child */
/* establish a new process group, and put the child in
* foreground if requested
*/
if (j->pgid < 0) /* init sets -ve to a new process */
j->pgid = getpid();
p->pid = 0;
if (!setpgid(0,j->pgid))
if(fg) // If success and fg is set
tcsetpgrp(shell_terminal, j->pgid); // assign the terminal
/* Set the handling for job control signals back to the default. */
signal(SIGTTOU, SIG_DFL);
/* execute the command through exec_ call */
execv(p->argv[0], p->argv);
exit(0);
default: /* parent */
/* establish child process group here to avoid race
* conditions. */
p->pid = pid;
if (j->pgid <= 0)
j->pgid = pid;
setpgid(pid, j->pgid);
}
/* Reset file IOs if necessary */
if(fg){
/* Wait for the job to complete */
}
else {
/* Background job */
}
}
}