日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux进程共享通信 -- mmap实现

發布時間:2025/3/15 linux 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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://blog.csdn.net/y396397735/article/details/50651633

轉載于:https://www.cnblogs.com/diegodu/p/9262314.html

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的Linux进程共享通信 -- mmap实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。