我正在写一个 shell 并想要实现信号SIGSTP
和SIGINT
. 当用户启动一个新进程并按下CTRL+C时,它应该向进程发送一个 SIGINT,当按下CTRL+时Z,进程应该得到 SIGSTP 信号。
到目前为止,这是我的代码:
string inputNew = input;
vector<char*> arguments;
size_t lastChar = 0;
for(int i = 0; i < input.size(); i++)
{
char& c = input[i];
if((c == ' ' || c == '\t'||c == '\n') && lastChar != i)
{
c = '\0';
arguments.push_back(&input[0] + lastChar);
lastChar = i+1;
}
}
bool checkIfBackground(string & input)
{
size_t lastChar = input.size() - 1;
if(input[lastChar] == '&')
{
return true;
}
return false;
}
if((pid = fork()) < 0) {
exit(1);
} else if(pid == 0) {
execvp(arguments[0], &arguments[0]);
exit(1);
} else if(checkIfBackground(inputNew) == false) {
int status;
pid_t pid_r;
if(waitpid(pid, &status, 0) < 0) {
cout << "PID not known!";
}
} else {
cout << "Prozess is waiting in the background." << endl;
}
我不知道如何在我的代码中实现 SIGSTP 和 SIGINT 信号。