如何用命名管道(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);
}
...全文
193 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello_Wrorld_2010 2010-08-23
  • 打赏
  • 举报
回复
友情帮顶。。。
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);
}

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧