1

在 C 上编写类似 shell 的程序时,我遇到了信号处理方面的问题。
这是我的代码的简化版本:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define SIZE 255

void sig_handler(int sig){
if (sig == SIGINT)
    printf("\n[shell]$\n");
}

int main()
{
    char input[SIZE];
    printf("[shell]$");
    signal(SIGINT,sig_handler);
    while( gets(input) != NULL ){
    // code of the shell including command interpreter and command execution
        printf("[shell]$");
    }
    return 0;
}

当我运行程序并使用命令“cat”尝试 SIGINT 时,输出显示如下:

[shell]$ ^C  (ctrl+C pressed for the first time)
[shell]$     
^C           (the input position go to the next line, which is unwanted)
[shell]$
cat          (I want it in the same line with [shell]$)
^C
[shell]$
[shell]$     (duplicate printing occurs)

我试图通过删除第二个 \n 来修改函数 void sig_handler(int sig)。然而,情况变得比以前更糟了。程序不会在第一次按下 ctrl+C 时自动触发信号事件。

为了澄清我的问题,这是我提出的两个问题:
1. 如何使输入位置与 [shell]$ 位于同一行?
2. 如何解决重复打印问题?

4

2 回答 2

1

首先,从信号处理程序打印是个坏主意。信号处理程序就像一个中断处理程序 - 它是异步发生的,它可以在您的标准库代码中被引发,并且调用另一个 stdlib 例程可能会弄乱它的不可重入内部(想象一下在循环SIGINT内部进行捕获)。printf()

如果你真的想从内部输出一些东西,你最好使用对文件描述符的原始write()调用。stderr

于 2013-10-09T03:13:25.427 回答
1

@zneak 说的是真的,你可以使用fflush和删除第二\nsig_handler

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define SIZE 255

void sig_handler(int sig){
if (sig == SIGINT)
    printf("\n[shell]$");
    fflush(stdout);
}

int main()
{
    char input[SIZE];
    printf("[shell]$");
    fflush(stdout);
    signal(SIGINT,sig_handler);
    while( gets(input) != NULL ){
    // code of the shell including command interpreter and command execution
        printf("[shell]$");
    }
    return 0;
}
于 2013-10-09T03:12:17.690 回答