所以我正在编写一个简单的 shell。相关代码贴在下面。
void sig_int_handler(int signum) {
if (pid == -1) // do nothing
else kill(...);
}
signal(SIGINT, sig_int_handler);
..
while(1) {
pid = -1;
printf(COMMAND_PROMPT);
input_len = read(...);
if only enter is pressed: continue;
// parse inputs
pid = fork();
if (pid == 0) { // Do child process operations }
else if (pid > 0) { // Parent waits for child }
else {..}
}
但现在当我按下 ctrl+c 时,它会正确地退出 child 。但是,如果我在常规终端中按 ctrl+c,它会在 stdout 上打印出“ctrl+c”,但随后它什么都不做。我如何强制读取一个换行符以便它给我另一个提示?
最佳答案
我假设您正在尝试在这里编写类似 shell 的东西。
如果是这样:您不必在 shell 中处理 ^C。如果您为子进程设置一个新的进程组(使用 setpgid()
和 TIOCSPGRP
),只有它会收到 SIGINT。
关于c - 如何强制读取通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616678/