c - 如何强制读取通过

标签 c shell system-calls

所以我正在编写一个简单的 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/

相关文章:

c - 有趣的可执行文件二进制转储

linux - 在带有标题的 block 中分组的行中的数字总和(使用 Bash 和 AWK)

linux - 关闭,比如 vim,会触发 sys_exit 吗?

c++ - 如何使用 Windows API 检索 HD 供应商/序列号

c - 在 C 中的链表中丢失部分

bash - 如何在 bash 中测试文件名扩展结果?

python - 在 python 中多重处理 shell 脚本

c - 如何使用 write() 系统调用将整数写入文件

assembly - 为什么传统模式下的 syscall/sysret 被认为是 "sufficiently poorly designed"?

c - 使用 ShellExecuteEx 在已启动的实例中调用 iexplore.exe