程序卡在 wait()

program stuck on wait()

我遇到一个进程卡在等待状态的问题。我一直在解决这个问题,这是我的 shell 程序目前唯一的错误。

问题是程序应该在用户输入 "exit" 时退出。但是,如果用户输入无效字符串,程序似乎会卡在 wait() 上。这导致必须键入 exit 两次才能退出,而不是一次。如何阻止这种情况发生,当用户输入一个哑字符串时如何退出 wait() 调用?

重现步骤:


#include <iostream>
#include <unistd.h>
#include "stdlib.h"
#include "stdio.h"
#include <iostream>
#include <string>
#include <sys/wait.h>
#include <sstream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>

using std::string;

using std::cout;
using std::endl;


bool exitstatus;
int argsIndex = 0;
pid_t pid;
int main(void)
{  

    char * args[100];
    string check = "";           

    while(exitstatus==false)
    {
        cout<<"tinyshell:~>";
        std::getline(std::cin, check);
        if(check == "exit"){
            exitstatus==true;

        }

        if(exitstatus==false&&check!="cd..")
        {
            pid = fork();
            perror("");
            if (pid < 0) { /* error occurred */
                fprintf(stderr, "Fork Failed");
                //return 1;
            }
            else if (pid == 0 ) { /* child process */
                execvp(args[0],args);
                perror("");
            }
            else if(check!= "&"){/* parent will wait for the child to complete */
                wait(NULL);
                perror("");
                //  cout <<"Child Complete" << endl;
            }
            else
            {
            }
        }
    }
    return 0;
};

这行可能需要做一些事情:

exitstatus==true;

你的意思是:

existatus = true;

gcc 无论如何都会报告类似的内容 (-Wall):

warning: statement has no effect [-Wunused-value]
         exitstatus==true;

这是一个很好的例子,说明了为什么启用警告是一种很好的做法...

您的代码还有一个更微妙的问题。您没有检查 execvp 函数的结果。所以基本上,如果你在 shell 中输入一些垃圾命令,你的 exec 将失败,但你的子进程将继续 运行 与父进程(循环)相同的代码。

只需在 execvp() 调用后添加一个 exit(EXIT_FAILURE);