You are on page 1of 1

#include <iostream>

#include <string>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

void signal_handler(int signal_num) {


switch(signal_num) {
case SIGINT:
std::cout << "Interrupt signal received. Stopping command execution."
<< std::endl;
break;
case SIGTSTP:
std::cout << "Background signal received. Moving command to
background." << std::endl;
break;
}
}

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;
}

pid_t pid = fork();

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;
}

You might also like