在多个进程写入时读取命名管道

问题描述 投票:-1回答:1

什么样的方法对于解决这个问题是正确的?

例如,我有一个名为write.c的程序,它有4个子进程,子进程将它们的PID写入一个全局命名管道。

另一个名为read.csh的程序会读取这个PID。

我有一个类似于下面的方法,但这种方法有一些问题。它无法读取所有PID,有时3个,有时2个。我认为存在同步问题,我该如何解决这个问题? :

writer.c

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

int main(){ 
    int fd; 
    char * myfifo = "/tmp/myfifo"; //FIFO file
    char buffer[50]; 

    mkfifo(myfifo, 0666); //creating the FIFO

    for(int i=0;i<4;i++){ //creating 4 child process
        if(fork() == 0) { 
            fd = open(myfifo, O_WRONLY); //each child process opens the FIFO for writing their own PID.

            sprintf(buffer, "%d", getpid()); //each child process gets pid and assign it to buffer
            printf("write:%s\n", buffer);  // each child process prints to see the buffer clearly

            write(fd, buffer, strlen(buffer)+1); //each child process writes the buffer to the FIFO
            close(fd);

            exit(0); 
        } 
    } 
    for(int i=0;i<4;i++) { //waiting the termination of all 4 child processes.
        wait(NULL); 
    }
    //parent area
} 

reader.c

#include <stdio.h> 
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h> 
#include <time.h>
#include <string.h>
#include <fcntl.h> 

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

    int fd1; 

    // FIFO file path 
    char * myfifo = "/tmp/myfifo"; 

    // Creating the named file(FIFO) 
    mkfifo(myfifo, 0666); 

    char str1[80]; //str2[80]; 
    while (1) 
    { 
        // First open in read only and read 
        fd1 = open(myfifo,O_RDONLY); 
        read(fd1, str1, 80); 

        // Print the read string and close 
        printf("read: %s\n", str1); 
        close(fd1); 
    } 
} 
c multiprocessing ipc named-pipes fifo
1个回答
2
投票

该行将空字节写入fifo:

write(fd, buffer, strlen(buffer)+1);

因此,如果管道中有两个pid,您将读取以下字符串:

1234\02345\0

printf将打印到第一个\0

1234

要修复它,将PID作为二进制文件而不是格式化和解析文本更容易:

Writer:

    if(fork() == 0) { 
        fd = open(myfifo, O_WRONLY);
        pid_t pid = getpid();
        write(fd, &pid, sizeof(pid));
        close(fd);
        exit(0); 
    } 

The reader:

fd1 = open(myfifo,O_RDONLY); 
pid_t pid;
while (1) // whatever is your termination condition
{ 
    read(fd1, &pid, sizeof(pid)); 
    printf("read: %d\n", pid); 
} 
close(fd1); 
© www.soinside.com 2019 - 2024. All rights reserved.