English 中文(简体)
IPC FIFO 生产者-消费者
原标题:IPC FIFO Producer-Consumer Deadlock

这是生产者。

// speak.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define FIFO_NAME "american_maid"

int main(void)
{
    char s[300];
    int num, fd;

    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    printf("waiting for readers...
");
    fd = open(FIFO_NAME, O_WRONLY);
    printf("got a reader--type some stuff
");

    while (gets(s), !feof(stdin)) {
        if ((num = write(fd, s, strlen(s))) == -1)
            perror("write");
        else
            printf("speak: wrote %d bytes
", num);
    }

    return 0;
}

这是消费者。

//tick.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define FIFO_NAME "american_maid"

int main(void)
{
    char s[300];
    int num, fd;

    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    printf("waiting for writers...
");
    fd = open(FIFO_NAME, O_RDONLY);
    printf("got a writer
");

    do {
        if ((num = read(fd, s, 300)) == -1)
            perror("read");
        else {
            s[num] =   ;
            printf("tick: read %d bytes: "%s"
", num, s);
        }
    } while (num > 0);

    return 0;
}

当我管理这些产品时,生产者的产出就是:

waiting for readers...

消费者产出

waiting for writers...

speak,t 查询读者,rite>。 http://beej.us/guide/bgipc/output/html/multipage/fifos.html#fifopc”rel=“nofollow” 顺便说一句,open(>)(发言.c)将一直受阻,直至<>open(>(p.c)。 反之亦然。 因此,我猜想会出现僵局或情况。 我需要解决这一问题。

问题回答

它认为,你在读者和作者之间有种族条件。

为了确定这一点,你需要一种方法,在作者“积极”之前,不启动读者。 为此,我建议,在撰写人准备就绪时,向它提供管道和书写。 接着,从 for子读到尾后,胎儿就已经准备好,读者应当工作。

You need to use forks here because coordinating mutexes between a parent and a child process is non-trivial and properly done pipes is easier.

另外,您称mknod(>两次。 获赠后,将1号重回法改为errno= EEXIST,但需更加谨慎。 为避免这种情况,使读者和作者能够发挥一种作为论据的途径的作用。

兹将作者改写为<代码>int 话(const char *fifo, int benfd),并改名为(const char *fifo)

这样,就象这样:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

const char fifo_name[] /* = ... */;

int speak(const char *fifo, int pipefd);
int tick(const char *fifo);

int main() {
    int pipefd[2];
    pipe(pipefd);

    mknod(fifo_name, S_IFIFO | 0666, 0);

    if (fork() == 0) {
        close(pipefd[0]);
        return speak(fifo_name, pipefd[1]);
    } else {
        close(pipefd[1]);
        char foo;
        read(pipefd[0], &foo, 1);
        return tick(fifo_name);
    }
}

修改撰稿人,在编造纤维后(即:在拨打<代码>后的权利)打印出一个(任何内容)的斜体(......), 页: 1

逐字使用我的代码,因为为了简洁起见,我略去看错误。

如果读者和作家准备就绪,公开将返回。 由于公开被阻止,因此我认为,空洞的功能是成功的。 愿各位以不同方式赞扬这两个进程。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...