Linux中的进程间通信
来源:互联网 发布:健身 年轻 知乎 编辑:程序博客网 时间:2024/05/16 11:11
在Linux中进程间通信有很多种方法,可以使用普通的文件,或者命名管道,以及共享内存都可以实现进程间通信,下面是几种实现方式的代码。
一. 使用普通文件实现进程间通信
服务端实现:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>int main(int argc, char *argv[]){ int fd; char message[20] = "hello world!"; if (argc != 2){ printf("error"); exit(1); } if ((fd = open(argv[1], O_WRONLY|O_CREAT, 0644)) == -1){ perror("open"); exit(1); } while (1){ write(fd, message, strlen(message)); sleep(1); } return 0;}
客户端实现:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>int main(int argc, char *argv[]){ int fd, len; char message[20] = "hello world!"; if (argc != 2){ printf("error"); exit(1); } if ((fd = open(argv[1], O_RDONLY)) == -1){ perror("open"); exit(1); } while ((len = read(fd, message, 20)) > 0){ printf("%s\n", message); sleep(1); } close(fd); return 0;}
二. 使用管道实现
服务端代码:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <stat.h>#include <string.h>int main(int argc, char *argv[]){ int fd; char message[20] = "hello world!"; if (argc != 2){ printf("error"); exit(1); } if ((fd = mkfifo(argv[1], O_CREAT, 0644)) == -1){ perror("mkfifo"); exit(1); } if ((fd = open(argv[1], O_WRONLY)) == -1){ perror("open"); exit(1); } while (1){ write(fd, message, strlen(message)); sleep(1); } return 0;}
客户端代码:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>int main(int argc, char *argv[]){ int fd, len; char message[20] = "hello world!"; if (argc != 2){ printf("error"); exit(1); } if ((fd = open(argv[1], O_RDONLY)) == -1){ perror("open"); exit(1); } while ((len = read(fd, message, 20)) > 0){ printf("%s\n", message); sleep(1); } close(fd); return 0;}
三. 共享内存实现
服务端代码:
#include <stdio.h>#include <stdlib.h>#include <shm.h>#include <string.h>#define SHM_KEY 99int main(void){ int shmid; char *shm_ptr, message[20] = "hello world!"; //创建共享内存,参数分别是共享内存的键值,尺寸,以及权限 if ((shmid = shmget(SHM_KEY, 100, IPC_CREAT|0777)) == -1){ perror("shmget"); exit(1); } /*将共享内存附加到进程的地址空间,与进程建立联系,返回共享内存区的指针,第一个参数为共享内存的id,第二个参数为NULL表示让内核分配共享内存区间,第三个为存取标志位,为0表示从0号位置开始存取*/ if ((shm_ptr = shmat(shmid, NULL, 0)) == NULL){ perror("shmat"); exit(1); } while (1){ //将信息拷贝到共享内存区 strcpy(shm_ptr, message); sleep(1); } //删除共享内存区 shmctl(shmid, IPC_RMID, NULL); return 0;}
客户端代码:
#include <stdio.h>#include <stdlib.h>#include <shm.h>#include <string.h>//此key和服务端相同,不然不能代表同一块内存区域#define SHM_KEY 99int main(void){ int shmid; char *shm_ptr; if ((shmid = shmget(SHM_KEY, 100, IPC_STAT)) == -1){ perror("shmget"); exit(1); } if ((shm_ptr = shmat(shmid, NULL, 0)) == NULL){ perror("shmat"); exit(1); } printf("%s\n", shm_ptr); //断开与共享内存区的联系 shmdt(shm_ptr); return 0;}
以上这三种为最基本的通信方式,希望对需要的同学们有帮助。
阅读全文
0 0
- Linux中的进程间通信
- Linux中的进程间通信
- Linux系统中的进程、线程间通信
- linux进程中的通信方式
- Linux进程间通信
- Linux进程间通信
- Linux进程间通信
- Linux进程间通信
- linux 进程间通信
- linux 进程间通信
- Linux进程间通信
- Linux进程间通信
- 【Linux】 进程间通信
- linux进程间通信
- Linux 进程间通信
- Linux进程间通信
- Linux进程间通信
- Linux 进程间通信
- MySQL+Python3.5+Django+Ubuntu
- 深度学习笔记-客流预测模型-20170627
- svn安装以及插件连接Myeclipse
- 九度1084:整数拆分
- 【转】JVM调优总结(一)-一些概念
- Linux中的进程间通信
- 解决R语言xlsx安装遇到问题
- 我大声地告诉您,一卡通的终极目标就是无卡通
- 《权限系列shiro+cas》----配置各个应用程序
- POJ 2104 . K-th Number
- 使用导航控件
- hadoop 2.2.X 配置参数说明:mapred-site.xml
- POJ1155-树形DP&&背包
- 【转】JVM调优总结(二)-一些概念