9.c练习-管道&有名管道

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

#define DEBUG   //DEBUG时需要打开
#ifndef DEBUG
#define dbg(format, ...) while(0)
#else
#define dbg(format, ...) printf(format, ##__VA_ARGS__)
#endif

#if 0
// IPC 管道 pipe
int main(void)
{
    int flag = -1;
    pid_t pid = -1;
    int pipefd[2] = {0};
    flag = pipe(pipefd);
    if(-1 == flag)
    {
        perror("pipe error");
        exit(-1);
    }
    pid = fork();
    if(0 > pid)
    {
        perror("fork error");
    }
    if(0 == pid)//子进程
    {
        char buf[20] = {'0'};
        close(pipefd[0]);//关闭读端
        while (1)
        {
            printf("子进程:请输入!\n");
            scanf("%s", buf);
            getchar();
            write(pipefd[1], buf, sizeof(buf));
            memset(buf, 0, sizeof(buf));
            
        }
    }
    if(0 < pid)//父进程
    {
        char buf[20] = {'0'};
        close(pipefd[1]);//关闭写端
        while (1)
        {
            read(pipefd[0], buf, sizeof(buf));
            printf("父进程:%s\n",buf);
            memset(buf, 0, sizeof(buf));
        }
        //回收子进程
        wait(NULL);

    }
    
    
    return 0;
}
#endif

#if 0
// IPC有名管道fifo,实际可以在无关系的两个进程通讯
#define FIFO_PATH "./myfifo"
int main(void)//输入"exit"退出
{
    pid_t pid = -1;
    pid = fork();
    if(0 > pid)
    {
        perror("fork error");
    }
    if(0 < pid)//父进程
    {
        int fifo_fd = -1;
        char buf[20] = {'0'};
        while (-1 == access(FIFO_PATH, F_OK))
        {
            // 创建管道文件
            if(!mkfifo(FIFO_PATH, 0777))
            {
                perror("mkfifo error");
                exit(-1);
            }
        }
        fifo_fd = open(FIFO_PATH, O_WRONLY);
        while (1)
        {
            printf("父进程:请输入\n");
            scanf("%s",buf);
            getchar();//消除回车换行
            write(fifo_fd, buf, sizeof(buf));
            if(!strncmp("exit", buf, 4))
            {
                exit(-1);
            }
            memset(buf, 0, sizeof(buf));
        }
    }
    if(0 == pid)//子进程
    {
        int fifo_fd = -1;
        char buf[20] = {'0'};
        sleep(1);//让父进程先创建文件
        fifo_fd = open(FIFO_PATH, O_RDONLY);
        while (1)
        {
            read(fifo_fd, buf, sizeof(buf));
            printf("子进程输出:%s\n",buf);
            if(!strncmp("exit", buf, 4))
            {
                exit(-1);
            }
            memset(buf, 0, sizeof(buf));
        }
    }
    return 0;
}
#endif



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hehelot

感谢您的打赏!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值