Linux进程共享通信 -- mmap实现
生活随笔
收集整理的這篇文章主要介紹了
Linux进程共享通信 -- mmap实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://blog.csdn.net/y396397735/article/details/50651633
?
使用mmap內存映射實現一端寫,另一端讀的進程間通信
寫端代碼write.c
/*write.c*/ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> /*映射內存大小*/ #define MAPLEN 0x100 /*定義一個學生信息結構體*/ struct STU { int id; char name[20]; char sex; }; /*出錯信息統一處理函數*/ void sys_err(char *str, int exitno) { perror(str); exit(exitno); } int main(int argc, char*argv[]) { struct STU *pm;//STU結構體指針 int fd, i = 0; if(argc < 2){ printf("args error\n"); exit(1); } fd = open(argv[1], O_RDWR | O_CREAT, 0777); //打開一文件 if(fd < 0){ sys_err("open", 1); } if(lseek(fd, MAPLEN - 1, SEEK_SET) < 0){//文件偏移至分配的內存地址末端 sys_err("lseek", 3); } if(write(fd, "\0", 1) < 0){ //末端賦值為'\0' sys_err("write", 4); } /*將文件映射至進程的地址空間*/ pm = mmap(NULL, MAPLEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pm == MAP_FAILED){ sys_err("mmap", 2); } /*關閉文件描述符*/ close(fd); /*對文件進行寫入操作*/ while(1){ pm->id = i; sprintf(pm->name, "yu-%d", i); if(i % 2 == 0){ pm->sex = 'm'; }else{ pm->sex = 'w'; } i++; sleep(1); } munmap(pm, MAPLEN); return 0; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
讀端代碼read.c
/*read.c*/ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #define MANLEN 0x1000 struct STU { int id; char name[20]; char sex; }; void sys_err(char *str, int exitno) { perror(str); exit(exitno); } int main(int argc, char *argv[]) { struct STU *pm; int fd, i = 0; if (argc < 2) { printf("args error\n"); exit(1); } fd = open(argv[1], O_RDWR); if (fd < 0){ sys_err("open", 1); } pm = mmap(NULL, MAPLEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pm == MAP_FAILED){ sys_err("mmap", 2); } /*關閉文件*/ close(fd); /*刪除文件*/ unlink(argv[1]); /*在內存中讀數據*/ while(1){ printf("%d\n", pm->id); printf("%s\n", pm->name); printf("%c\n", pm->sex); sleep(1); } munmap(pm, MAPLEN); return 0; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
執行過程:
yu@ubuntu:~/Linux/211/tongxin$ ls read.c write.c yu@ubuntu:~/Linux/211/tongxin$ gcc -o write write.c yu@ubuntu:~/Linux/211/tongxin$ gcc -o read read.c yu@ubuntu:~/Linux/211/tongxin$ ls read read.c write write.c- 1
- 2
- 3
- 4
- 5
- 6
此時執行寫操作
yu@ubuntu:~/Linux/211/tongxin$ ./write myfile //在向myfile文件中寫數據- 1
- 2
另開一終端到當前目錄,執行如下讀操作:
yu@ubuntu:~/Linux/211/tongxin$ ls read read.c write write.c myfile yu@ubuntu:~/Linux/211/tongxin$ ./read myfile 6 yu-6 m 7 yu-7 w ^C//讀取寫入的內容Ctrl+C退出- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
退出后,執行ls,可發現myfile文件已刪除
yu@ubuntu:~/Linux/211/tongxin$ ls read read.c write write.c- 1
- 2
轉載于:https://www.cnblogs.com/diegodu/p/9262314.html
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Linux进程共享通信 -- mmap实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】大叔问题定位分享(12)Spar
- 下一篇: Luogu P1122 最大子树和 树形