加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ctrl-c在我的shell中杀死我的后台进程

发布时间:2020-12-16 07:05:36 所属栏目:百科 来源:网络整理
导读:我正在尝试在C中编写一个基本的 shell.我需要做的一件事就是能够同时拥有后台和前台进程. Control-C必须终止前台进程(如果有),并且不得终止任何后台进程. 我为SIGINT写了一个信号处理程序来杀死前台进程.唯一的问题是,如果我有一个后台进程,它也会杀死它.根
我正在尝试在C中编写一个基本的 shell.我需要做的一件事就是能够同时拥有后台和前台进程. Control-C必须终止前台进程(如果有),并且不得终止任何后台进程.

我为SIGINT写了一个信号处理程序来杀死前台进程.唯一的问题是,如果我有一个后台进程,它也会杀死它.根据我的理解,当按下Control-C时,SIGINT会向队列传递给不同的进程,如果有人处理它,那么就是它停止的地方.我的shell应该处理它,所以它不应该传递给后台进程吗?

这是我的代码:

pid_t foreground_pid;

int main(int argc,char *argv[]) {
    signal(SIGINT,INThandler);
    char *buf;

    while(1) {
        fgets(buf,128,stdin);

        */ error checking */
        */ split buf into null terminated char* array (arg_array) 
           and count the number of args (num_args) */

        handlerCommand(buf,arg_array,num_args);

        zombieTerminator();
}

void handleCommand(char *command,char **args,int num) {
    pid_t pid;

    if ((pid = fork()) < 0)
        printf("errorn");
    else if (pid == 0) { // Child
        if (!strcmp(args[num-1],"&")) {
            /* redirect stdin to /dev/null */
        }

        execvp(args[0],args);
        printf("errorn");
        exit(127);
    }

    // parent - either wait (foreground) or continue (background)
    if (!strcmp(args[num-1],"&")) {    
        printf(" [%ld] : %sn",(long)pid,command);
    } else {
        foreground_pid = pid;
        if ((pid = waitpid(pid,&status,0)) < 0) 
            fprintf(stderr,"waitpid errorn");
    }

    return;
}

/** Terminates any zombie processes that finished in the background */
void zombieTerminator(void) {
    int status;
    pid_t pid;

    while ((pid = waitpid(-1,WNOHANG)) > 0) {
        if (pid != foreground_pid) {
            printf(" [%ld] exited with status: %dn",WEXITSTATUS(status));
        }
    }
}

/** Handles the control-c signal from the keyboard */
void INThandler(int sig) {
    if (foreground_pid) {
        kill(foreground_pid,SIGKILL);
        foreground_pid = 0;
    } else {
        printf("n%s? ",cwd);
    }
    fflush(stdout);
}

当我运行前台进程时:

sleep(100)

然后我可以点击contorl-c它会退出.喜欢它应该.但是,如果我运行后台进程:

sleep(100) &

我得到了一个新的提示,就像我应该的那样,但如果我点击了control-c,那么什么都不应该发生.但后台进程被杀死了.

我很想知道如何阻止后台进程被杀死.有任何想法吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读