linux禁止write函数,fifo linux-write()函数突然终止程序

我正在用C实现一个管道,其中多个生产者程序(在我的情况下为9)将数据写入一个单一的消费者程序.

问题在于,某些生产者(有时是一两个)有时会在调用write()函数时突然退出程序.

代码很简单,这是生产者代码:

#include

#include

#include

#include

#include

#include

#include

#include

#define MSG_SIZE_BYTES 4

void send(unsigned int * msg){

int fd, msg_size;

int r;

char buffer [5];

char myfifo[50] = "/tmp/myfifo";

fd = open(myfifo, O_WRONLY);

if(fd == -1){

perror("error open SEND to fifo");

}

r = write(fd, msg, MSG_SIZE_BYTES);

if(r == -1){

perror("error writing to fifo");

}

close(fd);

printf("Message send\n");

}

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

int cluster_id = atoi(argv[1]);

unsigned int msg[1];

msg[0] = cluster_id;

while(1){

printf("Press a key to continue...\n");

getchar();

send(msg);

}

}

这是消费者代码

#include

#include

#include

#include

#include

#include

#include

#include

#define MSG_SIZE_BYTES 4

int receive(unsigned int * received_msg){

int fd, msg_size;

int ret_code;

char buffer [5];

char myfifo[50] = "/tmp/myfifo";

fd = open(myfifo, O_RDONLY);

if(fd == -1)

perror("error open RECV to fifo");

ret_code = read(fd, received_msg, MSG_SIZE_BYTES);

close(fd);

if (ret_code == -1){

printf("\nERROR\n");

return 0;

}

return 1;

}

void main(){

mkfifo("/tmp/myfifo", 0666);

unsigned int msg[1];

while(1){

receive(msg);

printf("receive msg from id %d\n", msg[0]);

}

}

我正在使用以下命令编译生产者和消费者:gcc -o my_progam my_program.c

要重现该问题,您需要打开9个终端以运行每个生产者,并打开1个终端以运行使用者.

执行使用者:./consumer

同时在所有终端中执行生产者,并将每次通过命令行传递的关联ID传递给每次执行.例如:./生产者0,./生产者1.

生产者发送消息几次(平均10次)后,一个任意的生产者将突然停止执行,从而显示问题.

下图描述了执行:

Terminals ready to execute

下图描述了生产者ID 3上的错误

Error on producer 3

提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值