【发布时间】:2015-09-02 10:46:11
【问题描述】:
我正在使用命名管道在两个进程之间进行通信。
作家.c
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
write(fd, "hi", sizeof("hi"));
write(fd, "4:1.jpg,2.jpg;3.jpg", sizeof("4:1.jpg,2.jpg;3.jpg"));
write(fd, "hi2", sizeof("hi2"));
write(fd, "5:1.jpg,2.jpg;3.jpg", sizeof("5:1.jpg,2.jpg;3.jpg"));
write(fd, "6:1.jpg,2.jpg;3.jpg", sizeof("6:1.jpg,2.jpg;3.jpg"));
write(fd, "7:1.jpg,2.jpg;3.jpg", sizeof("6:1.jpg,2.jpg;3.jpg"));
write(fd, "8:1.jpg,2.jpg;3.jpg", sizeof("6:1.jpg,2.jpg;3.jpg"));
write(fd, "9:1.jpg,2.jpg;3.jpg", sizeof("6:1.jpg,2.jpg;3.jpg"));
sleep(5);
return 0;
}
阅读器.c
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 2048
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
for( i =0; i < 6 ; i++)
{
int a = read(fd, buf, MAX_BUF);
printf("buf has : %s with size = %d \n", buf,a);
}
return 0;
}
输出如下:
buf has : hi with size = 3
buf has : 4:1.jpg,2.jpg;3.jpg with size = 124
buf has : 4:1.jpg,2.jpg;3.jpg with size = 0
buf has : 4:1.jpg,2.jpg;3.jpg with size = 0
buf has : 4:1.jpg,2.jpg;3.jpg with size = 0
buf has : 4:1.jpg,2.jpg;3.jpg with size = 0
我想知道其他字符串去了哪里?有人可以告诉我出了什么问题。我需要输出就像所有字符串必须一个接一个地出现。
【问题讨论】:
标签: c ipc named-pipes