共享內存


共享內存

    共享內存是被多個進程共享的一部分物理內存。共享內存是進程間共享數據的一種最快的方法,一個進程向共享內存區域寫入了數據,共享這個內存區域的所有進程就可以立刻看到其中的內容。原理圖如下:

共享內存的實現分爲兩個步驟:

一、 創建共享內存,使用shmget函數。

二、 映射共享內存,將這段創建的共享內存映射到具體的進程空間去,使用shmat函數。

創建共享內存

int shmget(key_t key ,int size,int shmflg)

key標識共享內存的鍵值:0/IPC_PRIVATE。當key的取值爲IPC_PRIVATE,則函數shmget將創建一塊新的共享內存;如果key的取值爲0,而參數中又設置了IPC_PRIVATE這個標誌,則同樣會創建一塊新的共享內存。

返回值:如果成功,返回共享內存表示符,如果失敗,返回-1。

映射共享內存

int shmat(int shmid,char *shmaddr,int flag)

參數:

shmid:shmget函數返回的共享存儲標識符

flag:決定以什麼樣的方式來確定映射的地址(通常爲0)

返回值:

如果成功,則返回共享內存映射到進程中的地址;如果失敗,則返回-1。

共享內存解除映射

當一個進程不再需要共享內存時,需要把它從進程地址空間中多裏。

int shmdt(char *shmaddr)

貢獻內存實例如下:

實驗要求:創建兩個進程,在A進程中創建一個共享內存,並向其寫入數據,通過B進程從共享內存中讀取數據。

chm_com.h函數

[cpp] view plain copy
  1. #define TEXT_SZ 2048  
  2.   
  3. struct shared_use_st  
  4. {  
  5.     int written_by_you;  
  6.     char some_text[TEXT_SZ];  
  7. };  


讀取進程:

[cpp] view plain copy
  1. /********************************************************** 
  2. *實驗要求:   創建兩個進程,通過共享內存進行通訊。 
  3. *功能描述:   本程序申請和分配共享內存,然後輪訓並讀取共享中的數據,直至 
  4. *           讀到“end”。 
  5. *日    期:   2010-9-17 
  6. *作    者:   國嵌 
  7. **********************************************************/  
  8. #include <unistd.h>  
  9. #include <stdlib.h>  
  10. #include <stdio.h>  
  11. #include <string.h>  
  12. #include <sys/types.h>  
  13. #include <sys/ipc.h>  
  14. #include <sys/shm.h>  
  15. #include "shm_com.h"  
  16.   
  17. /* 
  18.  * 程序入口 
  19.  * */  
  20. int main(void)  
  21. {  
  22.     int running=1;  
  23.     void *shared_memory=(void *)0;  
  24.     struct shared_use_st *shared_stuff;  
  25.     int shmid;  
  26.     /*創建共享內存*/  
  27.     shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
  28.     if(shmid==-1)  
  29.     {  
  30.         fprintf(stderr,"shmget failed\n");  
  31.         exit(EXIT_FAILURE);  
  32.     }  
  33.   
  34.     /*映射共享內存*/  
  35.     shared_memory=shmat(shmid,(void *)0,0);  
  36.     if(shared_memory==(void *)-1)  
  37.     {  
  38.         fprintf(stderr,"shmat failed\n");  
  39.         exit(EXIT_FAILURE);  
  40.     }  
  41.     printf("Memory attached at %X\n",(int)shared_memory);  
  42.   
  43.     /*讓結構體指針指向這塊共享內存*/  
  44.     shared_stuff=(struct shared_use_st *)shared_memory;  
  45.   
  46.     /*控制讀寫順序*/  
  47.     shared_stuff->written_by_you=0;  
  48.     /*循環的從共享內存中讀數據,直到讀到“end”爲止*/  
  49.     while(running)  
  50.     {  
  51.        if(shared_stuff->written_by_you)  
  52.        {  
  53.            printf("You wrote:%s",shared_stuff->some_text);  
  54.            sleep(1);  //讀進程睡一秒,同時會導致寫進程睡一秒,這樣做到讀了之後再寫  
  55.            shared_stuff->written_by_you=0;  
  56.            if(strncmp(shared_stuff->some_text,"end",3)==0)  
  57.            {  
  58.                running=0; //結束循環  
  59.            }  
  60.        }  
  61.     }  
  62.     /*刪除共享內存*/  
  63.     if(shmdt(shared_memory)==-1)  
  64.     {  
  65.         fprintf(stderr,"shmdt failed\n");  
  66.         exit(EXIT_FAILURE);  
  67.     }  
  68.        exit(EXIT_SUCCESS);  
  69. }  


寫入進程:

[cpp] view plain copy
  1. /********************************************************** 
  2. *實驗要求:   創建兩個進程,通過共享內存進行通訊。 
  3. *功能描述:   本程序申請了上一段程序相同的共享內存塊,然後循環向共享中 
  4. *           寫數據,直至寫入“end”。 
  5. *日    期:   2010-9-17 
  6. *作    者:   國嵌 
  7. **********************************************************/  
  8. #include <unistd.h>  
  9. #include <stdlib.h>  
  10. #include <stdio.h>  
  11. #include <string.h>  
  12. #include <sys/types.h>  
  13. #include <sys/ipc.h>  
  14. #include <sys/shm.h>  
  15. #include "shm_com.h"  
  16.   
  17. /* 
  18.  * 程序入口 
  19.  * */  
  20. int main(void)  
  21. {  
  22.     int running=1;  
  23.     void *shared_memory=(void *)0;  
  24.     struct shared_use_st *shared_stuff;  
  25.     char buffer[BUFSIZ];  
  26.     int shmid;  
  27.     /*創建共享內存*/  
  28.     shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
  29.     if(shmid==-1)  
  30.     {  
  31.         fprintf(stderr,"shmget failed\n");  
  32.         exit(EXIT_FAILURE);  
  33.     }  
  34.   
  35.     /*映射共享內存*/  
  36.     shared_memory=shmat(shmid,(void *)0,0);  
  37.     if(shared_memory==(void *)-1)  
  38.     {  
  39.         fprintf(stderr,"shmat failed\n");  
  40.         exit(EXIT_FAILURE);  
  41.     }  
  42.     printf("Memory attached at %X\n",(int)shared_memory);  
  43.   
  44.     /*讓結構體指針指向這塊共享內存*/  
  45.     shared_stuff=(struct shared_use_st *)shared_memory;  
  46.     /*循環的向共享內存中寫數據,直到寫入的爲“end”爲止*/  
  47.     while(running)  
  48.     {  
  49.         while(shared_stuff->written_by_you==1)  
  50.         {  
  51.             sleep(1);//等到讀進程讀完之後再寫  
  52.             printf("waiting for client...\n");  
  53.         }  
  54.         printf("Ener some text:");  
  55.         fgets(buffer,BUFSIZ,stdin);  
  56.         strncpy(shared_stuff->some_text,buffer,TEXT_SZ);  
  57.         shared_stuff->written_by_you=1;  
  58.         if(strncmp(buffer,"end",3)==0)  
  59.         {  
  60.             running=0;  //結束循環  
  61.         }  
  62.     }  
  63.     /*刪除共享內存*/  
  64.     if(shmdt(shared_memory)==-1)  
  65.     {  
  66.         fprintf(stderr,"shmdt failed\n");  
  67.         exit(EXIT_FAILURE);  
  68.     }  
  69.     exit(EXIT_SUCCESS);  
  70. }  

3 . 在一個終端中運行shm1,在另一個終端中運行shm2.當shm1運行起來之後,由於共享內存中沒有數據可讀,會處於等待狀態

[root@localhost 2-4-4]# ./shm1

Memory attached at B7F9A000

/***阻塞***/

再向shm2運行的終端輸入字符串

[root@localhost 2-4-4]# ./shm2

Memory attached at B7FD8000

Enter some text:Impossible is nothing

waiting for client。。。

waiting for client。。。

Enter some text:Anything is possible

waiting for client。。。

Ener some text:end

[root@localhost 2-4-4]#

shm1能夠逐個從共享內存中巴他們讀出來,知道雙方暈倒字符串"end"後,兩個程序都退出。

[root@localhost 2-4-4]# ./shm1

Memory attached at B7F9A000

You write:Impossible is nothing

You write:Anything is possible

You write:end

[root@localhost 2-4-4]#

以上運行過程中,紅色表示在終端1中運行的結果,藍色表示在終端2裏面運行的結果。

發佈了12 篇原創文章 · 獲贊 23 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章