2

我正在使用 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 */
        }
    }
}
4

0 回答 0