日期:2014-05-16 浏览次数:20795 次
#include"fifo.h" int main(int argc,char **argv) { int readfifo,writefifo,dummyfd,fd; char *ptr,buff[MAXLINE+1],fifoname[MAXLINE]; pid_t pid; ssize_t n; if((mkfifo(SERV_FIFO,FILE_MODE)<0)&&(errno!=EEXIST)) printf("can't create %s",SERV_FIFO); readfifo=open(SERV_FIFO,O_RDONLY|O_NONBLOCK,0); dummyfd=open(SERV_FIFO,O_WRONLY,0); while((n=read(readfifo,buff,MAXLINE))>0) { if(buff[n-1]=='\n') n--; buff[n]='\0'; if((ptr=strchr(buff,' '))==NULL) { printf("bogus request: %s",buff); continue; } *ptr++=0; pid=atol(buff); snprintf(fifoname,sizeof(fifoname),"/tmp/fifo.%ld",(long)pid); if((writefifo=open(fifoname,O_WRONLY,0))<0) { printf("cannot open:%s",fifoname); continue; } if((fd=open(ptr,O_RDONLY))<0) { snprintf(buff+n,sizeof(buff)-n,":can't open,%s\n",strerror(errno)); n=strlen(ptr); write(writefifo,ptr,n); close(writefifo); } else { while((n=read(fd,buff,MAXLINE))>0) write(writefifo,buff,n); close(fd); close(writefifo); } } exit(0); }