c - 为什么我的 C 管道不工作?

标签 c segmentation-fault pipe signal-handling

作为练习,我需要使用信号处理程序,并在收到信号时通过管道在两个进程之间发送一些消息。下面是我的源代码。当我运行它时,只要我在它们的主要方法中调用管道(在本例中为 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;
}

最佳答案

基于快速查看的一些故障。

  • printf() 不是异步信号安全的;不要在信号处理程序中调用它。

  • 您正在读写 1 个字节,这很可能小于 sizeof(int)。

  • 您不能假设 PID 是连续的。在父进程中,fork() 的返回值给出了子进程的 PID。在子进程中,如果父进程在 fork() 之前存储了 getpid() 的返回值,那么你就有了;否则请参阅 getppid()。

关于c - 为什么我的 C 管道不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370667/

相关文章:

c - char* 数组中的元素在复制到新数组时损坏

c++ - Win32 中的 WM_NOTIFY 和父类(super class)链接问题

c - 运行C程序复制的二进制文件时出现段错误

c - 为什么在执行 strcpy 时出现段错误?

c - fork() 子执行命令输出奇怪

java - 如何将输出通过管道传输到文件末尾? (Java,终端)

c - 使用 mac osx 和 c 的硬件信息

c - 如何获得 GtkCellRenderer 的 GtkTreeViewColumn?

C++:在push_back时迭代指针 vector 时出现段错误

linux - 如何使用管道进行非阻塞 IPC(UART 仿真)