fork()
및 exec()
을 사용하여 C++로 쉘을 구현하려고합니다. 다음과 같이 내 코드는 :이 코드를 실행 한 명령을 실행하려고하면C++에서 execv 후에 쉘이 계속 입력을 요구하는 방법
#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;
}
, 작동하는 것 같다 제대로
Elliots - 맥북 - 프로 : minish 엘리엇 $ ./minish
minish> LS
minish> 메이크 minish의 minish.cpp 시험
execv를 통해 ls를 실행 한 후 코드는 "minish>"를 인쇄하지 않고 더 많은 입력을 요구하지만 명령을 계속 입력하면 계속 실행할 수 있습니다 (예 : "ls").
이 문제의 원인은 무엇이며 어떻게 해결할 수 있습니까?
오케이, pid> 0인지 확인하면 코드가 제대로 작동합니다. 고맙습니다! – ecatalano