Fork me on GitHub

有名管道——读写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*
posted @ 2016-07-28 10:05  千秋此意  阅读(551)  评论(0编辑  收藏  举报