📄 p11-10.c
字号:
#include "cliserv.h"int main(){ int server_fifo_fd, client_fifo_fd; struct data_to_pass my_request; int times_to_send ; char client_fifo_name[256]; pid_t mypid; /* 打开服务管道 */ server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY); if (server_fifo_fd == -1) err_exit("Sorry, no server"); /* 创建以进程ID命名的客户接收有名管道 */ mypid = getpid(); sprintf(client_fifo_name, CLIENT_FIFO_NAME, mypid); if (mkfifo(client_fifo_name, 0777) == -1) err_exit(client_fifo_name); /* 向服务进程连续发送和接收5次数据 */ for (times_to_send = 0; times_to_send < 5; times_to_send++) { /* 形成给服务进程的数据 */ sprintf(my_request.text_data, "Hello from %d,%d",mypid, times_to_send); my_request.client_pid = mypid; /* 向服务进程发出请求 */ printf("%d sent: %s, ", mypid, my_request.text_data); write(server_fifo_fd, &my_request, sizeof(my_request)); /* 从服务进程接收回答,为此,先打开客户接收有名管道 */ client_fifo_fd = open(client_fifo_name, O_RDONLY); if (client_fifo_fd != -1) { if (read(client_fifo_fd, &my_request, sizeof(my_request)) > 0) { printf("%d received: %s\n", mypid, my_request.text_data); } close(client_fifo_fd); } } /* 关闭服务FIFO并删除客户接收FIFO */ close(server_fifo_fd); unlink(client_fifo_fd); exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -