如何用命名管道(FIFO)实现非阻塞模式进程间通讯(IPC)?

ssuuuvvv999 2010-08-10 10:30:52
下面代码用阻塞模式实现进程间通讯,Lucy和Kate用阻塞模式进行聊天通讯。

/*Lucy.c*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

/*全局变量*/
char write_fifo_name[] = "read-fifo.txt";
char read_fifo_name[] = "write-fifo.txt";
int write_fd, read_fd;

/*初始化*/
int kateInitNamedPipe()
{
/*创建管道文件*/
int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
if ( (ret == -1) &&(errno!=EEXIST)) {
printf("Fail to create FIFO %s: %s", write_fifo_name, strerror(errno));
return -1;
}

/*读管道文件的ID*/
while ((read_fd = open(read_fifo_name, O_RDONLY)) == -1)
//while ((read_fd = open(read_fifo_name, O_NONBLOCK)) == -1)
{
sleep(1);
}

/*读管道文件的ID*/
write_fd = open(write_fifo_name, O_WRONLY);
//write_fd = open(write_fifo_name, O_NONBLOCK);
if (write_fd == -1) {
printf("Fail to open FIFO %s: %s", write_fifo_name, strerror(errno));
return -1;
}
return 1;
}

/*写管道文件*/
int kateWriteNamedPipe(int w_fd, char *mscIDState)
{
int wLen = write(w_fd, mscIDState, strlen(mscIDState));
return wLen;
}

/*读管道文件*/
int kateReadNamedPipe(int r_fd,char *pCalledNo)
{
int len = read(r_fd, pCalledNo, 256);
return len;
}

int main(void)
{
kateInitNamedPipe();
char buf[256]; /*每次读写的数据大小*/
int len; /*读到的数据大小*/
char pMsgToLucy[] ="Hi,Lucy !";

while (1) {
printf("kate ready to read namedPipe\n");
memset(buf,0,sizeof(buf));
len = kateReadNamedPipe(read_fd,buf);

/*业务处理*/
if ( len > 0) {
buf[len] = '\0';
printf("Data From Lucy: %s **** Length:%d\n", buf,strlen(buf));
}
else if (len == 0) /*管道为空*/
{
printf("Data From Lucy:NULL **** Length:0\n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Lucy: Error **** Length:-1\n");
}
printf("kate read namedPipe over\n");

printf("kate ready to write namedPipe\n");
printf(" Data To Lucy:%s **** Length:%d\n",pMsgToLucy,strlen(pMsgToLucy));
strcpy(buf,pMsgToLucy);
kateWriteNamedPipe(write_fd, buf);
printf("kate write namedPipe over\n");
}

close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}

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

/*全局变量*/
char write_fifo_name[] = "write-fifo.txt";
char read_fifo_name[] = "read-fifo.txt";
int write_fd, read_fd; /*管道文件的句柄*/
struct stat stat_buf; /*结构体对象,暂时未用到*/

/*初始化*/
int lucyInitNamedPipe()
{
/*创建命名管道*/
int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
if ( (ret == -1) &&(errno!=EEXIST)) {
printf("Fail to create FIFO!!! %s: %s", write_fifo_name, strerror(errno));
return -1;
}

/*写管道的ID*/
write_fd = open(write_fifo_name, O_WRONLY); /*阻塞模式*/
//write_fd = open(write_fifo_name, O_NONBLOCK); /*非阻塞模式*/
if (write_fd == -1) {
printf("Fail to open FIFO %s: %s", write_fifo_name, strerror(errno));
return 0;
}

/*读管道的ID*/
while ((read_fd = open(read_fifo_name, O_RDONLY)) == -1) /*阻塞模式*/
//while ((read_fd = open(read_fifo_name, O_NONBLOCK)) == -1) /*非阻塞模式*/
{
sleep(1); /*sleep休眠1秒钟*/
}
return 1;
}

/*写管道文件*/
int lucyWriteNamedPipe(int w_fd, char *pCalledNo)
{
int wLen = write(w_fd, pCalledNo, strlen(pCalledNo));
return wLen;
}

/*读管道文件*/
int lucyReadNamedPipe(int r_fd,char *mscIDState)
{
int len = read(r_fd, mscIDState, 256);
return len;
}

int main(void)
{
lucyInitNamedPipe(); /*管道文件初始化*/
char buf[256]; /*每次读写的数据大小*/
int len; /*读取的数据长度*/
char pSendMessage[] ="Hi,Kate";

/*循环读写管道文件*/
while (1) {
printf("lucy Ready to write namedPipe\n");
printf(" Data To Kate :%s Length:%d\n",pSendMessage,strlen(pSendMessage));
memset(buf,0,sizeof(buf));
strcpy(buf,pSendMessage); /*准备写入管道的数据*/
lucyWriteNamedPipe(write_fd, buf); /*向管道中写数据*/
printf("lucy write namedPipe over\n");

printf("lucy Ready to read namedPipe\n");
memset(buf,0,sizeof(buf));
len = lucyReadNamedPipe(read_fd,buf); /*从管道中读文件*/

/*业务处理*/
if (len > 0)
{
buf[len] = '\0';
printf("Data From Kate : %s **** Length:%d\n", buf,len);
}
else if (0 == len) /*管道为空,没有数据可读*/
{
printf("Data From Kate:NULL **** Length:0\n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Kate: Error **** Length:-1\n");
}
printf("lucy read namedPipe over\n");
}

close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
...全文
112 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
友情帮顶。。。
回复
ssuuuvvv999 2010-08-11

进程AAA非阻塞模式创建一个管道,用来写数据。
char write_fifo_name[] = "write-fifo.txt";
int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
write_fd = open(write_fifo_name, O_NONBLOCK); /*非阻塞模式*/


进程BBB阻塞模式打开这个管道时,
死在open函数那里,不向下走了。。。
char read_fifo_name[] = "write-fifo.txt";
printf("111111!\n");
while ((read_fd = open(read_fifo_name, O_RDONLY)) == -1)
{
printf("2222!\n"); /*该行不输出*/
sleep(1);
}
printf("3333!\n"); /*该行不输出*/
回复
ssuuuvvv999 2010-08-10
下面代码用阻塞模式实现进程间通讯,Lucy和Kate用阻塞模式进行聊天通讯。

/*Lucy.c*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

/*全局变量*/
char write_fifo_name[] = "read-fifo.txt";
char read_fifo_name[] = "write-fifo.txt";
int write_fd, read_fd;

/*初始化*/
int kateInitNamedPipe()
{
/*创建管道文件*/
int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
if ( (ret == -1) &&(errno!=EEXIST)) {
printf("Fail to create FIFO %s: %s", write_fifo_name, strerror(errno));
return -1;
}

/*读管道文件的ID*/
while ((read_fd = open(read_fifo_name, O_RDONLY)) == -1)
//while ((read_fd = open(read_fifo_name, O_NONBLOCK)) == -1)
{
sleep(1);
}

/*读管道文件的ID*/
write_fd = open(write_fifo_name, O_WRONLY);
//write_fd = open(write_fifo_name, O_NONBLOCK);
if (write_fd == -1) {
printf("Fail to open FIFO %s: %s", write_fifo_name, strerror(errno));
return -1;
}
return 1;
}

/*写管道文件*/
int kateWriteNamedPipe(int w_fd, char *mscIDState)
{
int wLen = write(w_fd, mscIDState, strlen(mscIDState));
return wLen;
}

/*读管道文件*/
int kateReadNamedPipe(int r_fd,char *pCalledNo)
{
int len = read(r_fd, pCalledNo, 256);
return len;
}

int main(void)
{
kateInitNamedPipe();
char buf[256]; /*每次读写的数据大小*/
int len; /*读到的数据大小*/
char pMsgToLucy[] ="Hi,Lucy !";

while (1) {
printf("kate ready to read namedPipe\n");
memset(buf,0,sizeof(buf));
len = kateReadNamedPipe(read_fd,buf);

/*业务处理*/
if ( len > 0) {
buf[len] = '\0';
printf("Data From Lucy: %s **** Length:%d\n", buf,strlen(buf));
}
else if (len == 0) /*管道为空*/
{
printf("Data From Lucy:NULL **** Length:0\n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Lucy: Error **** Length:-1\n");
}
printf("kate read namedPipe over\n");

printf("kate ready to write namedPipe\n");
printf(" Data To Lucy:%s **** Length:%d\n",pMsgToLucy,strlen(pMsgToLucy));
strcpy(buf,pMsgToLucy);
kateWriteNamedPipe(write_fd, buf);
printf("kate write namedPipe over\n");
}

close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}

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

/*全局变量*/
char write_fifo_name[] = "write-fifo.txt";
char read_fifo_name[] = "read-fifo.txt";
int write_fd, read_fd; /*管道文件的句柄*/
struct stat stat_buf; /*结构体对象,暂时未用到*/

/*初始化*/
int lucyInitNamedPipe()
{
/*创建命名管道*/
int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
if ( (ret == -1) &&(errno!=EEXIST)) {
printf("Fail to create FIFO!!! %s: %s", write_fifo_name, strerror(errno));
return -1;
}

/*写管道的ID*/
write_fd = open(write_fifo_name, O_WRONLY); /*阻塞模式*/
//write_fd = open(write_fifo_name, O_NONBLOCK); /*非阻塞模式*/
if (write_fd == -1) {
printf("Fail to open FIFO %s: %s", write_fifo_name, strerror(errno));
return 0;
}

/*读管道的ID*/
while ((read_fd = open(read_fifo_name, O_RDONLY)) == -1) /*阻塞模式*/
//while ((read_fd = open(read_fifo_name, O_NONBLOCK)) == -1) /*非阻塞模式*/
{
sleep(1); /*sleep休眠1秒钟*/
}
return 1;
}

/*写管道文件*/
int lucyWriteNamedPipe(int w_fd, char *pCalledNo)
{
int wLen = write(w_fd, pCalledNo, strlen(pCalledNo));
return wLen;
}

/*读管道文件*/
int lucyReadNamedPipe(int r_fd,char *mscIDState)
{
int len = read(r_fd, mscIDState, 256);
return len;
}

int main(void)
{
lucyInitNamedPipe(); /*管道文件初始化*/
char buf[256]; /*每次读写的数据大小*/
int len; /*读取的数据长度*/
char pSendMessage[] ="Hi,Kate";

/*循环读写管道文件*/
while (1) {
printf("lucy Ready to write namedPipe\n");
printf(" Data To Kate :%s Length:%d\n",pSendMessage,strlen(pSendMessage));
memset(buf,0,sizeof(buf));
strcpy(buf,pSendMessage); /*准备写入管道的数据*/
lucyWriteNamedPipe(write_fd, buf); /*向管道中写数据*/
printf("lucy write namedPipe over\n");

printf("lucy Ready to read namedPipe\n");
memset(buf,0,sizeof(buf));
len = lucyReadNamedPipe(read_fd,buf); /*从管道中读文件*/

/*业务处理*/
if (len > 0)
{
buf[len] = '\0';
printf("Data From Kate : %s **** Length:%d\n", buf,len);
}
else if (0 == len) /*管道为空,没有数据可读*/
{
printf("Data From Kate:NULL **** Length:0\n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Kate: Error **** Length:-1\n");
}
printf("lucy read namedPipe over\n");
}

close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告