我正在尝试用c编写一个shell。它的一部分是一个处理程序,用于捕获 SIGTSTP 信号并将其设置为使程序进入和退出仅前台模式。
以下是相关的代码片段:
//global variables
int global;
//header
void catch_tstp(int);
//main function
int main(int argc, char** argv){
...
// initiate sigaction struct for CTRL-Z action
struct sigaction ctrlz_act;
ctrlz_act.sa_handler = catch_tstp;
ctrlz_act.sa_flags = SA_SIGINFO|SA_RESTART;
sigfillset(&(ctrlz_act.sa_mask));
sigaction(SIGTSTP, &ctrlz_act, NULL);
global = 0;
...
}
//handler
void catch_tstp(int sig){
if(sig == SIGTSTP){
if(global){
global=0;
printf("Entering foreground-only mode (& is now ignored)\n");
}
else{
global=1;
printf("Exiting foreground-only mode\n");
}
}
}
现在我的输出看起来像这样:
: ^ZExiting foreground-only mode //pressed ctrl+z
//nothing here, had to press enter again
: //pressing enter just gives me another :, which is what I want
: ^ZEntering foreground-only mode (& is now ignored)
^ZExiting foreground-only mode
我希望输出看起来像这样:
: ^Z
Entering foreground-only mode (& is now ignored)
: //":"should show up own automatically on the next line after I press ctrl-z, then enter
: ^Z
Exiting foreground-only mode
:
谁能指出我做错了什么?任何帮助将不胜感激。谢谢!