有名管道——读写fifo

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<unistd.h>
 4 #include<string.h>
 5 #include<sys/types.h>
 6 #include<sys/wait.h>
 7 #include<sys/stat.h>
 8 #include<fcntl.h>
 9 int main(void)
10 {
11     int len =0;
12     char buf[100];
13     memset(buf,0 ,sizeof(buf));
14     int fd = open("fifo1",O_RDONLY);
15     while( (len=read(fd,buf,sizeof(buf))) > 0)
16     {
17         printf("%s\n",buf);
18     }
19     close(fd);
20     return 0;
21 }
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<unistd.h>
 4 #include<string.h>
 5 #include<sys/types.h>
 6 #include<sys/wait.h>
 7 #include<sys/stat.h>
 8 #include<fcntl.h>
 9 int main(int arg,char* args[])
10 {
11     int len = 0;
12     char buf[100];
13     memset(buf,0,sizeof(buf));
14     int fd = open("fifo1",O_WRONLY);
15     while(1)
16     {
17         scanf("%s",buf);
18         if(buf[0]=='0')
19             break;
20         write(fd,buf,sizeof(buf));
21     }
22     close(fd);
23     return 0;
24 }
.SUFFIXES:.c .o

CC=gcc
SRCS1=myfifo.c
SRCS2=writefifo.c

OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)

EXEC1=readfifo
EXEC2=writefifo


all: $(OBJS1) $(OBJS2)
    $(CC) -o $(EXEC1) $(OBJS1)
    $(CC) -o $(EXEC2) $(OBJS2)
    @echo '-----------------ok----------------------'
.c.o:
    $(CC) -Wall -g -o $@ -c $<
clean:
    -rm -f $(OBJS1)
    -rm -f $(OBJS2)
    -rm -f core*

转载于:https://www.cnblogs.com/leejxyz/p/5713651.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值