簡體   English   中英

在 C / UNIX 中使用 SIGINT 信號時遇到問題

[英]Having problems working with SIGINT signal in C / UNIX

我有這個 C 程序,它為 UNIX 運行基本的 shell,我想做一些具體的事情。 基本上,每當我按 CTRL+CI 時,我都希望殺死所有子進程,但要保持父進程處於活動狀態,以便循環返回下一行輸入。 下面的代碼似乎可以按預期用於 date、whoami 等基本命令。 但是,當我發出以下命令進行測試時,shell 不會循環返回以獲得更多輸入,之后輸入的任何文本都不會執行任何操作。

$sleep 100 
$CTRL+C

為了停止 shell,我必須使用 CTRL+Z 強制程序停止。 我需要 SIGINT 信號來中斷子進程的睡眠命令並強制父進程在殺死子進程后返回提示用戶,但顯然我做錯了什么。 這是 output 來解釋我的意思。

$ ./a3shell2
--------myShell > date
 Input command is: date
Sat Nov  9 15:38:08 CST 2019
--------myShell > whoami
 Input command is: whoami
klm46
--------myShell > ^C
Caught SIGINT!
killing children
date
 Input command is: date
Sat Nov  9 15:38:20 CST 2019
--------myShell > sleep 100
 Input command is: sleep 100
^C
Caught SIGINT!
killing children
date
date
whoami
^Z
[2]+  Stopped                 ./a3shell2

Shell 程序:

/* ----------------------------------------------------------------- */
/* PROGRAM  simple shell.c progam                                    */
/* ----------------------------------------------------------------- */
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <assert.h>
#include <sched.h>

pid_t pid = 0;

void sig_int(int signo) 
{
    printf("\nCaught SIGINT!\n");
  printf("killing children\n"); 
  if (pid != 0) 
  {
    kill(pid, SIGKILL); 
    pid = 0; 
  }
}

char *getinput(char *buffer, size_t buflen) 
{
    printf("--------myShell > ");
    return fgets(buffer, buflen, stdin);
}

/* ----------------------------------------------------------------- */
/* FUNCTION  parse:                                                  */
/* ----------------------------------------------------------------- */

void  parse(char *line, char **argv)
{
     while (*line != '\0') {       /* if not the end of line ....... */ 
          while (*line == ' ' || *line == '\t' || *line == '\n')
               *line++ = '\0';     /* replace white spaces with 0    */
          *argv++ = line;          /* save the argument position     */
          while (*line != '\0' && *line != ' ' && 
                 *line != '\t' && *line != '\n') 
               line++;             /* skip the argument until ...    */
     }
     *argv = '\0';                 /* mark the end of argument list  */
}

/* ----------------------------------------------------------------- */
/* FUNCTION execute:                                                 */
/* ----------------------------------------------------------------- */

void execute(char **argv)
{
     //pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {     /* fork a child process           */
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process:         */
          if (execvp(*argv, argv) < 0) {     /* execute the command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                                  /* for the parent:      */
          while (wait(&status) != pid)       /* wait for completion  */
               ;
     }
}

/* ----------------------------------------------------------------- */
/*                  The main program starts here                     */
/* ----------------------------------------------------------------- */

void  main(void)
{

     char  line[1024];             /* the input line                 */
     char  *argv[64];              /* the command line argument      */
     size_t linelen;

if (signal(SIGINT, sig_int) == SIG_ERR) 
  {
        fprintf(stderr, "signal error: %s\n", strerror(errno));
        exit(1);
    }

     while (1) 
     {                   /* repeat until done ....         */
          getinput(line, sizeof(line));
          line[strlen(line) - 1] = '\0';
          printf(" Input command is: %s \n", line);

          parse(line, argv);       /*   parse the line               */

          if (strcmp(argv[0], "exit") == 0)  /* is it an "exit"?     */
               exit(0);            /*   exit if it is                */

          execute(argv);           /* otherwise, execute the command */
     }
}

當您CTRL + C時,您的 shell 陷入無休止的wait(&status) != pid循環。

SIGINT 中斷wait() ,並且您的信號處理程序將全局pid變量設置為零。 當控制返回到循環時,它永遠不會結束: pid為零,並且wait() 只能返回 PID 和 -1 ,永遠不會為零。

(您看不到datewhoami的這種行為,因為在您發出CTRL + C時,這些子進程已經終止。在這種情況下,您可能正在中斷fgets() ,而不是wait() ,並將 SIGKILL 發送到已經獲得了 PID。這也不好,因為當您發送 SIGKILL 時,PID 可能會被重新使用。)

順便說一句,當 SIGINT 中斷您的wait()調用時,尚不清楚實際發生了什么,因為signal()在不同平台上的行為歷來不同。 調用可能會被中斷,返回 -1 (EINTR),或者它可能會恢復wait() ,返回kill() ed 子進程的 PID。 在這種情況下它沒有實際意義——它們都不匹配你當時正在尋找的零——但這是使用sigaction()而不是signal()的一個很好的理由。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM