Search code examples
forkposixchild-processmultiprocesswaitpid

How to make shell continually ask for input after execv in C++


I'm attempting to implement a shell in c++, using fork() and exec(). My code is as follows:

#include <iostream>
#include <string>
#include <unistd.h>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char** argv){
    while(true){
        cout << "minish> ";
        string input = "";
        getline(cin, input);

        int count = 1;
        int shouldCount = 1;
        if(input.length() == 0) shouldCount = 0;

        if(shouldCount == 1){
            int didFindLetter = 0;
            for(int i = 1; i < input.length()-1;i++){
                if(input[i] ==' ' && didFindLetter == 1){
                    count++;
                    didFindLetter = 0;
                }
                else if(input[i]!=' '){
                    didFindLetter = 1;
                }
            }
        }
    
        //need to create a wordArray here...
        vector<string> wordArray;
        std::istringstream iss (input);
        string tempWord;
        for(int i = 0; i < count; i++){
            iss >> tempWord;
            wordArray.push_back(tempWord);
        }
        
        char* argsArray[1024];
        count = 0;
        for (int i = 0; i < wordArray.size(); i++) {
            //strdup returns pointer to a char array copy of its parameter
            argsArray[count++] = strdup(wordArray[i].c_str());
        }
        argsArray[count++] = (char *)NULL;


        pid_t pid = fork();

        if(pid == 0){
            //child
            execvp(argsArray[0], argsArray);
            fprintf(stderr, "exec: %s\n", strerror(errno));
            exit(errno);

        }
        else if(pid == 1){
            //int waitStatus;
            //pid_t terminated_child_pid = wait(&waitStatus);
            wait(NULL);
        }
    }

    return 0;
}

When I run this code and try to execute a single command, it seems to work properly

Elliots-MacBook-Pro:minish Elliot$ ./minish

minish> ls

minish> makefile minish minish.cpp test

After running ls via execv, the code does not print "minish> " to prompt me for more input, however, is able to continually execute if i continue to type commands i.e "ls".

What is the cause of this problem, and how might i be able to fix it?


Solution

  • I'm not quite sure I follow your example, but you have the semantics of fork() wrong:

    RETURN VALUE
           On success, the PID of the child process is returned 
           in the parent, and 0 is returned in the child.  On
           failure, -1 is returned in the  parent, no child 
           process is created, and errno is set appropriately.
    

    Your code has the parent checking for a return value of 1 - meaning it skips waiting for the child; it immediately continues with another iteration.