Linux进程共享通信 -- mmap实现
生活随笔
收集整理的這篇文章主要介紹了
Linux进程共享通信 -- mmap实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://blog.csdn.net/y396397735/article/details/50651633
?
使用mmap內(nèi)存映射實現(xiàn)一端寫,另一端讀的進(jìn)程間通信
寫端代碼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> /*映射內(nèi)存大小*/ #define MAPLEN 0x100 /*定義一個學(xué)生信息結(jié)構(gòu)體*/ struct STU { int id; char name[20]; char sex; }; /*出錯信息統(tǒng)一處理函數(shù)*/ void sys_err(char *str, int exitno) { perror(str); exit(exitno); } int main(int argc, char*argv[]) { struct STU *pm;//STU結(jié)構(gòu)體指針 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){//文件偏移至分配的內(nèi)存地址末端 sys_err("lseek", 3); } if(write(fd, "\0", 1) < 0){ //末端賦值為'\0' sys_err("write", 4); } /*將文件映射至進(jìn)程的地址空間*/ pm = mmap(NULL, MAPLEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pm == MAP_FAILED){ sys_err("mmap", 2); } /*關(guān)閉文件描述符*/ close(fd); /*對文件進(jìn)行寫入操作*/ 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); } /*關(guān)閉文件*/ close(fd); /*刪除文件*/ unlink(argv[1]); /*在內(nèi)存中讀數(shù)據(jù)*/ 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
執(zhí)行過程:
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
此時執(zhí)行寫操作
yu@ubuntu:~/Linux/211/tongxin$ ./write myfile //在向myfile文件中寫數(shù)據(jù)- 1
- 2
另開一終端到當(dāng)前目錄,執(zhí)行如下讀操作:
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//讀取寫入的內(nèi)容Ctrl+C退出- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
退出后,執(zhí)行l(wèi)s,可發(fā)現(xiàn)myfile文件已刪除
yu@ubuntu:~/Linux/211/tongxin$ ls read read.c write write.c- 1
- 2
轉(zhuǎn)載于:https://www.cnblogs.com/diegodu/p/9262314.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Linux进程共享通信 -- mmap实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】大叔问题定位分享(12)Spar
- 下一篇: Luogu P1122 最大子树和 树形