把猫放在后台​​冻结我的壳

rou*_*ter 0 linux shell

简介 UX作为我课程工作的一部分,我在linux中编写自己的shell.我在后台放置一些进程时遇到问题.我知道在命令末尾放置一个'&'会使进程保持在后台,而父进程(myShell)不必等待它.它与ls -l&,firefox等工作正常,

问题

我真正担心的问题是cat&.在这里,当我运行这个命令时,cat进程返回后台,然后我回到myShell(父进程)提示,虽然我可以键入但是myshell在几秒钟后就会被冻结.

这与阻塞输入有什么关系,有什么建议吗?谢谢.

编辑:这是我的代码中的一个函数,它执行@minitech提出的shell命令

void executeCmd(char **cmdArgs, int inRedirectFd, int outRedirectFd, char *inFileName, char *outFileName, int bgProc, int inPipe, int outPipe, int *pipeFd1, int *pipeFd2){
int childPid;
childPid = fork();

if(childPid==0){

    //writing the pipes before the redirection because the redirection can overwrite pipes
    if(!inPipe && outPipe){
        close(pipeFd1[0]);
        dup2(pipeFd1[1], STDOUT_FILENO);
    }
    else if(inPipe && !outPipe){
        close(pipeFd1[1]);
        dup2(pipeFd1[0], STDIN_FILENO);
        }
    else if(inPipe && outPipe){
        close(pipeFd1[1]);
        close(pipeFd2[0]);
        dup2(pipeFd1[0], STDIN_FILENO);
        dup2(pipeFd2[1], STDOUT_FILENO);
    }

    if(outRedirectFd==1){
        //token = strtok(NULL, " ");
        int fd = open(outFileName, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
        if(fd==-1){
            printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
        }
        dup2(fd, STDOUT_FILENO);
    }
    if(inRedirectFd==0){
        //token = strtok(NULL," ");
        int fd = open(inFileName, O_RDONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
        if(fd == -1){
            printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
        }
        dup2(fd, STDIN_FILENO);
    }

    execvp(cmdArgs[0], cmdArgs);
    printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
    _Exit(EXIT_FAILURE);
}
else{
    if(!bgProc){
        int retStatus;
        waitpid(childPid, &retStatus, 0);
        //waitpid(childPid, &retStatus, 0);
        //printf("%d\n", retStatus);
    }
    else{
        //printf("parentDesn't Wait");
    }   
}
Run Code Online (Sandbox Code Playgroud)

}

抛出你能想到的任何其他建议.谢谢.

R S*_*ahu 5

cat没有文件参数的情况下调用时,它会处理stdin.当你使用时cat &,它进入后台并等待来自的输入stdin.由于你没有提供信号结束的方式stdin,它等待永远.

如果你提供一个文件cat,就像cat test.txt &它一样,它不会冻结你的shell.