1

私は自分のミニバッシュを書いていて、デフォルトの SIGINT 動作を無効にしたかったのですが (「終了」のみが bash を終了する必要があります)、SIGINT は実行中の子を殺す可能性があります (たとえば、「sleep 60 | sleep 30」を実行しています)。

SIGINT をキャッチするために、signal(SIGINT, catchSignal);関数を使用しています。問題は、ミニバッシュ内で ^C を送信しても、それが強制終了されることです =(

GNU によると: http://www.gnu.org/software/libc/manual/html_node/Basic-Signal-Handling.html

編集:おそらく、Linux ではなく Mac で意図したとおりに動作することに言及する価値があります!

A signal handler is just a function that you compile together with the rest of the program. Instead of directly invoking the function, you use signal or sigaction to tell the operating system to call it when a signal arrives.

したがって、^C を押すと、catchSignal() が実行され、これ以外に何もないことを理解しています。右?

はいの場合、ミニバッシュが終了する理由は、これが私の catchSignal() です。確かにkill()、実行中の子だけが持っています。

サンプル実行

[22:33:31][user][~/edu/sysprog/lab4]$ ./minibash 
mbash% ^CCatched Sigint
Inside Sigint
No children

コード:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <signal.h>

#include "sighant.h"

// Global variable
extern pidList children;

void catchSignal(int recvSign)
{
    printf("Catched Sigint\n");
    if(recvSign == SIGINT)
    {
        printf("Inside Sigint\n");
        if(children.count < 1)
        {
            printf("No children\n");
        }

        for(int i = 0; i < children.count; i++)
        {
            if(children.child[i] >= 0)
            {
                printf("KILLIN\n\n");
                kill(children.child[i], SIGKILL);
            }
        }
    }
    else
    {
        fprintf(stderr, "[ERROR] Could not execute Signal! (reason uknown)");
    }
}

コード2

int main(void)
{

    fprintf(stderr, "mbash%% ");
    fflush(stderr);

    signal(SIGINT, catchSignal);

    .......
4

1 に答える 1