1. 程式人生 > >進程間通信:共享內存

進程間通信:共享內存

== mmap blank targe 進程間通信 mman types proc perror

任務:主進程打開一段共享內存,fork出8個子進程分別將共享內存map到各自虛擬地址空間中,每個子進程都需要將共享內存中的一個數值加一。

參考文檔:

http://man7.org/linux/man-pages/man3/shm_open.3.html
http://man7.org/linux/man-pages/man2/mmap.2.html
http://man7.org/linux/man-pages/man7/inode.7.html
http://man7.org/linux/man-pages/man2/ftruncate.2.html

代碼實現:

#include <stdio.h>
#include 
<fcntl.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #define MAPPING_SIZE 4096 int main (int argc,char* argv[]){ int mapfd; const char* mapname = "/number"; mapfd = shm_open(mapname,O_RDWR|O_CREAT,S_IRUSR | S_IWUSR);
if(mapfd == -1){ perror("shm_open fail"); exit(EXIT_FAILURE); } // ftruncate可以用來設置共享內存到指定大小 if(ftruncate(mapfd,MAPPING_SIZE) == -1){ perror("ftruncate fail"); close(mapfd); exit(EXIT_FAILURE); } int stat,cid,n,procCount=0,maxProcCount=8
; for(n = 0;n<maxProcCount;++n){ cid = fork(); if (cid == -1){ perror("fork fail"); continue; } if(cid == 0){ void* sp = mmap(NULL,MAPPING_SIZE,PROT_READ | PROT_WRITE,MAP_SHARED,mapfd,0); if (sp == NULL){ perror("mmap fail"); close(mapfd); _exit(EXIT_FAILURE); } int * num = (int*)sp; (*num)++; printf("Process %d: %d\n",getpid(),*num); if(munmap(sp,MAPPING_SIZE) == -1){ perror("munmap fail"); } close(mapfd); _exit(EXIT_SUCCESS); } ++procCount; } while(procCount--){ cid = wait(&stat); if(cid == -1){ perror("wait fail"); break; } printf("%d cid %d exit.\n",procCount,cid); } close(mapfd); // 如果不執行shm_unlink則多次執行程序的輸出是遞增的,共享內存被重復利用了 shm_unlink(mapname); }

執行結果:

[root@centos7 c]# gcc process.c -lrt -o proc
[root@centos7 c]# ./proc Process 5941: 1 Process 5942: 2 Process 5943: 3 Process 5944: 4 Process 5946: 5 Process 5947: 6 Process 5945: 7 7 cid 5941 exit. 6 cid 5942 exit. 5 cid 5943 exit. 4 cid 5944 exit. 3 cid 5946 exit. 2 cid 5947 exit. 1 cid 5945 exit. Process 5948: 8 0 cid 5948 exit.

進程間通信:共享內存