3

演習として、シグナルハンドラーを使用し、シグナルを取得するときに2つのプロセス間でいくつかのメッセージを送信するためにパイプを使用する必要があります。以下は私のソースコードです。実行しているときは、パイプを機能させることができます。メインメソッド(この場合はprocess1()とprocess2())でパイプを呼び出す限り、両方のプロセスが通信できます。しかし、私はシグナルハンドラー内のパイプを使用したいと思います。しかし、今ではパイプは機能しません。これは私が得たいくつかの出力です:

3 - 4 and 5 - 6
Segv at 8825
USR1 at 8824
898 sent to 4
130 received on 3
130

'898'と'130'は等しいはずですが、そうではありません。私はパイプが正しく機能していることを知っているので、それは信号物と関係があると思います...しかし何...?

ソースコード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

int fd1[2], fd2[2], status;
int cpid, cpoid;

void process1() {   
    cpid = getpid();        /*What's my process ID?*/
    cpoid = cpid + 1;       /*And what's the other process ID?*/

    close(fd1[0]);
    close(fd2[1]);

    while (1) {}
}

void process2() {   
    cpid = getpid();
    cpoid = cpid - 1;

    close(fd1[1]);
    close(fd2[0]);

    raise(SIGSEGV);         /*Start with a SegV signal*/

    while (1) {}
}

/*Method to send a message to the other process, by pipe*/
void send (int msg) {
    if (cpid < cpoid) {
        write(fd1[1], &msg, 1);
        printf("%d sent to %d\n", msg, fd1[1]);
    } else {
        write(fd2[1], &msg, 1);
        printf("%d sent to %d\n", msg, fd2[1]);
    }
}

/*Method to receive a message from the other process*/
int receive () {
    int msg = 0;
    if (cpid < cpoid) {
        read(fd2[0], &msg, 1);
        printf("%d received on %d\n", msg, fd2[0]);
    } else {
        read(fd1[0], &msg, 1);
        printf("%d received on %d\n", msg, fd1[0]);
    }
    return msg;
}

/*The SegV Signal handler*/
void segvHandler() {
    int y = -1;
    printf("Segv at %d\n", cpid);
    kill(cpoid, SIGUSR1);           /*Send an USR1 Signal to the other proces*/

    while (y != 898) {
        y = receive();
        printf("%d\n", y);
    }
}

/*The Usr1 Signal handler*/
void usr1Handler() {
    int x = 898;
    printf("USR1 at %d\n", cpid);

    send(x);
}

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

    if (pipe(fd1) < 0) {
        fprintf (stderr, "Could not make pipe\n");
        return (EXIT_FAILURE);
    }
    if (pipe(fd2) < 0) {
        fprintf (stderr, "Could not make pipe\n");
        return (EXIT_FAILURE);
    }
    printf("%d - %d and %d - %d\n", fd1[0], fd1[1], fd2[0], fd2[1]);    /*Pipe numbers*/

    signal(SIGUSR1, usr1Handler);   /*Signal handlers*/
    signal(SIGSEGV, segvHandler);

    if (fork() != 0) {
        process1();
    } else {
        process2();
    }
    waitpid(-1, &status, 0);

    return EXIT_SUCCESS;
}
4

2 に答える 2

3

クイックルックに基づくいくつかの障害。

  • printf()は非同期シグナルセーフではありません。シグナルハンドラーで呼び出さないでください。

  • 1バイトを読み書きしていますが、これはsizeof(int)よりも小さい可能性があります。

  • PIDが連続しているとは限りません。親では、fork()の戻り値が子のPIDを示します。子では、親がfork()の前にgetpid()の戻り値を格納している場合、それがあります。それ以外の場合は、getppid()を参照してください。

于 2011-06-16T11:43:19.200 に答える
2

コメントで述べたように、シグナルハンドラーでprintfを呼び出さないでください。ただし、それはおそらく問題ではありません。マシン上でintが1バイトでない限り、パイプに1バイトしか書き込んでいないため、int全体を書き込んだり読み取ったりしていないという問題があります。(コードをwrite(fd [1]、&msg、sizeof msg)に変更し、読み取り時に同じ変更を加えます。)

于 2011-06-16T11:32:16.700 に答える