IO进程线程基础练习(2023-3-6)

1.要求实现AB进程对话

a.A进程先发送—句话给B进程,B进程接收后打印
b.B进程再回复—句话给A进程,A进程接收后打印
c.重复1.2步骤,当收到quit后,要结束AB进程

2.在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。

通过线程实现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
struct info
{
    sem_t sem1,sem2;
    int fd_r;
    int fd_w;
};
void *WriteMesge(void *arg)
{
    char buf[128]="";
    struct info *ADC=(struct info*)arg;
    while(1)
    {
        if(sem_wait(&ADC->sem1)<0)
        {
            perror("sem_post");
            pthread_exit(NULL);
        }
        bzero(buf,sizeof(buf));
    //    printf("请输入>>>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]=0;

        if(write(ADC->fd_w,buf,sizeof(buf))<0)
        {
            perror("write");
            pthread_exit(NULL);
        }
    //    printf("写入成功\n");
        sem_post(&ADC->sem1);

        if(strcmp(buf,"quit")==0)
            break;
    }
//    sem_post(&ADC->sem1);
    pthread_exit(NULL);
}
void *ReadMesge(void *arg)
{
    struct info *ADC=(struct info*)arg;
        while(1)
        {
            char buf[128]="";
            if(sem_wait(&ADC->sem2)<0)
            {
                perror("sem_post");
                pthread_exit(NULL);
            }

            ssize_t res=0;
            bzero(buf,sizeof(buf));
            res=read(ADC->fd_r,buf,sizeof(buf));
            if(res<0)
            {
                perror("read");
                pthread_exit(NULL);
            }else if(0==res)
            {
                printf("对端程序退出\n");
                break;
            }
            if(strcmp(buf,"quit")==0)
                res=0;
            printf("buf=%s\n",buf);
            //printf("res=%ld buf=%s\n",res,buf);
            sem_post(&ADC->sem2);
        }
        pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
    struct info ADC;
    if(mkfifo("./fifo",0775)<0)
    {
        if(17!=errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    if(mkfifo("./fifo1",0775)<0)
    {
        if(17!=errno)
        {
            perror("mkfifo");
            return -1;
        }
    }

    printf("fifo success\n");
    ADC.fd_r=open("./fifo",O_RDONLY);
    if(ADC.fd_r<0)
    { 
        perror("open");
        return -1; 
    }
    ADC.fd_w=open("./fifo1",O_WRONLY);
    if(ADC.fd_w<0)
    {
        perror("open");
        return -1;
    }

    printf("open wr success\n");
    
    if(sem_init(&ADC.sem1,0,1)<0)
    {
        perror("sem_init");
        return -1;
    }
    if(sem_init(&ADC.sem2,0,1)<0)
    {
        perror("sem_init");
        return -1;
    }

    pthread_t tid1,tid2;
    if(pthread_create(&tid1,NULL,WriteMesge,&ADC)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&tid2,NULL,ReadMesge,&ADC)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(tid1,NULL);
    sem_destroy(&ADC.sem1);
    pthread_join(tid1,NULL);
    sem_destroy(&ADC.sem1);

    close(ADC.fd_w);
    close(ADC.fd_r);
    return 0;
}

通过进程实现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
int main(int argc, const char *argv[])
{
    if(mkfifo("./fifo",0775)<0)
    {
        if(17!=errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    if(mkfifo("./fifo1",0775)<0)
    {
        if(17!=errno)
        {
            perror("mkfifo");
            return -1;
        }
    }

    printf("fifo success\n");
    int fd_r=open("./fifo",O_RDONLY);
    if(fd_r<0)
    { 
        perror("open");
        return -1; 
    }
    int fd_w=open("./fifo1",O_WRONLY);
    if(fd_w<0)
    {
        perror("open");
        return -1;
    }

    printf("open wr success\n");
    

    pid_t cpid=fork();
    if(cpid>0)
    {
        while(1)
        {
            char buf[128]="";
            ssize_t res=0;
            bzero(buf,sizeof(buf));
            res=read(fd_r,buf,sizeof(buf));
            if(res<0)
            {
                perror("read");
                return -1;
            }else if(0==res)
            {
                printf("对端程序退出\n");
                break;
            }
            if(strcmp(buf,"quit")==0)
                break;
            printf("buf=%s\n",buf);
        }
    }else if(0==cpid)
    {
        char buf[128]="";
        while(1)
        {
        
        bzero(buf,sizeof(buf));
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]=0;

        if(write(fd_w,buf,sizeof(buf))<0)
        {
            perror("write");
            return -1;
        }
        if(strcmp(buf,"quit")==0)
            break;
    }
    }else
    {
        perror("fork");
        return -1;
    }


    close(fd_w);
    close(fd_r);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值