IO:进程多线程实现简单的相互聊天的程序

无名管道 int pipe(int pipefd[2])

特点:

  1. 因为管道没有名字,因此无法用open()函数打开。
  2. 只能用于亲缘间进程通信,不能在无关联的进程中通信
  3. 半双工工作模式,pipefp[0]一端读操作,pipefp[1]一端写操作,不能同时进行读写。
  4. 是一种特殊文件,在内存中存储,由内核进行管理
  5. 读写可以使用文件IO中read,write函数
  6. 无名管道操作属于一次性操作,如果对无名管道进行读操作,数据会被全部读走

注意事项

  1. 当管道无数据时,执行读操作,读操作阻塞
  2. 无名管道大小是固定的,管道一旦满,写操作就会导致进程阻塞 大小为:1024*64
  3. 写入数据不会覆盖前面的数据,会存储到后面,像队列一样,读数据会将读取到数据从管道中直接拿走
  4. 将读端关闭,向无名管道写入数据,管道会破裂,进程收到信号(SIGPIPE),会将进程杀死
  5. 当管道中有数据时,关闭写端,读操作可以继续执行,数据读完后,读取的返回值为0

 代码示例

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ 
    FILE *a = fopen("./1.txt","r");
    FILE *fd = fopen("./2.txt","w");
    int fp[2];
    int ret = pipe(fp);
    if(-1 == ret)
    {
        perror("pipe");
        exit(-1);
    }
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        exit(-1);
    }
    else if (0 == pid)
    {
        while(1)
        {
            close(fp[0]);//关闭从管道中读取操作
            char buf[200] = {0};
            fgets(buf,sizeof(buf),a);//从标准输入中读取数据到数组buf中
            write(fp[1],buf,strlen(buf));//将buf中的数据通过管道写入到内存中
        }
    }
    else
    {
        while(1)
        {
            close(fp[1]);//关闭从管道中写入操作
            char arr[200] = {0};
            char buf[200] = {0};
            read(fp[0],arr,sizeof(arr));//从内存中将数据通过管道读取到arr数组中
            printf("%s",arr);
        }
    }
    //while(1);
    fclose(a);
    fclose(fd);
    return 0;
} 

 有名管道int mkfifo(const char *pathname, mode_t mode);

特点;

  1. 有名管道存在文件系统中,数据在内存中
  2. 可以用于无亲缘关系的进程
  3. 只有读端和写端同时存在管道才能打开成功

注意事项

  1. 读端不存在时,写端写入数据将会阻塞
  2. 读端意外结束,写端再写入数据将会管道破裂,进程结束
  3. 有名管道的数据存储在内存中,数据交互在内核中

 聊天程序的实现原理

  1. 创建两个有名管道,fp1,fp2
  2. 编写两个程序,一个主程序自己接收和发送信息,另一个程序其他人接收你发的数据和发送数据给你。
  3. 程序主要函数:open(),write(),read(),fgets(),close(),memset,

聊天软件代码1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{ 
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        exit(-1);
    }
    else if(pid == 0)
    {
        int fp2 = open("./f2",O_WRONLY);
        char buf[100] = {0};
        while(1)
        {
            //printf("mesend:");
            fgets(buf,sizeof(buf),stdin);
            write(fp2,buf,strlen(buf)-1);
            //memset(buf,0,sizeof(buf));
        }
    }
    else
    {
        int fp1 = open("./f1",O_RDONLY);
        char buf[100] = {0};
        int n = 1;
        while(1)
        {
            n = read(fp1,buf,sizeof(buf));
            if(0 == n)
                break;
            printf("mereceived:%s\n",buf);
            memset(buf,0,sizeof(buf));
        }
    }
    return 0;
} 

聊天软件代码2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    pid_t pid = fork();
    if(pid <0)
    {
        perror("fork");
        exit(-1);
    }
    else if(pid == 0)
    {
        int fp1 = open("./f1",O_WRONLY);
        char buf[100] = {0};
        while(1)
        {
            //printf("yousend:");
            fgets(buf,sizeof(buf),stdin);
            write(fp1,buf,strlen(buf)-1);
        }
    }
    else
    {
        int fp2 = open("./f2",O_RDONLY);
        char buf[100] = {0};
        int n = 1;
        while(1)
        {
            n = read(fp2,buf,sizeof(buf));
            if(0 == n)
                break;
            printf("youreceived:%s\n",buf);
            memset(buf,0,sizeof(buf));
        }
    }
    return 0;
}

 实现效果通信

me:
Can you show me your photos

mereceived:No, mom won't let me play with bad guys

Then I won't play with you

mereceived:baibai

you:
youreceived:Can you show me your photos

No, mom won't let me play with bad guys

youreceived:Then I won't play with you

baibai

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雨的路口

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值