Professional Documents
Culture Documents
#include <string>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
int main() {
signal(SIGINT, signal_handler);
signal(SIGTSTP, signal_handler);
while (true) {
std::cout << "shell> ";
std::string command;
std::getline(std::cin, command);
if (command == "exit") {
break;
}
if (pid == -1) {
std::cerr << "Failed to create child process." << std::endl;
} else if (pid == 0) {
// child process
execlp(command.c_str(), command.c_str(), NULL);
std::cerr << "Failed to execute command: " << command << std::endl;
exit(1);
} else {
// parent process
int status;
waitpid(pid, &status, 0);
}
}
return 0;
}