write
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[1024] = {0};
if(argc != 2)
{
fprintf(stderr,"usage: %s fifo\n",argv[0]);
return -1;
}
/* if (mkfifo(argv[1],0666) < 0 && errno == EEXIST)*/
/*{*/
/*perror("Fail to mkfifo");*/
/*return -1;*/
/*}*/
fd = open(argv[1],O_RDWR);
while(1)
{
fgets(buf,sizeof(buf),stdin);
write(fd,buf,sizeof(buf));
if(!strncmp(buf,"quit",4))
{
exit(0);
}
}
return 0;
}
read
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[1024] = {0};
if(argc != 2)
{
fprintf(stderr,"usage: %s fifo\n",argv[0]);
return -1;
}
/* if (mkfifo(argv[1],0666) < 0 && errno == EEXIST)*/
/*{*/
/*perror("Fail to mkfifo");*/
/*return -1;*/
/*}*/
fd = open(argv[1],O_RDWR);
while(1)
{
/* fgets(buf,sizeof(buf),stdin);*/
/*write(fd,buf,sizeof(buf));*/
read(fd,buf,sizeof(buf));
if(!strncmp(buf,"quit",4))
{
exit(0);
}
printf("Read:%s\n",buf);
}
return 0;
}