1. 程式人生 > >linux 共享記憶體 總結

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
  1. /* 
  2.  * Posix shared memory is easy to use in Linux 2.6, in this program, we  
  3.  * shared a memory between parent process and child process, stored several 
  4.  * objects of struct namelist in it. We store number of items in ptr[0]. 
  5.  */  
  6. #include <unistd.h>  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <sys/ipc.h>  
  11. #include <sys/mman.h>  
  12. #include <sys/types.h>  
  13. #include <sys/wait.h>  
  14. #include <sys/stat.h>  
  15. #include <fcntl.h>  
  16. #include <errno.h>  
  17. #define FILE_MODE (S_IRUSR | S_IWUSR)  
  18. const char shmfile[] = "/tmp";  
  19. const int size = 100;  
  20. struct namelist   
  21. {  
  22.  int  id;   
  23.  char name[20];  
  24. };  
  25. int   
  26. main(void)  
  27. {  
  28.  int fd, pid, status;   
  29.  int *ptr;  
  30.  struct stat stat;  
  31.  // create a Posix shared memory  
  32.  int flags = O_RDWR | O_CREAT;  
  33.  fd = shm_open(shmfile, flags, FILE_MODE);  
  34.     if (fd < 0) {  
  35.         printf("shm_open failed, errormsg=%s errno=%d", strerror(errno), errno);  
  36.         return 0;  
  37.     }  
  38.  ftruncate(fd, size);  
  39.  ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);   
  40.  pid = fork();  
  41.  if (pid == 0) { // child process  
  42.   printf("Child %d: start/n", getpid());  
  43.   fd = shm_open(shmfile, flags, FILE_MODE);  
  44.   fstat(fd, &stat);    
  45.   ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);   
  46.   close(fd);  
  47.   struct namelist tmp;  
  48.   // store total num in ptr[0];  
  49.   *ptr = 3;  
  50.   ptr++;  
  51.   namelist *cur = (namelist *)ptr;  
  52.   // store items  
  53.   tmp.id = 1;  
  54.   strcpy(tmp.name, "Nellson");  
  55.   *cur++ = tmp;  
  56.   tmp.id = 2;  
  57.   strcpy(tmp.name, "Daisy");  
  58.   *cur++ = tmp;  
  59.   tmp.id = 3;  
  60.   strcpy(tmp.name, "Robbie");  
  61.   *cur++ = tmp;  
  62.   exit(0);  
  63.  } else// parent process  
  64.   sleep(1);  
  65.   struct namelist tmp;  
  66.   int total = *ptr;  
  67.   printf("/nThere is %d item in the shm/n", total);   
  68.   ptr++;  
  69.   namelist *cur = (namelist *)ptr;  
  70.   for (int i = 0; i< total; i++) {  
  71.    tmp = *cur;  
  72.    printf("%d: %s/n", tmp.id, tmp.name);  
  73.    cur++;  
  74.   }  
  75.   printf("/n");  
  76.   waitpid(pid, &status, 0);  
  77.  }  
  78.  // remvoe a Posix shared memory from system  
  79.  printf("Parent %d get child status:%d/n", getpid(), status);  
  80.  return 0;  
  81. }  
  

編譯執行

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
  1. /* 
  2.  * Posix shared memory is easy to use in Linux 2.6, in this program, we  
  3.  * shared a memory between parent process and child process, stored several 
  4.  * objects of struct namelist in it. We store number of items in ptr[0]. 
  5.  */  
  6. #include <unistd.h>  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <sys/ipc.h>  
  11. #include <sys/mman.h>  
  12. #include <sys/types.h>  
  13. #include <sys/wait.h>  
  14. #include <sys/stat.h>  
  15. #include <fcntl.h>  
  16. #define FILE_MODE (S_IRUSR | S_IWUSR)  
  17. const char shmfile[] = "./tmp.shm";  
  18. const int size = 100;  
  19. struct namelist   
  20. {  
  21.  int  id;   
  22.  char name[20];  
  23. };  
  24. int main(void)  
  25. {  
  26.  int fd, pid, status;   
  27.  int *ptr;  
  28.  struct stat stat;  
  29.  // create a Posix shared memory  
  30.  int flags = O_RDWR | O_CREAT;  
  31.  fd = open(shmfile, flags, FILE_MODE);  
  32.  ftruncate(fd, size);  
  33.  ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);   
  34.  pid = fork();  
  35.  if (pid == 0) { // child process  
  36.   printf("Child %d: start/n", getpid());  
  37.   fd = open(shmfile, flags, FILE_MODE);  
  38.   fstat(fd, &stat);    
  39.   ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);   
  40.   close(fd);  
  41.   struct namelist tmp;  
  42.   // store total num in ptr[0];  
  43.   *ptr = 3;  
  44.   ptr++;  
  45.   namelist *cur = (namelist *)ptr;  
  46.   // store items  
  47.   tmp.id = 1;  
  48.   strcpy(tmp.name, "Nellson");  
  49.   *cur++ = tmp;  
  50.   tmp.id = 2;  
  51.   strcpy(tmp.name, "Daisy");  
  52.   *cur++ = tmp;  
  53.   tmp.id = 3;  
  54.   strcpy(tmp.name, "Robbie");  
  55.   *cur++ = tmp;  
  56.   exit(0);  
  57.  } else// parent process  
  58.   sleep(1);  
  59.   struct namelist tmp;  
  60.   int total = *ptr;  
  61.   printf("/nThere is %d item in the shm/n", total);   
  62.   ptr++;  
  63.   namelist *cur = (namelist *)ptr;  
  64.   for (int i = 0; i< total; i++) {  
  65.    tmp = *cur;  
  66.    printf("%d: %s/n", tmp.id, tmp.name);  
  67.    cur++;  
  68.   }  
  69.   printf("/n");  
  70.   waitpid(pid, &status, 0);  
  71.  }  
  72.  printf("Parent %d get child status:%d/n", getpid(), status);  
  73.  return 0;  
  74. }  

編譯執行

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
  1. /* 
  2.  * System V shared memory in easy to use in Linux 2.6, in this program, we  
  3.  * shared a memory between parent process and child process, stored several 
  4.  * objects of struct namelist in it. We store number of items in ptr[0]. 
  5.  */  
  6. #include <unistd.h>  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <sys/ipc.h>  
  11. #include <sys/shm.h>  
  12. #include <sys/types.h>  
  13. #include <sys/wait.h>  
  14. #define SVSHM_MODE (SHM_R | SHM_W | SHM_R>>3 | SHM_R>>6)  
  15. const char shmfile[] = "./tmp.shm";  
  16. const int shmsize = 10;  
  17. struct namelist   
  18. {  
  19.  int  id;   
  20.  char name[20];  
  21. };  
  22. int   
  23. main(void)  
  24. {  
  25.  int shmid, pid, status;   
  26.  int *ptr;  
  27.  struct shmid_ds buff;  
  28.  // create a systym V shared memory  
  29.  //shmid = shmget(ftok(shmfile, 0), shmsize, SVSHM_MODE | IPC_CREAT);  
  30.  shmid = shmget((key_t)0x12345678, shmsize, SVSHM_MODE | IPC_CREAT);  
  31.  pid = fork();  
  32.  if (pid == 0) { // child process  
  33.   printf("Child %d: start/n", getpid());  
  34.   //shmid = shmget(ftok(shmfile, 0), shmsize, SVSHM_MODE | IPC_CREAT);  
  35.   shmid = shmget((key_t)0x12345678, shmsize, SVSHM_MODE | IPC_CREAT);  
  36.   ptr = (int *) shmat(shmid, NULL, 0);  
  37.   shmctl(shmid, IPC_STAT, &buff);  
  38.   struct namelist tmp;  
  39.   // store total num in ptr[0];  
  40.   *ptr = 3;  
  41.   ptr++;  
  42.   namelist *cur = (namelist *)ptr;  
  43.   // store items  
  44.   tmp.id = 1;  
  45.   strcpy(tmp.name, "Nellson");  
  46.   *cur++ = tmp;  
  47.   tmp.id = 2;  
  48.   strcpy(tmp.name, "Daisy");  
  49.   *cur++ = tmp;  
  50.   tmp.id = 3;  
  51.   strcpy(tmp.name, "Robbie");  
  52.   *cur++ = tmp;  
  53.   exit(0);  
  54.  } else// parent process  
  55.   sleep(1);  
  56.   shmctl(shmid, IPC_STAT, &buff);  
  57.   ptr = (int *) shmat(shmid, NULL, 0);   
  58.   struct namelist tmp;  
  59.   int total = *ptr;  
  60.   printf("/nThere is %d item in the shm/n", total);   
  61.   ptr++;  
  62.   namelist *cur = (namelist *)ptr;  
  63.   for (int i = 0; i< total; i++) {  
  64.    tmp = *cur;  
  65.    printf("%d: %s/n", tmp.id, tmp.name);  
  66.    cur++;  
  67.   }  
  68.   printf("/n");  
  69.   waitpid(pid, &status, 0);  
  70.  }  
  71.  // remvoe a systym V shared memory from system  
  72.  shmctl(shmid, IPC_RMID, NULL);  
  73.  printf("Parent %d get child status:%d/n", getpid(), status);  
  74.  return 0;  
  75. }  

編譯執行

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
  1. /* 
  2.  * 共享記憶體讀寫速度測試 
  3.  */  
  4. #include <unistd.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8. #include <sys/ipc.h>  
  9. #include <sys/mman.h>  
  10. #include <sys/types.h>  
  11. #include <sys/wait.h>  
  12. #include <sys/stat.h>  
  13. #include <fcntl.h>  
  14. #include <unistd.h>  
  15. #include <stdio.h>  
  16. #include <stdlib.h>  
  17. #include <string.h>  
  18. #include <sys/ipc.h>  
  19. #include <sys/mman.h>  
  20. #include <sys/types.h>  
  21. #include <sys/wait.h>  
  22. #include <sys/stat.h>  
  23. #include <fcntl.h>  
  24. #include <errno.h>  
  25. #include <unistd.h>  
  26. #include <stdio.h>  
  27. #include <stdlib.h>  
  28. #include <string.h>  
  29. #include <sys/ipc.h>  
  30. #include <sys/shm.h>  
  31. #include <sys/types.h>  
  32. #include <sys/wait.h>  
  33. #include "module_call.h"  
  34. #define FILE_MODE (S_IRUSR | S_IWUSR)  
  35. #define SVSHM_MODE (SHM_R | SHM_W | SHM_R>>3 | SHM_R>>6)  
  36. enum emType  
  37. {  
  38.     SHMOPEN = 0x01,  
  39.     OPEN = 0x02,  
  40.     SHMGET = 0x04,  
  41. };  
  42. void * GetShmMem(emType type, int size)  
  43. {  
  44.     void * ptr = NULL;  
  45.     switch (type)  
  46.     {  
  47.         case SHMOPEN:  
  48.             {  
  49.                 const char shmfile[] = "/tmp";  
  50.                 int flags = O_RDWR | O_CREAT;  
  51.                 int fd = shm_open(shmfile, flags, FILE_MODE);  
  52.                 if (fd < 0)  
  53.                 {  
  54.                     printf("shm_open failed, errormsg=%s errno=%d/n", strerror(errno), errno);    
  55.                     return NULL;  
  56.                 }  
  57.                 ftruncate(fd, size);  
  58.                 ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);     
  59.                 if (MAP_FAILED == ptr)  
  60.                 {  
  61.                     printf("mmap failed, errormsg=%s errno=%d/n", strerror(errno), errno);    
  62.                     return NULL;  
  63.                 }  
  64.                 break;  
  65.             }  
  66.         case OPEN:  
  67.             {  
  68.                 const char shmfile[] = "./tmp.shm";  
  69.                 int flags = O_RDWR | O_CREAT;  
  70.                 int fd = open(shmfile, flags, FILE_MODE);  
  71.                 if (fd < 0)  
  72.                 {  
  73.                     printf("ope failed, errormsg=%s errno=%d/n", strerror(errno), errno);  
  74.                     return NULL;  
  75.                 }  
  76.                 ftruncate(fd, size);  
  77.                 ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);                 
  78.                 if (MAP_FAILED == ptr)  
  79.                 {  
  80.                     printf("mmap failed, errormsg=%s errno=%d/n", strerror(errno), errno);    
  81.                     return NULL;  
  82.                 }  
  83.                 break;  
  84.             }  
  85.         case SHMGET:  
  86.             {  
  87.                 int shmid;    
  88.                 struct shmid_ds buff;  
  89.                 const char shmfile[] = "./tmp.shm_v";  
  90.                 shmid = shmget(ftok(shmfile, 0), size, SVSHM_MODE | IPC_CREAT);  
  91.                 if (shmid < 0)  
  92.                 {  
  93.                     printf("shmget failed, errormsg=%s errno=%d/n", strerror(errno), errno);  
  94.                     return NULL;  
  95.                 }  
  96.                 ptr = (int *) shmat(shmid, NULL, 0);  
  97.                 if ((void *) -1 == ptr)  
  98.                 {  
  99.                     printf("shmat failed, errormsg=%s errno=%d/n", strerror(errno), errno);  
  100.                     return NULL;  
  101.                 }  
  102.                 shmctl(shmid, IPC_STAT, &buff);  
  103.                 break;  
  104.             }  
  105.     }  
  106.     return ptr;  
  107. }  
  108. int realmain(int size, int loop, emType type)  
  109. {  
  110.     int * array_int = NULL;  
  111.     /* get shmmem*/  
  112.     array_int = (int *)GetShmMem(type, size);  
  113.     if (NULL == array_int)  
  114.     {  
  115.         printf("GetShmMem failed/n");  
  116.         return -1;  
  117.     }  
  118.     /* loop */  
  119.     int array_num = size/sizeof(int);  
  120.     modulecall::call_start();  
  121.     while (0 != loop)  
  122.     {  
  123.         /* write */  
  124.         for (int i = 0; i < array_num; i++)  
  125.         {  
  126.             array_int[i] = i;  
  127.         }  
  128.         /* read */  
  129.         for (int i = 0; i < array_num; i++)  
  130.         {  
  131.             if (array_int[i] != i)  
  132.             {  
  133.                 printf("ShmMem is invalid i=%d v=%d/n", i, array_int[i]);  
  134.                 return -1;  
  135.             }  
  136.         }  
  137.         loop--;  
  138.     }  
  139.     modulecall::call_end();  
  140.     printf("timecost=%lld/n", modulecall::call_timecost());  
  141.     return 0;  
  142. }  
  143. int main(int argc, char ** argv)  
  144. {  
  145.     if (argc < 4)  
  146.     {  
  147.         printf("usage: %s size loop shmtype(1-shmposix 2-mapposix 4-shmv 7-all)/n", argv[0]);  
  148.         return -1;  
  149.     }  
  150.     const int size = atoi(argv[1]);  
  151.     int loop = atoi(argv[2]);  
  152.     const int type = atoi(argv[3]);  
  153.     if ((type&SHMOPEN) == SHMOPEN)  
  154.     {  
  155.         printf("shmopen ");  
  156.         realmain(size, loop, SHMOPEN);  
  157.     }  
  158.     if ((type&OPEN) == OPEN)  
  159.     {  
  160.         printf("open    ");  
  161.         realmain(size, loop, OPEN);  
  162.     }  
  163.     if ((type&SHMGET) == SHMGET)  
  164.     {  
  165.         printf("shmget  ");  
  166.         realmain(size, loop, SHMGET);  
  167.     }     
  168.     return 0;  
  169. }  
  

相關推薦

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為一個程序建立的一個特殊的地址範圍,它將出現在程序的地址空間中。 其他程序可以把同一段共享記憶體段“連