【发布时间】:2020-03-06 02:29:17
【问题描述】:
我有一个运行 UNIX 基本 shell 的 C 程序,我希望做一些特定的事情。基本上,每当我按 CTRL+C 时,我都希望杀死所有子进程,但要保持父进程处于活动状态,以便循环返回下一行输入。下面的代码似乎可以按预期用于 date、whoami 等基本命令。但是,当我发出以下命令进行测试时,shell 不会循环返回以获取更多输入,之后输入的任何文本都不会执行任何操作。
$sleep 100
$CTRL+C
为了停止 shell,我必须使用 CTRL+Z 来强制程序停止。我需要 SIGINT 信号来中断子进程的睡眠命令并强制父进程在杀死子进程后返回提示用户,但显然我做错了什么。这是解释我的意思的输出。
$ ./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 */
}
}
【问题讨论】:
-
注意:您可以在信号处理程序中安全执行的操作是extremely limited。
printf不是安全操作之一。不过kill是。