I am writing on a UNIX shell.
When CTRL-C
is pressed, the SIGINT
signal is sent. (Is working!)
But when CTRL-Z
is pressed, the process which gets the signal is stopped, but I cannot return to my shell. Only if I close the process, i can return.
Here is my signal_handler()
:
// in the main()
signal(SIGINT, signal_handler);
signal(SIGTSTP, signal_handler);
// when pid == 0, right over execvp()
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
// signal handler
void signal_handler(int signum) {
switch(signum) {
case SIGINT:
cout << "[caught SIGINT]" << endl;
kill(pid, SIGINT);
break;
case SIGTSTP:
cout << "[caught SIGTSTP]" << endl;
kill(pid, SIGTSTP);
break;
default:
break;
}
}
pid_t pid; // is a global variable
// this would be my main()
if((pid = fork()) < 0) {
cout << "Error" << endl;
exit(1);
} else if(pid == 0) { // child
setpgid(0,0);
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
execvp(arguments[0], &arguments[0]);
cout << "Argument not found" << endl;
exit(1);
} else if(checkIfBackground(inputNew) == false) { // mother
int status;
pid_t pid_r;
if(waitpid(pid, &status, 0) < 0) {
cout << "Error" << endl;
}
} else {
cout << "Process is in background" << endl;
}