多线程,多进程综合练习

1.要求实现AB进程对话

  1.1A进程先发送一句话给B进程,B进程接收后打印

  1.2.B进程再回复一句话给A进程,A进程接收后打印

  1.3重复1.1,1.2步骤,当收到quit后,要结束AB进程

代码A如下

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

int main(int argc, char const *argv[])
{
    int mkAB = mkfifo("./fifoAB",0777);   //创建有名管道fifoAB 实现线程A向B写入
    if(-1 == mkAB){
        if(17 != errno){
        perror("mkfifo");
        return -1;
        }
    }
    printf("fifoAB创建成功\n");

    int fdAB = open("./fifoAB",O_WRONLY);  //线程A以只写方式打开fifoAB
    if(-1 == fdAB){
        perror("open");
        return -1;
    }
    printf("open WO_fifoAB success\n");

    int mkBA = mkfifo("./fifoBA",0777);  //创建有名管道fifoBA 线程A读取线程B的写入
    if(-1 == mkBA){
        if(17 != errno){
        perror("mkfifo");
        return -1;
        }
    }
    printf("fifoBA创建成功\n");

    int fdBA = open("./fifoBA",O_RDONLY);  //线程A以只读方式打开fifoBA
    if(-1 == fdBA){
        perror("open");
        return -1;
    }
    printf("open RO_fifoBA success\n");

    //A线程从终端获取数据,发送给读端
    char buf[32] = "";  //定义存储数据的字符串
    ssize_t res = 0;
    while (1)
    {
        bzero(buf,sizeof(buf));    //清空buf
        printf("请输入>>>");

        fgets(buf,sizeof(buf),stdin);  //从终端获得数据,存储在buf中
        buf[strlen(buf)-1]=0;          //将'\n'转换为'\0'

        if(write(fdAB,buf,sizeof(buf))<0){   //将buf中数据写入有名管道fifoAB
            perror("write");
            return -1;
        }    
        printf("A写入成功\n");

        if(strcasecmp(buf,"quit") == 0)   //如果终端输入quit 退出进程
        break;

        bzero(buf,sizeof(buf));     //清空buf
        res = read(fdBA,buf,sizeof(buf));  //进程A从有名管道fifoBA中将数据读到buf中
        if(res < 0){
            perror("read");
            return -1;
        }  
        else if(0 == res){
            fprintf(stderr,"对方进程退出\n");
            break;
        }
        printf("A reply:%s\n",buf);  //进程A打印接收到的数据
    }

    close(fdAB);   //关闭有名管道
    close(fdBA);
    return 0;
}

代码B如下

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

int main(int argc, char const *argv[])
{
    int mkAB = mkfifo("./fifoAB",0777);//创建有名管道fifoAB 实现线程A向B写入
    if(-1 == mkAB){
        if(17 != errno){
        perror("mkfifo");
        return -1;
        }
    }
    printf("fifoAB创建成功\n");

    int fdAB = open("./fifoAB",O_RDONLY);//线程B以只读方式打开fifoAB
    if(-1 == fdAB){
        perror("open");
        return -1;
    }
    printf("open RO_fifoAB success\n");

    int mkBA = mkfifo("./fifoBA",0777);//创建有名管道fifoBA 实现线程B向A写入
    if(-1 == mkBA){
        if(17 != errno){
        perror("mkfifo");
        return -1;
        }
    }
    printf("fifoBA创建成功\n");

    int fdBA = open("./fifoBA",O_WRONLY);//线程B以只写方式打开fifoBA
    if(-1 == fdBA){
        perror("open");
        return -1;
    }
    printf("open RO_fifoBA success\n");

    char buf[32] = ""; //定义存储数据的字符串
    ssize_t res = 0;
    while (1)
    {
        bzero(buf,sizeof(buf));//清空buf
        res = read(fdAB,buf,sizeof(buf));//进程B从有名管道fifoAB中将数据读到buf中
        if(res < 0){
            perror("read");
            return -1;
        }    
        else if(0 == res){
            fprintf(stderr,"对方进程退出\n");
            break;
        }
        printf("B reply:%s\n",buf);//进程A打印接收到的数据

        bzero(buf,sizeof(buf));//清空buf
        printf("请输入>>>");

        fgets(buf,sizeof(buf),stdin);//从终端获得数据,存储在buf中
        buf[strlen(buf)-1]=0;//将'\n'转换为'\0'

        if(write(fdBA,buf,sizeof(buf))<0){//将buf中数据写入有名管道fifoBA
            perror("write");
            return -1;
        }    
        printf("B写入成功\n");

        if(strcasecmp(buf,"quit") == 0)//如果终端输入quit 退出进程
        break;
    }
    
    close(fdAB);
    close(fdBA);
    return 0;
}

效果如下图1

                                                                      图1

 2.要求1中,A进程先发消息,B接收,然后B发消息,A接收,现要求AB随时收发,B进程不许接收消息后才发消息

代码A如下

#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

char buf[32] = "";  //定义存储数据的字符串
ssize_t res = 0;

void* callBack1(void* arg)
{
    int mkAB = mkfifo("./fifoAB",0777);   //创建有名管道fifoAB 实现线程A向B写入
    if(-1 == mkAB){
        if(17 != errno){
        perror("mkfifo");
        //return -1;
        }
    }
    printf("fifoAB创建成功\n");

    int fdAB = open("./fifoAB",O_WRONLY);  //线程A以只写方式打开fifoAB
    if(-1 == fdAB){
        perror("open");
        //return -1;
    }
    printf("open WO_fifoAB success\n");

    //A线程从终端获取数据,发送给读端
    while (1)
    {
        bzero(buf,sizeof(buf));    //清空buf
        printf("请输入>>>");

        fgets(buf,sizeof(buf),stdin);  //从终端获得数据,存储在buf中
        buf[strlen(buf)-1]=0;          //将'\n'转换为'\0'

        if(write(fdAB,buf,sizeof(buf))<0){   //将buf中数据写入有名管道fifoAB
            perror("write");
            //return -1;
        }    
        printf("A写入成功\n");

        if(strcasecmp(buf,"quit") == 0)   //如果终端输入quit 退出进程
        break;
    }
    close(fdAB); 
    pthread_exit(NULL);
}

void* callBack2(void* arg)
{
    int mkBA = mkfifo("./fifoBA",0777);  //创建有名管道fifoBA 线程A读取线程B的写入
    if(-1 == mkBA){
        if(17 != errno){
        perror("mkfifo");
        //return -1;
        }
    }
    printf("fifoBA创建成功\n");

    int fdBA = open("./fifoBA",O_RDONLY);  //线程A以只读方式打开fifoBA
    if(-1 == fdBA){
        perror("open");
        //return -1;
    }
    printf("open RO_fifoBA success\n");
    while (1)
    {
    bzero(buf,sizeof(buf));     //清空buf
        res = read(fdBA,buf,sizeof(buf));  //进程A从有名管道fifoBA中将数据读到buf中
        if(res < 0){
            perror("read");
            //return -1;
        }  
        else if(0 == res){
            fprintf(stderr,"对方进程退出\n");
            break;
        }
        printf("A reply:%s\n",buf);  //进程A打印接收到的数据
    }
    close(fdBA);
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pthread_t tid1,tid2;
    int pth1 = pthread_create(&tid1,NULL,callBack1,NULL);
    if(0 != pth1){
        printf("pthread_create error\n");
        return -1;
    }
    int pth2 = pthread_create(&tid2,NULL,callBack2,NULL);
    if(0 != pth2){
        printf("pthread_create error\n");
        return -1;
    }


    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    return 0;
}

代码B如下

#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

char buf[32] = ""; //定义存储数据的字符串
ssize_t res = 0;
void* callBack1(void* arg)
{
    int mkAB = mkfifo("./fifoAB",0777);//创建有名管道fifoAB 实现线程A向B写入
    if(-1 == mkAB){
        if(17 != errno){
        perror("mkfifo");
        //return -1;
        }
    }
    printf("fifoAB创建成功\n");

    int fdAB = open("./fifoAB",O_RDONLY);//线程B以只读方式打开fifoAB
    if(-1 == fdAB){
        perror("open");
        //return -1;
    }
    printf("open RO_fifoAB success\n");

    while (1)
    {
        bzero(buf,sizeof(buf));//清空buf
        res = read(fdAB,buf,sizeof(buf));//进程B从有名管道fifoAB中将数据读到buf中
        if(res < 0){
            perror("read");
            //return -1;
        }    
        else if(0 == res){
            fprintf(stderr,"对方进程退出\n");
            break;
        }
        printf("B reply:%s\n",buf);//进程A打印接收到的数据
    }

    close(fdAB);
    pthread_exit(NULL);
}

void* callBack2(void* arg)
{
    int mkBA = mkfifo("./fifoBA",0777);//创建有名管道fifoBA 实现线程B向A写入
    if(-1 == mkBA){
        if(17 != errno){
        perror("mkfifo");
        //return -1;
        }
    }
    printf("fifoBA创建成功\n");

    int fdBA = open("./fifoBA",O_WRONLY);//线程B以只写方式打开fifoBA
    if(-1 == fdBA){
        perror("open");
        //return -1;
    }
    printf("open RO_fifoBA success\n");

    while (1)
    {
        bzero(buf,sizeof(buf));//清空buf
        printf("请输入>>>");

        fgets(buf,sizeof(buf),stdin);//从终端获得数据,存储在buf中
        buf[strlen(buf)-1]=0;//将'\n'转换为'\0'

        if(write(fdBA,buf,sizeof(buf))<0){//将buf中数据写入有名管道fifoBA
            perror("write");
            //return -1;
        }    
        printf("B写入成功\n");

        if(strcasecmp(buf,"quit") == 0)//如果终端输入quit 退出进程
        break;
    }    
    
    close(fdBA);
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pthread_t tid1,tid2;
    int pth1 = pthread_create(&tid1,NULL,callBack1,NULL);
    if(0 != pth1){
        printf("pthread_create error\n");
        return -1;
    }
    int pth2 = pthread_create(&tid2,NULL,callBack2,NULL);
    if(0 != pth2){
        printf("pthread_create error\n");
        return -1;
    }


    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    return 0;
}

效果如下图2

                                                            图2 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值