📄 example2-03.c
字号:
/************************************************************* * 用FIFO实现进程间通信的例子example2-03.c example2-04.c ************************************************************/ /******************************************************************************************* * 因为FIFO不要求进程间有亲缘关系,服务器端就可以以众所周知的路径名创建一个FIFO,并打开来读。 * 此后,另外的客户进程就可以打开该FIFO来写,将消息通过FIFO传给服务器。但这只是一种单向的数据 * 传送,要实现双向的数据传送,可以每个客户进程在创建一个FIFO(可以用自己的进程ID来命名), * 从而在实现从服务器到客户端的通信。 *******************************************************************************************/ /******************************************************************************************* * 此例中客户端向服务器端发送一个路径名,服务器端去打开客户所请求的文件并返回文件内容,客户收 * 到回应后将文件内容打印到标准输出。服务器以路径名/tmp/fifo.serv来创建FIFO,并从此FIFO中读出 * 客户请求。而客户端都创建自己的FIFO,路径名以自己的进程号来标识。然后将自己的请求写入服务器 * 创建的FIFO中,二次请求中包含它的进程号和请求的路径名。服务器据此将相应的文件内容写回到该进 * 程创建的FIFO中,从而实现双向通信。 *******************************************************************************************/ /***************************************** * 服务器端程序example2-03.c *****************************************/ /***************************************************************************************************第53、54行两次用open()打开此FIFO文件,一次只读,一次只写。而第二次打开后获得的描述符从未使用。这是因为,若只打开一次,那么每当一个客户终止时,该FIFO就会变空,服务器的read也会返回0,表示文件结束。这样,就只能关闭次FIFO,然后再次调用read。但是这次会阻塞调用,直到下一个客户请求到为止。如果总是有一个文件描述符被打开进行写操作,那么read函数就不会返回,而是一直阻塞直到有客户请求为止。 ***************************************************************************************************/ #include <sys/types.h> #include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#define SERV_FIFO "/tmp/fifo.serv"#define LINE 1024#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)int main(){ int readf, writef, dummyfd, fd; char *ptr, buff[LINE], fifoname[LINE]; pid_t pid; //进程标识符 ssize_t n;/* create fifo use wellknow pathname */ if (((mkfifo(SERV_FIFO, FILE_MODE)<0) && (errno != EEXIST))) //未产生文件或文件不存在,则 printf("can't create fifo"); readf = open(SERV_FIFO, O_RDONLY, 0); /* open the fifo with read only mode */ dummyfd = open(SERV_FIFO, O_WRONLY, 0); /* open the fifo with write only mode */ while ((n = getline(buff, 100, readf)) > 0) { if (buff[n-1] == '\n') n--; /* delete the return character */ buff[n] = '\0'; /* end the pathname with null character */ if ((ptr = strchr(buff, ' ')) == NULL) { printf("bogus request: %s", buff); continue; } *ptr++ = 0; pid = atoi(buff);/* create the pathname of the client's fifo */ snprintf(fifoname, sizeof(fifoname), "/tmp/fifo.%ld", (long)pid); if ((writef = open(fifoname, O_WRONLY, 0)) < 0) { printf("cannot open: %s", fifoname); continue; } if ((fd = open(ptr, O_WRONLY)) < 0) { snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",strerror(errno)); n = strlen(ptr); write(writef, ptr, n); /* write the file to the client's fifo */ close(writef); } else { while ((n = read(fd, buff, LINE)) > 0) write(writef, buff, n); close(fd); close(writef); } } exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -