1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<sys/types.h> 4 #include<sys/stat.h> 5 #include<fcntl.h> 6 #include<errno.h> 7 #define PATH "./fifo" 8 #define SIZE 128 9 int main() 10 { 11 umask(0); 12 if (mkfifo (PATH,0666|S_IFIFO) == -1) 13 { 14 perror ("mkefifo error"); 15 exit(0); 16 } 17 int fd = open (PATH,O_RDONLY); 18 if (fd<0) 19 { 20 printf("open fd is error\n"); 21 return 0; 22 } 23 24 char Buf[SIZE]; 25 while(1){ 26 ssize_t s = read(fd,Buf,sizeof(Buf)); 27 if (s<0) 28 { 29 perror("read error"); 30 exit(1); 31 } 32 else if (s == 0) 33 { 34 printf("client quit! i shoud quit!\n"); 35 break; 36 } 37 else 38 { 39 Buf[s] = '\0'; 40 printf("client# %s ",Buf); 41 fflush(stdout); 42 } 43 } 44 close (fd); 45 return 3; 46 }
1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<unistd.h> 4 #include<sys/types.h> 5 #include<sys/stat.h> 6 #include<string.h> 7 #include<errno.h> 8 #include<fcntl.h> 9 10 #define PATH "./fifo" 11 #define SIZE 128 12 int main() 13 { 14 int fd = open(PATH,O_WRONLY); 15 if (fd < 0) 16 { 17 perror("open error"); 18 exit(0); 19 } 20 21 char Buf[SIZE]; 22 while(1) 23 { 24 printf("please Enter#:"); 25 fflush(stdout); 26 ssize_t s = read(0,Buf,sizeof(Buf)); 27 if (s<0) 28 { 29 perror("read is failed"); 30 exit(1); 31 } 32 else if(s==0) 33 { 34 printf("read is closed!"); 35 return 1; 36 } 37 else{ 38 Buf[s]= '\0'; 39 write(fd,Buf,strlen(Buf)); 40 } 41 } 42 return 0; 43 }
3、實例展現html