IO作业:使用函数 使用线程实现,实现能够随时收发,即AB可以 随时 互相收发消息:提示 用多线程 或者多进程

A中的内容

**************************************************************************************************************

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
//创建全局变量作为管道
int fd_chd=0;
int fd_far=0;

void * strat_tid(void *arg)
{
    char brr[64]="";//用于管道2
    ssize_t res=0;
    //分支线程用于打印
    while(1)
    {
        //执行读取有名管道2中的内容
        memset(brr,0,sizeof(brr));
        res = read(fd_chd,brr,sizeof(brr));
        if(res<0)
        {
            perror("read");
            return NULL;
        }
        if(strcmp(brr,"quit")==0)
        {
            close(fd_far);
            close(fd_chd);
            exit(0);
        }
        printf("父进程读取长度为 %ld读取到的内容%s\n",strlen(brr),brr);
    }
}
int main(int argc, const char *argv[])
{
    //创建有名管道
    if(mkfifo("./far_talk",0777)<0)
    {
        if(errno!=17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    //创建有名管道
    if(mkfifo("./chd_talk",0777)<0)
    {
        if(errno!=17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    //打开管道1,以写方式打开,阻塞等待读执行
    fd_far=open("./far_talk",O_WRONLY);
    if(fd_far<0)
    {
        perror("open");
        return -1;
    }    
    //打开管道2,以读方式打开,阻塞等待写执行
    fd_chd=open("./chd_talk",O_RDONLY);
    if(fd_chd<0)
    {
        perror("open");
        return -1;
    }
    printf("打开管道完成\n");
    //创建线程
    pthread_t tid;
    if(pthread_create(&tid,NULL,strat_tid,NULL)!=0)
    {
        perror("pthread_create");
        return -1;
    }
    //分离线程
    pthread_detach(tid);
    ssize_t wrs=0;
    char arr[64]="";//用于管道1
    //管道要循环执行的内容
    while(1)
    {
        //先向有名管道中写入
        printf("A中请输入-->");
        if(fgets(arr,sizeof(arr),stdin)<0)     //从终端获取
        {
            perror("fgets");
            return -1;
        }
        arr[strlen(arr)-1]='\0';
        //将获取的内容写入管道1的写中
        wrs =write(fd_far,arr,strlen(arr));                                         
        if(wrs < 0)
        {
            perror("write");
            return -1;
        }
        if(strcmp(arr,"quit")==0)//结束条件
        {
            break;
        }
        printf("写入的大小 %ld,写入了什么 %s\n",strlen(arr),arr);
    }
    pthread_cancel(tid);
    pthread_join(tid,NULL);
            close(fd_far);
            close(fd_chd);
    return 0;
}
**************************************************************************************************************

B中的内容

**************************************************************************************************************

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
//创建全局变量作为管道
int fd_chd=0;
int fd_far=0;
void * strat_tid(void *arg)
{
    char brr[64]="";//用于管道2
    ssize_t res=0;
    //分支线程用于打印
    while(1)
    {
        //执行读取有名管道fd_far中的内容
        memset(brr,0,sizeof(brr));
        res = read(fd_far,brr,sizeof(brr));
        if(res<0)
        {
            perror("read");
            return NULL;
        }
        printf("父进程读取长度为 %ld读取到的内容%s\n",strlen(brr),brr);
        if(strcmp(brr,"quit")==0)
        {
            close(fd_far);
            close(fd_chd);
            exit(0);
        }
    }
}
int main(int argc, const char *argv[])
{
    //创建有名管道
    if(mkfifo("./far_talk",0777)<0)
    {
        if(errno!=17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    //创建有名管道
    if(mkfifo("./chd_talk",0777)<0)
    {
        if(errno!=17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    //打开管道1,以读方式打开,    
    fd_far=open("./far_talk",O_RDONLY);
    if(fd_far<0)
    {
        perror("open");
        return -1;
    }    
    //打开管道2,以写方式打开,
    fd_chd=open("./chd_talk",O_WRONLY);
    if(fd_chd<0)
    {
        perror("open");
        return -1;
    }
    printf("d打开管道完成\n");
    //创建线程,分支线程中用来读管道fd_far的内容,主线程用来写到管道fd_chd里
    pthread_t tid;
    if(pthread_create(&tid,NULL,strat_tid,NULL)!=0)
    {
        perror("pthread_create");
        return -1;
    }
    //分离线程
    pthread_detach(tid);

    ssize_t wrs=0;
    char arr[64]="";//用于管道1
    //管道要循环执行的内容
    while(1)
    {
        //先向有名管道中写入
        printf("b中请输入-->");
        if(fgets(arr,sizeof(arr),stdin)<0)     //从终端获取
        {
            perror("fgets");
            return -1;
        }
        arr[strlen(arr)-1]='\0';
        //将获取的内容写入管道fd_chd的写中
        wrs =write(fd_chd,arr,strlen(arr));                                         
        if(wrs < 0)
        {
            perror("write");
            return -1;
        }
        if(strcmp(arr,"quit")==0)//结束条件
        {
            return 0;
        }
        printf("写入的大小 %ld,写入了什么 %s\n",strlen(arr),arr);
    }
    pthread_cancel(tid);
    pthread_join(tid,NULL);
            close(fd_far);
            close(fd_chd);

    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值