使用有名管道完成单机QQ(mkfifo)

有名管道

  1. 有名管道的创建
有名管道本质上是一个文件,可以在文件系统中查找到,但是管道的内容是保存到内存的
int ret = mikfifo("./a.txt",0666);
if(0 == ret)
{
	printf("创建成功!\n");
}

“a.txt”是创建的有名管道 "./"是在当前目录下面
后面的权限给的是只写权限,可以根据自己的需要进行更改

2 有名管道的使用

使用的时候要先打开这个有名管道
写:
int fd=open("./fifo_cmd",O_WRONLY);
write(fd,"hello",strlen("hello"));
读:
int fd=open("./fifo_cmd",O_RDONLY);
char buf[32]="";
read(fd,buf,sizeof(buf));

读的时候要根据发的消息来接收,一般来说用数组接收就好了。
要注意的是,在进行读写操作的时候,要先打开这个有名管道。
下面是我写的单机版QQ,能够进行两个程序之间的通信
1.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{

    mkfifo("./a.txt",0666);
    mkfifo("./b.txt",0666);
    
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
    }
    else if(pid == 0)
    {
        //子进程
        int fd2 = open("./b.txt",O_WRONLY);
        while(1)
        {
            char buff2[128]="";
            fgets(buff2,sizeof(buff2),stdin); 
            buff2[strlen(buff2)-1]=0;

            write(fd2,buff2,strlen(buff2));
            if(strcmp(buff2,"再见") == 0 )
            {
                close(fd2);
                break;
            }
        }
      
        _exit(0);
    }
    else
    {
        //父进程
        int fd = open("./a.txt",O_RDONLY);
        
        while(1)
        {
            char buf[128]="";
            read(fd,buf,sizeof(buf));
            printf("读取到的内容:%s\n",buf);
             if(strcmp(buf,"再见")==0)
            {
                close(fd);
                break;

            }
            sleep(1);
        }
        wait(NULL);
    //close(fd);
    }

   
    return 0;
}

2.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
    //创建有名管道
     mkfifo("./a.txt",0666);
    //创建有名管道2
     mkfifo("./b.txt",0666);

    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
    }
    else if(pid == 0)
    {
        //子进程
         //打开有名管道
        int fd = open("./a.txt",O_WRONLY);
        //写管道
        while(1)
        {
            char buff[128]="";
            fgets(buff,sizeof(buff),stdin); 
            buff[strlen(buff)-1]=0;

            write(fd,buff,strlen(buff));
            if(strcmp(buff,"再见") == 0)
            {
                close(fd);
                break;
            }
        }
        
        _exit(0);
    }
    else
    {
        //父进程
        int fd2 = open("./b.txt",O_RDONLY);
       
        while(1)
        {
            char buff2[128]="";
            read(fd2,buff2,sizeof(buff2));
            printf("读取到的内容:%s\n",buff2);
            if(strcmp(buff2,"再见")==0)
            {
                close(fd2);
                break;
            }
            sleep(1);
        }
        wait(NULL);
    }
   


    return 0;
}

运行的效果如下:
运行效果
这个退出是在两个都说“再见”的时候退出。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值