0

I am writing on a shell and want to implement the signals SIGSTP and SIGINT. When the user starts a new process and presses CTRL+C it should send a SIGINT to the process and when CTRL+Z is pressed the process should get the SIGSTP signal.

Here is my code so far:

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

I have no idea how to implement the SIGSTP and SIGINT signals inside my code.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
user3653164
  • 979
  • 1
  • 8
  • 24

1 Answers1

0

See the sigaction(2) manual pages. It explains how to set up an implement a signal handler.

Note that a signal handler is asynchronous. This has a number of implications. Read the manual page, and spend some time in Google.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148