1

我在处理“CTRL-C”信号时遇到了一些问题,我需要的是 handle_SIGINT 只输出一次,然后返回到设置函数中的读取,但我不知道该怎么做这个。我被告知要在 length=read 调用周围添加一个循环,因为 read 是一个阻塞调用。我只是对如何处理这些信号感到困惑。无论如何,我当前的输出是:

COMMAND->test
COMMAND->test2
COMMAND->test3
COMMAND->test4
COMMAND->^CFound your CTRL-C
Found your CTRL-C
COMMAND->Found your CTRL-C
COMMAND->Found your CTRL-C
COMMAND->error reading the command: Interrupted system call
^Z
Suspended


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */

void handle_SIGINT()
{
  printf("Found your CTRL-C\n");
}

void setup(char inputBuffer[], char *args[],int *background)
{
    int length, /* # of characters in the command line */
         i,      /* loop index for accessing inputBuffer array */
         start,  /* index where beginning of next command parameter is */
        ct;     /* index of where to place the next parameter into args[] */

     ct = 0;

     /* read what the user enters on the command line */
     length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  

     start = -1;
     if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
     if (length < 0){
        perror("error reading the command");
    exit(-1);           /* terminate with error code of -1 */
    }

     /* examine every character in the inputBuffer */
     for (i = 0; i < length; i++) { 
        switch (inputBuffer[i]){
        case ' ':
        case '\t' :               /* argument separators */
            if(start != -1){
                args[ct] = &inputBuffer[start];    /* set up pointer */
                ct++;
            }
            inputBuffer[i] = '\0'; /* add a null char; make a C string */
            start = -1;
            break;

        case '\n':                 /* should be the final char examined */
            if (start != -1){
                args[ct] = &inputBuffer[start];     
                ct++;
            }
            inputBuffer[i] = '\0';
            args[ct] = NULL; /* no more arguments to this command */
            break;

        case '&':
            *background = 1;
            inputBuffer[i] = '\0';
            break;

        default :             /* some other character */
            if (start == -1)
                start = i;
    } 
    }    
    args[ct] = NULL; /* just in case the input line was > 80 */
} 

int main(void)
{
    char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

    struct sigaction handler;
    handler.sa_handler = handle_SIGINT;
    sigaction(SIGINT, &handler, NULL);

    while (1){            /* Program terminates normally inside setup */
        background = 0;
    printf("COMMAND->");
        fflush(0);
        setup(inputBuffer, args, &background);       /* get next command */

        int pid;

    pid = fork(); /* for a child process */

    if (pid < 0) { /* check if error occurred with child process */
      fprintf(stderr, "Fork Failed");
      return 1;
    } else if (pid == 0) { /* child process */
      execvp(args[0], args);
    } else {
      if (background == 0){ /* check if the parent should wait */
        //wait(args);
        waitpid(pid, NULL, 0);
      }
    }

    /* the steps are:
      (1) fork a child process using fork()
      (2) the child process will invoke execvp()
      (3) if background == 0, the parent will wait, 
        otherwise returns to the setup() function. */
    }
}
4

5 回答 5

3

当 read() 返回 -1 时,检查errno- 如果是则EINTR返回以再次执行读取。 EINTR表示阻塞系统调用(在我们的例子中是 read())被信号中断了,所以你可以安全地返回执行再读一个

   EINTR  The call was interrupted by a signal before any data was read; see signal(7).

另一方面,在许多情况下按下 CTRL+C 表示用户想要终止程序,因此退出程序是预期的行为。

于 2012-10-19T20:09:18.493 回答
1

告诉您的程序在打印消息后忽略 handle_SIGINT 中的 SIGINT 信号。

void handle_SIGINT()
{
  printf("Found your CTRL-C\n");
  signal(SIGINT, SIG_IGN);
}
于 2012-10-19T20:11:50.333 回答
0

这就是您应该使用以下方式编写的sigaction()

struct sigaction new_handler,old_handler;

new_handler.sa_handler=handle_SIGINT;
sigemptyset(&new_handler.sa_mask);
new_handler.sa_flags=0;

sigaction(SIGINT,NULL, &old_handler);
if(old_handler.sa_handler != SIG_IGN)
   sigaction(SIGINT, &new_handler,NULL);
于 2012-10-19T20:47:22.083 回答
0

作为建议,请尝试使您的程序尽可能可移植,我的意思是,在我看来,您可以使用 ANSI C 编写大量程序。

考虑使用 fgets() 而不是 read() 并且不要从不依赖行为,即使该行为有据可查,如 bimda 指出的那样。

void setup(char inputBuffer[], char *args[],int *background)
{
    char *ret; 
    int length; /* # of characters in the command line */
    int i;      /* loop index for accessing inputBuffer array */
    int start=-1;  /* index where beginning of next command parameter is */
    int ct=0;     /* index of where to place the next parameter into args[] */


     /* read what the user enters on the command line */
     ret = fgets(inputBuffer, MAX_LINE, stdin);  

     if(!ret)
        exit(0);            /* ^d was entered, end of user command stream */

    /* examine every character in the inputBuffer */
    for (i = 0; inputBuffer[i]; i++) {
            ...
    }
    ...
}

和抄送部分作为patater说:

void handle_SIGINT()
{
    printf("Found your CTRL-C\n");
    signal(SIGINT, SIG_IGN);
}

~$ 人信号

也许这不是你需要的,但这只是我的拙见。

于 2012-10-19T23:07:05.547 回答
0

正如上面其他人所说,我只是添加了一个while循环:

while((length = read(STDIN_FILENO, inputBuffer, MAX_LINE) == -1);  
于 2018-04-28T14:21:34.530 回答