linux 共享記憶體 總結
共享記憶體可以說是最有用的程序間通訊方式,也是最快的IPC形式。兩個不同程序A、B共享記憶體的意思是,同一塊實體記憶體被對映到程序A、B各自的程序地址空間。程序A可以即時看到程序B對共享記憶體中資料的更新,反之亦然。由於多個程序共享同一塊記憶體區域,必然需要某種同步機制,互斥鎖和訊號量都可以。採用共享記憶體通訊的一個顯而易見的好處是效率高,因為程序可以直接讀寫記憶體,而不需要任何資料的拷貝。因此,採用共享記憶體的通訊方式效率是非常高的。
【應用場景】
1. 程序間通訊-生產者消費者模式
生產者程序和消費者程序通訊常使用共享記憶體,比如一個網路伺服器,接入程序收到資料包後,直接寫到共享記憶體中,並喚醒處理程序,處理程序從共享記憶體中讀資料包,進行處理。當然,這裡要解決互斥的問題。
2. 父子程序間通訊
由於fork產生的子程序和父程序不共享記憶體區,所以父子程序間的通訊也可以使用共享記憶體,以POSIX共享記憶體為例,父程序啟動後使用MAP_SHARED建立記憶體對映,並返回指標ptr。fork結束後,子程序也會有指標ptr的拷貝,並指向同一個檔案對映。這樣父、子程序便共享了ptr指向的記憶體區。
3. 程序間共享-只讀模式
業務經常碰到一種場景,程序需要載入一份配置檔案,可能這個檔案有100K大,那如果這臺機器上多個程序都要載入這份配置檔案時,比如有200個程序,那記憶體開銷合計為20M,但如果檔案更多或者程序數更多時,這種對記憶體的消耗就是一種嚴重的浪費。比較好的解決辦法是,由一個程序負責把配置檔案載入到共享記憶體中,然後所有需要這份配置的程序只要使用這個共享記憶體即可。
【共享記憶體分類】
1. POSIX共享記憶體物件
const char shmfile[] = "/tmp";
const int size = 100;
shm_open建立一個名稱為tmp,大小為100位元組的共享記憶體區物件後,在/dev/shm/下可以看到對應的檔案,cat可以看到內容。
root:/home/#ls -al /dev/shm/tmp
-rw------- 1 root root 100 10-15 13:37 /dev/shm/tmp
訪問速度:非常快,因為 /dev/shm 是tmpfs的檔案系統, 可以看成是直接對記憶體操作的,速度當然是非常快的。
持續性:隨核心,即程序重啟共享記憶體中資料不會丟失,核心自舉或顯示呼叫shm_unlink或rm掉檔案刪除後丟失。
2. POSIX記憶體對映檔案
const char shmfile[] = "./tmp.shm";
const int size = 100;
open在指定目錄下建立指定名稱後文件,cat可以看到內容。
root:/home/#ls -al ./tmp.shm
-rw------- 1 root root 100 10-15 13:42 tmp.shm
訪問速度:慢於記憶體區物件,因為核心為同步或非同步更新到檔案系統中,而記憶體區物件是直接操作記憶體的。
持續性:隨檔案,即程序重啟或核心自舉不後丟失,除失顯示rm掉檔案後丟失。
3. SYSTEM V共享記憶體
共享記憶體建立後,執行ipcs命令,會打印出相應的資訊,比如下面所示,key為申請時分配的,可以執行ipcrm -M 0x12345678 刪除,nattch欄位為1表示有一個程序掛載了該記憶體。
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x12345678 32769 root 644 10 1
訪問速度:非常快,可以理解為全記憶體操作。
持續性: 隨核心,即程序重啟共享記憶體中資料不會丟失,核心自舉或顯示呼叫shmdt或使用ipcrm刪除後丟失。
與POSIX V共享記憶體區物件不同的是,SYSTEM V的共享記憶體區物件的大小是在呼叫shmget建立時固定下來的,而POSIX共享記憶體區大小可以在任何時刻通過ftruncate修改。
【程式碼示例】
下面給出三種共享記憶體使用方法的示例程式碼,都採用父子程序間通訊,並未考慮互斥,僅做示例供大家參考。
1.POSIX共享記憶體物件
[cpp:nogutter] view plaincopy- /*
- * Posix shared memory is easy to use in Linux 2.6, in this program, we
- * shared a memory between parent process and child process, stored several
- * objects of struct namelist in it. We store number of items in ptr[0].
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #define FILE_MODE (S_IRUSR | S_IWUSR)
- const char shmfile[] = "/tmp";
- const int size = 100;
- struct namelist
- {
- int id;
- char name[20];
- };
- int
- main(void)
- {
- int fd, pid, status;
- int *ptr;
- struct stat stat;
- // create a Posix shared memory
- int flags = O_RDWR | O_CREAT;
- fd = shm_open(shmfile, flags, FILE_MODE);
- if (fd < 0) {
- printf("shm_open failed, errormsg=%s errno=%d", strerror(errno), errno);
- return 0;
- }
- ftruncate(fd, size);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- pid = fork();
- if (pid == 0) { // child process
- printf("Child %d: start/n", getpid());
- fd = shm_open(shmfile, flags, FILE_MODE);
- fstat(fd, &stat);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- close(fd);
- struct namelist tmp;
- // store total num in ptr[0];
- *ptr = 3;
- ptr++;
- namelist *cur = (namelist *)ptr;
- // store items
- tmp.id = 1;
- strcpy(tmp.name, "Nellson");
- *cur++ = tmp;
- tmp.id = 2;
- strcpy(tmp.name, "Daisy");
- *cur++ = tmp;
- tmp.id = 3;
- strcpy(tmp.name, "Robbie");
- *cur++ = tmp;
- exit(0);
- } else{ // parent process
- sleep(1);
- struct namelist tmp;
- int total = *ptr;
- printf("/nThere is %d item in the shm/n", total);
- ptr++;
- namelist *cur = (namelist *)ptr;
- for (int i = 0; i< total; i++) {
- tmp = *cur;
- printf("%d: %s/n", tmp.id, tmp.name);
- cur++;
- }
- printf("/n");
- waitpid(pid, &status, 0);
- }
- // remvoe a Posix shared memory from system
- printf("Parent %d get child status:%d/n", getpid(), status);
- return 0;
- }
編譯執行
root:/home/ftpuser/ipc#g++ -o shm_posix -lrt shm_posix.cc
root:/home/ftpuser/ipc#./shm_posix
Child 2280: start
There is 3 item in the shm
1: Nellson
2: Daisy
3: Robbie
Parent 2279 get child status:0
2.POSIX檔案對映
[cpp:nogutter] view plaincopy- /*
- * Posix shared memory is easy to use in Linux 2.6, in this program, we
- * shared a memory between parent process and child process, stored several
- * objects of struct namelist in it. We store number of items in ptr[0].
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define FILE_MODE (S_IRUSR | S_IWUSR)
- const char shmfile[] = "./tmp.shm";
- const int size = 100;
- struct namelist
- {
- int id;
- char name[20];
- };
- int main(void)
- {
- int fd, pid, status;
- int *ptr;
- struct stat stat;
- // create a Posix shared memory
- int flags = O_RDWR | O_CREAT;
- fd = open(shmfile, flags, FILE_MODE);
- ftruncate(fd, size);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- pid = fork();
- if (pid == 0) { // child process
- printf("Child %d: start/n", getpid());
- fd = open(shmfile, flags, FILE_MODE);
- fstat(fd, &stat);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- close(fd);
- struct namelist tmp;
- // store total num in ptr[0];
- *ptr = 3;
- ptr++;
- namelist *cur = (namelist *)ptr;
- // store items
- tmp.id = 1;
- strcpy(tmp.name, "Nellson");
- *cur++ = tmp;
- tmp.id = 2;
- strcpy(tmp.name, "Daisy");
- *cur++ = tmp;
- tmp.id = 3;
- strcpy(tmp.name, "Robbie");
- *cur++ = tmp;
- exit(0);
- } else{ // parent process
- sleep(1);
- struct namelist tmp;
- int total = *ptr;
- printf("/nThere is %d item in the shm/n", total);
- ptr++;
- namelist *cur = (namelist *)ptr;
- for (int i = 0; i< total; i++) {
- tmp = *cur;
- printf("%d: %s/n", tmp.id, tmp.name);
- cur++;
- }
- printf("/n");
- waitpid(pid, &status, 0);
- }
- printf("Parent %d get child status:%d/n", getpid(), status);
- return 0;
- }
編譯執行
root:/home/ftpuser/ipc#g++ -o map_posix map_posix.cc
root:/home/ftpuser/ipc#./map_posix
Child 2300: start
There is 3 item in the shm
1: Nellson
2: Daisy
3: Robbie
Parent 2299 get child status:0
3.SYSTEM V 共享記憶體物件
[cpp:nogutter] view plaincopy- /*
- * System V shared memory in easy to use in Linux 2.6, in this program, we
- * shared a memory between parent process and child process, stored several
- * objects of struct namelist in it. We store number of items in ptr[0].
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #define SVSHM_MODE (SHM_R | SHM_W | SHM_R>>3 | SHM_R>>6)
- const char shmfile[] = "./tmp.shm";
- const int shmsize = 10;
- struct namelist
- {
- int id;
- char name[20];
- };
- int
- main(void)
- {
- int shmid, pid, status;
- int *ptr;
- struct shmid_ds buff;
- // create a systym V shared memory
- //shmid = shmget(ftok(shmfile, 0), shmsize, SVSHM_MODE | IPC_CREAT);
- shmid = shmget((key_t)0x12345678, shmsize, SVSHM_MODE | IPC_CREAT);
- pid = fork();
- if (pid == 0) { // child process
- printf("Child %d: start/n", getpid());
- //shmid = shmget(ftok(shmfile, 0), shmsize, SVSHM_MODE | IPC_CREAT);
- shmid = shmget((key_t)0x12345678, shmsize, SVSHM_MODE | IPC_CREAT);
- ptr = (int *) shmat(shmid, NULL, 0);
- shmctl(shmid, IPC_STAT, &buff);
- struct namelist tmp;
- // store total num in ptr[0];
- *ptr = 3;
- ptr++;
- namelist *cur = (namelist *)ptr;
- // store items
- tmp.id = 1;
- strcpy(tmp.name, "Nellson");
- *cur++ = tmp;
- tmp.id = 2;
- strcpy(tmp.name, "Daisy");
- *cur++ = tmp;
- tmp.id = 3;
- strcpy(tmp.name, "Robbie");
- *cur++ = tmp;
- exit(0);
- } else{ // parent process
- sleep(1);
- shmctl(shmid, IPC_STAT, &buff);
- ptr = (int *) shmat(shmid, NULL, 0);
- struct namelist tmp;
- int total = *ptr;
- printf("/nThere is %d item in the shm/n", total);
- ptr++;
- namelist *cur = (namelist *)ptr;
- for (int i = 0; i< total; i++) {
- tmp = *cur;
- printf("%d: %s/n", tmp.id, tmp.name);
- cur++;
- }
- printf("/n");
- waitpid(pid, &status, 0);
- }
- // remvoe a systym V shared memory from system
- shmctl(shmid, IPC_RMID, NULL);
- printf("Parent %d get child status:%d/n", getpid(), status);
- return 0;
- }
編譯執行
root:/home/ftpuser/ipc#g++ -o shm_v shm_v.cc
root:/home/ftpuser/ipc#./shm_v
Child 2323: start
There is 3 item in the shm
1: Nellson
2: Daisy
3: Robbie
Parent 2322 get child status:0
【效能測試】
下面對三種方式進行效能測試,比較下差異。
測試機資訊:
AMD Athlon(tm) Neo X2 Dual Core Processor 6850e
cpu:1.7G
os: Linux 2.6.18
測試方式:
開啟大小為SIZE的共享記憶體,對映到一個int型的陣列中,迴圈寫陣列、讀陣列。
重複10W次,計算時間開銷。
記憶體大小 |
Shmopen+mmap(ms) |
Open+mmap |
Shmget |
4k |
1504 |
1470 |
1507 |
16k |
6616 |
6201 |
5994 |
64k |
25905 |
24391 |
24315 |
256k |
87487 |
76981 |
69417 |
1M |
253209 |
263431 |
241886 |
重複1K次,計算時間開銷。
記憶體大小 |
Shmopen+mmap(ms) |
Open+mmap(ms) |
Shmget(ms) |
1M |
5458 |
5447 |
5404 |
4M |
21492 |
21447 |
21307 |
16M |
90880 |
93685 |
87594 |
32M |
178000 |
214900 |
193000 |
分析:
Sytem V方式讀寫速度快於POSIX方式,而POSIX 共享記憶體和檔案對映方式相差不大, 共享記憶體效能略優。
附上測試原始碼:
[cpp:nogutter] view plaincopy- /*
- * 共享記憶體讀寫速度測試
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include "module_call.h"
- #define FILE_MODE (S_IRUSR | S_IWUSR)
- #define SVSHM_MODE (SHM_R | SHM_W | SHM_R>>3 | SHM_R>>6)
- enum emType
- {
- SHMOPEN = 0x01,
- OPEN = 0x02,
- SHMGET = 0x04,
- };
- void * GetShmMem(emType type, int size)
- {
- void * ptr = NULL;
- switch (type)
- {
- case SHMOPEN:
- {
- const char shmfile[] = "/tmp";
- int flags = O_RDWR | O_CREAT;
- int fd = shm_open(shmfile, flags, FILE_MODE);
- if (fd < 0)
- {
- printf("shm_open failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- ftruncate(fd, size);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (MAP_FAILED == ptr)
- {
- printf("mmap failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- break;
- }
- case OPEN:
- {
- const char shmfile[] = "./tmp.shm";
- int flags = O_RDWR | O_CREAT;
- int fd = open(shmfile, flags, FILE_MODE);
- if (fd < 0)
- {
- printf("ope failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- ftruncate(fd, size);
- ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (MAP_FAILED == ptr)
- {
- printf("mmap failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- break;
- }
- case SHMGET:
- {
- int shmid;
- struct shmid_ds buff;
- const char shmfile[] = "./tmp.shm_v";
- shmid = shmget(ftok(shmfile, 0), size, SVSHM_MODE | IPC_CREAT);
- if (shmid < 0)
- {
- printf("shmget failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- ptr = (int *) shmat(shmid, NULL, 0);
- if ((void *) -1 == ptr)
- {
- printf("shmat failed, errormsg=%s errno=%d/n", strerror(errno), errno);
- return NULL;
- }
- shmctl(shmid, IPC_STAT, &buff);
- break;
- }
- }
- return ptr;
- }
- int realmain(int size, int loop, emType type)
- {
- int * array_int = NULL;
- /* get shmmem*/
- array_int = (int *)GetShmMem(type, size);
- if (NULL == array_int)
- {
- printf("GetShmMem failed/n");
- return -1;
- }
- /* loop */
- int array_num = size/sizeof(int);
- modulecall::call_start();
- while (0 != loop)
- {
- /* write */
- for (int i = 0; i < array_num; i++)
- {
- array_int[i] = i;
- }
- /* read */
- for (int i = 0; i < array_num; i++)
- {
- if (array_int[i] != i)
- {
- printf("ShmMem is invalid i=%d v=%d/n", i, array_int[i]);
- return -1;
- }
- }
- loop--;
- }
- modulecall::call_end();
- printf("timecost=%lld/n", modulecall::call_timecost());
- return 0;
- }
- int main(int argc, char ** argv)
- {
- if (argc < 4)
- {
- printf("usage: %s size loop shmtype(1-shmposix 2-mapposix 4-shmv 7-all)/n", argv[0]);
- return -1;
- }
- const int size = atoi(argv[1]);
- int loop = atoi(argv[2]);
- const int type = atoi(argv[3]);
- if ((type&SHMOPEN) == SHMOPEN)
- {
- printf("shmopen ");
- realmain(size, loop, SHMOPEN);
- }
- if ((type&OPEN) == OPEN)
- {
- printf("open ");
- realmain(size, loop, OPEN);
- }
- if ((type&SHMGET) == SHMGET)
- {
- printf("shmget ");
- realmain(size, loop, SHMGET);
- }
- return 0;
- }
相關推薦
linux 共享記憶體 總結
共享記憶體可以說是最有用的程序間通訊方式,也是最快的IPC形式。兩個不同程序A、B共享記憶體的意思是,同一塊實體記憶體被對映到程序A、B各自的程序地址空間。程序A可以即時看到程序B對共享記憶體中資料的更新,反之亦然。由於多個程序共享同一塊記憶體區域,必然需要某種同步機制,
Linux:共享記憶體
概念: 共享記憶體區是最快的IPC形式。⼀一旦這樣的記憶體對映到共享它的程序的地址空間,這些程序間資料傳遞不再 涉及到核心,換句話說是程序不再通過執⾏行進⼊入核心的系統調⽤用來傳遞彼此的資料。 共享記憶體中的函式: shmget函式: 功能:⽤用來建立共享記
程序間通訊之Linux共享記憶體程式設計
共享記憶體 共享記憶體(Shared Memory)是指多個程序共享一段指定的記憶體空間進行資料互動,三種System V IPC機制(另外兩種是訊號量和訊息佇列)中共享記憶體是速度最快的一種。 共享記憶體機制是最快的一種程序間通訊(Interprocess Commun
Linux 程式設計1:深入淺出 Linux 共享記憶體
筆者最近在閱讀Aerospike 論文時,發現了Aerospike是利用了Linux 共享記憶體機制來實現的儲存索引快速重建的。這種方式比傳統利用索引檔案進行快速重啟的方式大大提高了效率。(減少了磁碟 i/o,但是缺點是耗費記憶體,並且伺服器一旦重啟之後就只能冷重啟了~~)而目前筆者工作之中維護的 No
(轉)Linux共享記憶體使用常見陷阱與分析(4)-共享記憶體刪除的陷阱
轉自http://os.51cto.com/art/201311/418977_3.htm 共享記憶體刪除的陷阱? 當程序結束使用共享記憶體區時,要通過函式 shmdt 斷開與共享記憶體區的連線。該函式宣告在 sys/shm.h 中,其原型如下: #include #
(轉)Linux共享記憶體使用常見陷阱與分析(3)-ftok是否一定會產生唯一的key值
轉自http://os.51cto.com/art/201311/418977_2.htm ftok是否一定會產生唯一的key值? 系統建立IPC通訊(如訊息佇列、共享記憶體時)必須指定一個ID值。通常情況下,該id值通過ftok函式得到。 ftok原型如下: ke
Linux 共享記憶體 .
一、概念 共享記憶體是被多個程序共享的一部分實體記憶體,是程序間共享資料的最快的一種方法。 二、實現 分為兩個步驟: 1、建立共享記憶體。 2、對映共享記憶體。 1、建立 int shmget(key_t key, int size, int shmflg
linux 共享記憶體(shmget,shmat,shmdt,shmctl)解析
shmget int shmget(key_t key, size_t size, int shmflg); key: 識別符號的規則 size: 共享儲存段的位元組數 flag: 讀寫的許可權還有IPC_CREAT或IPC_EXCL對應檔案的O_CREAT或O_EXCL 返回值:
Linux共享記憶體與互斥鎖
Linux共享記憶體 共享記憶體是從系統的空閒記憶體池中分配,並希望訪問它的每個程序都能連線它。連線的過程稱為對映。對映後,每個程序都可通過訪問自己的記憶體而訪問共享記憶體區域,進而與其它程序進行通訊。 共享記憶體相關函式 開啟建立共享記憶體檔案 int shm_op
Linux共享記憶體物件,shm_open,mmap
Linux共享記憶體物件 概述 linux下,每個程序都有自己擁有的記憶體區域,程序的記憶體總是私有的。共享記憶體是從系統的空閒記憶體池中分配的,希望訪問它的每個程序連線它。這個連線過程稱為對映。對映後,每個程序都可通過訪問自己的記憶體而訪問共享記憶體區域,
修改linux共享記憶體大小
這是實際linux系統顯示的實際資料: [email protected]:/proc/sys/kernel$ cat shmmax 33554432 [email protected]:/proc/sys/kernel$ cat shmmni
Linux共享記憶體和cache
申請以下共享記憶體,不對其進行初始化,共享記憶體大小為56600032bytes: 此時free: total used free shared buffers cachedMem: 3932
Linux-程序通訊-訊息佇列/訊號燈/共享記憶體
訊息佇列 訊息佇列提供了程序間傳送資料塊的方法,每個資料塊都可以被認為是有一個型別,接受者接受的資料塊可以有不同的型別;我們可以通過傳送訊息來避免命名管道的同步和阻塞問題;訊息佇列與命名管道一樣,每個資料塊都有一個最大長度的限制;我們可以將每個資料塊當作是一
c/c++ linux 程序間通訊系列4,使用共享記憶體
linux 程序間通訊系列4,使用共享記憶體 1,建立共享記憶體,用到的函式shmget, shmat, shmdt 函式名 功能描述 shmget 建立共享記憶體,返回pic key
Linux:程序間通訊(匿名管道命名管道)(共享記憶體,訊息佇列,訊號量)
目錄 程序間通訊的介紹 管道 匿名管道 原理: 程式碼實現 匿名管道特性 實現管道符 | 命名管道 命名管道特性 程式碼實現 管道讀寫規則 作業系統中ipc的相關命令 共享記憶體(重點) 生命週期: 程式碼實現 程式碼實現獲
Linux(高階程式設計)8————程序間通訊4(共享記憶體)
共享記憶體是什麼? 因為程序之間是相互獨立的,他們有各自程序地址空間,那麼他們需要通訊時就要藉助核心來為他們建立橋樑,像之前我們瞭解的管道、訊息佇列就是核心做的工作來為程序間通訊架的橋樑。共享記憶體也是核心為程序間通訊駕的一座橋樑,只不過這座橋樑比其他橋樑更優,共享記憶體是核心為需要通訊
Linux系統程式設計—共享記憶體之mmap
共享記憶體概念 共享記憶體是通訊效率最高的IPC方式,因為程序可以直接讀寫記憶體,而無需進行資料的拷備。但是它沒有自帶同步機制,需要配合訊號量等方式來進行同步。 共享記憶體被建立以後,同一塊實體記憶體被對映到了多個程序地址空間,當有一個程序修改了共享記憶體的資料,其餘的程序均可看見所修改的內容,反之亦然。
Linux 通過共享記憶體機制實現程序間通訊
問題背景 編寫程式 sender ,它建立一個共享記憶體,然後等待使用者通過終端輸入一串字元,並將這串字元通過共享記憶體傳送給 receiver;最後,它等待 receiver 的應答,收到應答訊息後,將接收到的應答資訊顯示在終端螢幕上,刪除共享記憶體,結束程式的執行。 編寫 receiver 程
Linux 下共享記憶體實現
c程式實現 寫記憶體 讀記憶體 執行結果 總結 c程式實現 寫記憶體 /* * rShareM.c * * Created on: 2011-11-20 * Author: snape */ #include &
Linux關於程序間通訊共享記憶體
共享記憶體概念 共享記憶體允許兩個不相關的程序去訪問同一部分邏輯記憶體 如果需要在兩個執行中的程序之間傳輸資料,共享記憶體將是一種效率極高的解決方案 共享記憶體是由IPC為一個程序建立的一個特殊的地址範圍,它將出現在程序的地址空間中。 其他程序可以把同一段共享記憶體段“連