作业-11.16

1、FIFO实现管道半双工通信

进程一代码:

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

char buf[128] = "";

void *fifo_w(void *arg)
{
    ssize_t res = 0;
    int fd = open("./fifo", O_WRONLY);
    if(fd <0)
    {
        perror("open");
        return NULL;
    }

    while(1)
    {
        bzero(buf, sizeof(buf));

        printf("一号进程发送>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';
        res = write(fd, buf, sizeof(buf));
        if(res<0)
        {
            perror("write");
            return NULL;
        }else if(0 == strcmp(buf, "quit"))
        {
            printf("一号进程退出\n");
            break;
        }
        
    }
    pthread_exit(NULL);
    close(fd);

}

void *fifo_r(void *arg)
{
    ssize_t res = 0;
    int fd_2 = open("./2_fifo", O_RDONLY);
    if(fd_2 <0)
    {
        perror("open");
        return NULL;
    }

    while(1)
    {

        //接受二号进程的内容
        bzero(buf, sizeof(buf));
        res = read(fd_2, buf, sizeof(buf));
        if(0 == strcmp(buf, "quit"))
        {
            printf("一号进程已退出\n");
            break;
        }else if(res >0)
        {
            printf("二号进程发给一号进程的内容\n");
            printf("\n%s\n", buf);
        }else if(res<0)
        {
            perror("read");
            return NULL;
        }

    }

    pthread_exit(NULL);
    close(fd_2);

}


int main(int argc, const char *argv[])
{
    umask(0);

    if(mkfifo("./fifo", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("FIFO create success\n");

    if(mkfifo("./2_fifo", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("FIFO create success\n");
    pthread_t pid1, pid2;
    if(pthread_create(&pid1, NULL,fifo_w, NULL ) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&pid2, NULL,fifo_r, NULL ) != 0)
    {
        perror("pthread_create");
        return -1;
    }

    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);

    system("rm fifo");
    system("rm 2_2_fifo");

    return 0;
}

进程二代码:

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

char buf[128] = "";

void *fifo_w(void *arg)
{
    ssize_t res = 0;
    int fd_2 = open("./2_fifo", O_WRONLY);
    if(fd_2 <0)
    {
        perror("open");
        return NULL;
    }

    while(1)
    {
        bzero(buf, sizeof(buf));
        printf("二号进程发送>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';
        res = write(fd_2, buf, sizeof(buf));
        if(res<0)
        {
            perror("write");
            return NULL;
        }else if(0 == strcmp(buf, "quit"))
        {
            printf("二号进程退出\n");
            break;
        }
        printf("二号进程已发送,等待一号回复\n");

    }

    pthread_exit(NULL);
    close(fd_2);

}
void *fifo_r(void *arg)
{
    ssize_t res = 0;
    int fd = open("./fifo", O_RDONLY);
    if(fd <0)
    {
        perror("open");
        return NULL;
    }

    while(1)
    {
        bzero(buf, sizeof(buf));
        res = read(fd, buf, sizeof(buf));
            if(0 == strcmp(buf, "quit"))
        {
            printf("一号进程已退出\n");
            break;
        }else if(res >0)
        {
            printf("一号进程发给二号进程的内容\n");
            printf("%s\n", buf);
        }else
        {
            perror("read");
            return NULL;
        }
        
    }
    pthread_exit(NULL);
    close(fd);

}

int main(int argc, const char *argv[])
{
    umask(0);

    if(mkfifo("./fifo", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("FIFO create success\n");

    if(mkfifo("./2_fifo", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("FIFO create success\n");


    pthread_t pid1, pid2;
    if(pthread_create(&pid1, NULL,fifo_w, NULL ) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&pid2, NULL,fifo_r, NULL ) != 0)
    {
        perror("pthread_create");
        return -1;
    }

    pthread_join(pid1, NULL);
    pthread_join(pid2, NULL);


    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值