首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C管道:糟糕的文件描述符

C管道:糟糕的文件描述符
EN

Stack Overflow用户
提问于 2017-11-25 20:10:24
回答 1查看 1.2K关注 0票数 2

我在C中有一个简单的生产者消费者程序,试着用叉子来解决它,当生产者试图在管道上写东西的时候,我得到了一个错误:我已经用同样的逻辑编写了另一个程序,但是这个程序没有给我任何线索来知道为什么?

生产者未能在管道上写入项:糟糕的文件描述符

有人知道我为什么会犯这个错误吗?谢谢

代码语言:javascript
复制
#define READ 0
#define WRITE 1
int mutex = 1, full = 0, empty = BUFFER_SIZE, x = 0;

void consumer();

void producer();

int wait_(int);

int signal_(int);

int pipefd[2];

int main() {
    printf("Starting producer-consumer problem!\n");
    //We intend to run the producer in parent process and the consumer in the child process
    if (pipe(pipefd) == -1) {       /* An error has occurred. */
        fprintf(stderr, "%s", "The call to pipe() has failed.\n");
        exit(EXIT_FAILURE);
    }
    for (int j = 0; j < sizeof(pipefd); j++) {
        if (pipe(&pipefd[j]) < 0) { //Initialize each pipe appropriately
            perror("Error in making pipe...");
        }
    }
    pid_t pid = fork();
    if (pid < 0) {
        perror("**********Error in creating fork()!**************\n");
        exit(STDERR_FILENO);
    } else if (pid == 0) {
        consumer();//We intend to run the consumer in child
    } else {
        producer();//We intend to run the producer in parent
    }
    return 0;
}

int wait_(int s) {
    return (--s);
}

int signal_(int s) {
    return (++s);
}

void producer() {
    printf("Starting Producer\n");
    //while (1) {
    //sleep(1);
    if (close(pipefd[READ]) != 0) {
        perror("Error in closing reading pipe");
    }
    if (write(pipefd[WRITE], &full, 1) < 0) {
        perror("Producer failed to write item on pipe");
    }
    if ((mutex == 1) && (empty != 0)) {
        mutex = wait_(mutex);
        full = signal_(full);
        empty = wait_(empty);
        x++;
        printf("Producer produces the item %d\n", x);
        mutex = signal_(mutex);
    }
    if (close(pipefd[WRITE]) != 0) {
        perror("Error in closing writing pipe");
    }
    //}
}

void consumer() {
    printf("Starting Consumer\n");
    //while (1) {
    //sleep(1);
    int status = 0;
    wait(&status);              /* wait for all children to return back the result */
    if (close(pipefd[WRITE]) != 0) {
        perror("Error in closing reading pipe");
    }
    if (read(pipefd[READ], &full, 1) > 0) {
        printf("Consumer\t%d\n", full);
    }
    if ((mutex == 1) && (full != 0)) {
        mutex = wait_(mutex);
        full = wait_(full);
        empty = signal_(empty);
        printf("Consumer consumes item %d\n", x);
        x--;
        mutex = signal_(mutex);
    }
    if (close(pipefd[READ]) != 0) {
        perror("Error in closing reading pipe");
    }
    //}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-25 20:14:37

sizeof运算符返回大小(以字节为单位)。因此,在一个典型的int为四个字节的系统中,sizeof(pipefd)将产生值8。这不是循环中正确的元素数。

而且,pipe(&pipefd[j])也不正确。pipefd中的两个管道已经被初始化为“适当”。不需要再初始化了。特别是在这种情况下和上一种情况下,您将拥有https://en.wikipedia.org/wiki/Undefined_behavior

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47490077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档