Linux--进程间通信(二)-命名管道(pipe)通信
来源:互联网 发布:4gip网络加速器 费用 编辑:程序博客网 时间:2024/06/03 17:45
- server.c
- client.c
- pipelib.h
- 编译命令:
gcc -o server server.c pipelib.h
- 编译命令:
gcc -o client client.c pipelib.h
- 运行命令:
./server
- 运行命令:
./client.c
server.c
#include "pipelib.h"#include <stdio.h>#include <string.h>#include <stdlib.h>int main() { int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_name[NAME_SIZE]; message msg; char *p; if ( mkfifo(SERVER_FIFO_NAME, 0777) == -1) {//创建服务器从客户端接收消息的管道文件 fprintf(stderr, "Sorry,create server fifo failure!\n" ); return -1; } server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);//以读的方式打开服务器管道 if (server_fifo_fd == -1) { fprintf(stderr, "Sorry server fifo open failure!\n" ); return -1; } sleep(5); while ( res = read(server_fifo_fd, &msg, sizeof(msg)) > 0) { p = msg.data; while (*p){ *p = toupper(*p);/*转换为大写字母*/ ++p; } sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid); client_fifo_fd = open(CLIENT_FIFO_NAME, O_WRONLY);/*以写的方式打开客户端通道*/ if (client_fifo_fd == -1){ fprintf(stderr, "Sorry,client fifo open failure!\n" ); return -1; } write(client_fifo_fd, &msg, sizeof(msg));/*将消息通过client的管道发送给客户端*/ close(client_fifo_fd); } close(server_fifo_fd); unlink(SERVER_FIFO_NAME); exit(EXIT_SUCCESS);}
client.c
#include "pipelib.h"#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){ int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_name[NAME_SIZE]; message msg; msg.client_pid = getpid(); sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid);/*将客户进程的pid写入结构体中*/ if ( mkfifo(client_fifo_name, 0777) == -1) {/*创建向服务器发送消息的管道文件*/ fprintf(stderr, "Sorry,create client fifo failure!\n" ); return -1; } server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);/*以写的方式打开服务器通道*/ if (server_fifo_fd == -1) { fprintf(stderr, "Sorry ,open server fifo failure!\n" ); return -1; } sprintf(msg.data, "Hello from: %d",msg.client_pid);/*向服务器进程发送Hello from PID信息*/ printf("%d sent %s\n",msg.client_pid, msg.data ); write(server_fifo_fd, &msg, sizeof(msg)); client_fifo_fd = open(client_fifo_name, O_RDONLY);/*以读的方式打开客户端通道*/ if (client_fifo_fd == -1) { fprintf(stderr, "Sorry ,client fifo open failure!\n"); return -1; } res = read(client_fifo_fd, &msg, sizeof(msg));/*从服务器接收数据*/ if (res > 0) { printf("received:%s\n", msg.data); } close(client_fifo_fd); close(server_fifo_fd); unlink(client_fifo_name); return 0;}
pipelib.h
#include <string.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#define SERVER_FIFO_NAME "/web/liefyuan/study/android/server_fifo"#define CLIENT_FIFO_NAME "/web/liefyuan/study/android/client_fifo"#define BUFFER_SIZE PIPE_BUF#define MESSAGE_SIZE 256#define NAME_SIZE 256typedef struct message{ pid_t client_pid; char data[MESSAGE_SIZE + 1];}message;
0 0
- Linux--进程间通信(二)-命名管道(pipe)通信
- Linux--进程间通信(一)-管道(pipe)通信
- Linux进程通信-管道(pipe)
- linux进程间通信(命名管道)
- (二十六)进程间通信——pipe管道
- 进程间通信:管道(pipe)
- 进程间通信--无名管道(pipe)
- linux进程间通信之管道(pipe与fifo)
- 深入理解Linux进程间通信(IPC)-- 管道pipe
- 【Linux系统编程】进程间通信--无名管道(pipe)
- Linux环境进程间通信(一)——管道(pipe)和命名管道(fifo)
- Linux环境进程间通信(一)——管道(pipe)和命名管道(fifo)
- linux进程间通信(一)管道和命名管道
- linux管道通信(pipe)
- Linux进程间通信(二)管道
- Linux进程间通信之管道(pipe)
- Linux环境进程间通信 --- 管道Pipe
- Linux 进程间管道pipe通信
- 记住密码的用户登录
- lsattr和chattr用法
- Vue.js2.0环境配置到你第一个叼大的App!
- Java的equal和==的区别
- Javascript <script>标签详解
- Linux--进程间通信(二)-命名管道(pipe)通信
- C++工厂方法模式
- 使用HttpURLConnection需要注意的地方
- 关于Android中获取短信息会话(threads)表中的信息
- Java集合框架图
- STM32之外部中断 EXTI
- robot framework之接口测试(get方式)
- sql优化
- sql server中replace()函数用法解析