当前位置: 技术问答>linux和unix
如何用命名管道(FIFO)实现非阻塞模式进程间通讯(IPC)?
来源: 互联网 发布时间:2016-09-17
本文导语: 下面代码用阻塞模式实现进程间通讯,Lucy和Kate用阻塞模式进行聊天通讯。 /*Lucy.c*/ #include #include #include #include #include #include /*全局变量*/ char write_fifo_name[] = "read-fifo.txt"; char read_fifo_name[] = "write-fifo.txt"; ...
下面代码用阻塞模式实现进程间通讯,Lucy和Kate用阻塞模式进行聊天通讯。
/*Lucy.c*/
#include
#include
#include
#include
#include
#include
/*全局变量*/
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 namedPipen");
memset(buf,0,sizeof(buf));
len = kateReadNamedPipe(read_fd,buf);
/*业务处理*/
if ( len > 0) {
buf[len] = '';
printf("Data From Lucy: %s **** Length:%dn", buf,strlen(buf));
}
else if (len == 0) /*管道为空*/
{
printf("Data From Lucy:NULL **** Length:0n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Lucy: Error **** Length:-1n");
}
printf("kate read namedPipe overn");
printf("kate ready to write namedPipen");
printf(" Data To Lucy:%s **** Length:%dn",pMsgToLucy,strlen(pMsgToLucy));
strcpy(buf,pMsgToLucy);
kateWriteNamedPipe(write_fd, buf);
printf("kate write namedPipe overn");
}
close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
/*Kate.c*/
#include
#include
#include
#include
#include
#include
#include
/*全局变量*/
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 namedPipen");
printf(" Data To Kate :%s Length:%dn",pSendMessage,strlen(pSendMessage));
memset(buf,0,sizeof(buf));
strcpy(buf,pSendMessage); /*准备写入管道的数据*/
lucyWriteNamedPipe(write_fd, buf); /*向管道中写数据*/
printf("lucy write namedPipe overn");
printf("lucy Ready to read namedPipen");
memset(buf,0,sizeof(buf));
len = lucyReadNamedPipe(read_fd,buf); /*从管道中读文件*/
/*业务处理*/
if (len > 0)
{
buf[len] = '';
printf("Data From Kate : %s **** Length:%dn", buf,len);
}
else if (0 == len) /*管道为空,没有数据可读*/
{
printf("Data From Kate:NULL **** Length:0n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Kate: Error **** Length:-1n");
}
printf("lucy read namedPipe overn");
}
close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
/*Lucy.c*/
#include
#include
#include
#include
#include
#include
/*全局变量*/
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 namedPipen");
memset(buf,0,sizeof(buf));
len = kateReadNamedPipe(read_fd,buf);
/*业务处理*/
if ( len > 0) {
buf[len] = '';
printf("Data From Lucy: %s **** Length:%dn", buf,strlen(buf));
}
else if (len == 0) /*管道为空*/
{
printf("Data From Lucy:NULL **** Length:0n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Lucy: Error **** Length:-1n");
}
printf("kate read namedPipe overn");
printf("kate ready to write namedPipen");
printf(" Data To Lucy:%s **** Length:%dn",pMsgToLucy,strlen(pMsgToLucy));
strcpy(buf,pMsgToLucy);
kateWriteNamedPipe(write_fd, buf);
printf("kate write namedPipe overn");
}
close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
/*Kate.c*/
#include
#include
#include
#include
#include
#include
#include
/*全局变量*/
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 namedPipen");
printf(" Data To Kate :%s Length:%dn",pSendMessage,strlen(pSendMessage));
memset(buf,0,sizeof(buf));
strcpy(buf,pSendMessage); /*准备写入管道的数据*/
lucyWriteNamedPipe(write_fd, buf); /*向管道中写数据*/
printf("lucy write namedPipe overn");
printf("lucy Ready to read namedPipen");
memset(buf,0,sizeof(buf));
len = lucyReadNamedPipe(read_fd,buf); /*从管道中读文件*/
/*业务处理*/
if (len > 0)
{
buf[len] = '';
printf("Data From Kate : %s **** Length:%dn", buf,len);
}
else if (0 == len) /*管道为空,没有数据可读*/
{
printf("Data From Kate:NULL **** Length:0n");
}
else if (len == -1) /*读管道出现异常*/
{
printf("Data From Kate: Error **** Length:-1n");
}
printf("lucy read namedPipe overn");
}
close(write_fd);
unlink(write_fifo_name);
close(read_fd);
}
|
write_fd = open(write_fifo_name, O_NONBLOCK); /*非阻塞模式*/
改成
write_fd = open(write_fifo_name, O_WRONLY|O_NONBLOCK); /*非阻塞模式*/
试试
改成
write_fd = open(write_fifo_name, O_WRONLY|O_NONBLOCK); /*非阻塞模式*/
试试
|
直接用O_NONBLOCK有什么问题吗?
|
多读几次试试看