我正在编写一个程序,从一个文件中获取一个unix命令列表并按顺序执行它们。为了保持一切正常,我必须初始化每个命令,并通过SIGUSR1一直等待sigwait()。当每个命令被初始化时,每个命令都可以执行。
用法:> program.c input.txt
然而,sigusr1似乎被反复调用,完全超过了sigwait()。这是怎么回事?我尝试过很多不同的东西,但最近它是模仿this answer。换言之,我希望在初始化之后立即为命令发出信号。我想在所有命令都完全初始化时解除信号的锁定

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


void on_sigusr1(int sig)
{
// Note: Normally, it's not safe to call almost all library functions in a
// signal handler, since the signal may have been received in a middle of a
// call to that function.
    printf("SIGUSR1 received!\n");
}

int main(int arc, char* argv[])
{


    FILE *file;
    file = fopen(argv[1] ,"r");

    int BUF_SIZE = 100;

    char *token;
    char buffer[BUF_SIZE];
    char programs[BUF_SIZE];
    char *commands[BUF_SIZE];

    int i = 0;
    int counter = 1;

    while (fgets(buffer, sizeof buffer, file ) != NULL)
    {
        strcpy(programs, buffer);

        int length = strlen(buffer)-1;
        if (buffer[length] == '\n')
        {
            buffer[length] = '\0';
        }

        i = 0;
        token = strtok(buffer," ");
        while(token != NULL)
        {
            commands[i++] = token;
            token = strtok(NULL, " ");
        }
        commands[i] = 0;

        pid_t pids[counter];


        // Set a signal handler for SIGUSR1
        signal(SIGUSR1, &on_sigusr1);

    // At program startup, SIGUSR1 is neither blocked nor pending, so raising it
    // will call the signal handler
        raise(SIGUSR1);

    // Now let's block SIGUSR1
        sigset_t sigset;
        sigemptyset(&sigset);
        sigaddset(&sigset, SIGUSR1);
        sigprocmask(SIG_BLOCK, &sigset, NULL);

    // SIGUSR1 is now blocked, raising it will not call the signal handler
        printf("About to raise SIGUSR1\n");
        raise(SIGUSR1);
        printf("After raising SIGUSR1\n");


        for(i = 0; i < counter; ++i)
        {
            pids[i] = fork();

            if(pids[i] > 0)
            {
                printf("Child process %d ready to execute command %s", getpid(), programs);
                // SIGUSR1 is now blocked and pending -- this call to sigwait will return
                // immediately
                int sig;
                int result = sigwait(&sigset, &sig);
                if(result == 0) {
                    printf("Child process %d executing command %s", getpid(), programs);
                    execvp(commands[0], commands);
                }
            }
        }

        // All programs have been launched
        for(i = 0; i < counter; ++i)
        {
            wait(&pids[i]);
        }

        // All programs are waiting to execute
        for (i = 0; i < counter; ++i)
        {
        // SIGUSR1 is now no longer pending (but still blocked).  Raise it again and
        // unblock it
            raise(SIGUSR1);
            printf("About to unblock SIGUSR1\n");
            sigprocmask(SIG_UNBLOCK, &sigset, NULL);
            printf("Unblocked SIGUSR1\n");
        }
    }

exit(0);
fclose(file);
return 0;
}

更新:尝试将signal()更改为sigaction()。没有变化。

最佳答案

在检查pid是否是子进程之后,应该考虑调用sigwait。
所以也许放
国际标准化组织;

int result=sigwait(&sigset,&sig);
在一个if语句中,该语句检查pid是否等于0,这表示它是一个子对象。否则,您就是sigwaiting父进程。
如果PID大于0,则为父进程ID;如果小于0,则为错误。
然后,对于pids数组中的每个进程,您将调用kill(pid_array[i],sigusr1)来解除阻塞。

07-26 06:20