程序間通訊之共享記憶體

2022-09-24 01:33:10 字數 4407 閱讀 1950

共享記憶體允許兩個或多個程序共享乙個給定的儲存區。因為資料不需要在兩個程序之間進行複製,所以這是一種最快的程序間通訊方式。使用共享儲存區的唯一竅門是多個程序之間對乙個給定的儲存區的同步訪問。通常訊號量被用來實現對共享儲存訪問的同步。

在linux系統中系統共享儲存段的做大位元組數為33554432,最小位元組數為1;系統中共享儲存段的最大段數4096,每個程序共享儲存段的最大段數為4096.

獲得共享記憶體識別符號:

[cpp]

#include   

int shmget(key_t key, size_t size, int flag); 

返回值:成功返回共享記憶體id,失敗返回-1 

#include

int shmget(key_t key, size_t size, int flag);

返回值:成功返回共享記憶體id,失敗返回-1     引數size是共享記憶體的段的大小,如果正在建立乙個新段,則必須指定size的大小,如果正在引用乙個現存的段,則將size設定為0.

引數flag是表示對該段的操作,建立段的時候該標誌應該為ipc_creat|0666,ipc_creat為建立共享記憶體,0666為可讀可寫許可權。

引數key,鍵。這個概念比較模糊。通過函式ftok函式獲得乙個key。

[html]

#include  

key_t ftok(const char *path, int id); 

返回值:成功返回鍵值,出錯返回-1 

#include

key_t ftok(const char *path, int id);

返回值:成功返回鍵值,出錯返回-1    path引數必須是乙個存在的檔案。

共享記憶體建立完成後,就可以使用shmat函式將其連線到它的位址空間中。

[html]

#include  

void *shmat(int shmid, const void *addr, int flag); 

返回值:成功返回指向共享記憶體的指標,出錯返回-1 

#include

void *shmat(int shmid, const void *addr, int flag);

返回值:成功返回指向共享記憶體的指標,出錯返回-1    共享儲存段連線到呼叫程序的那個位址上與addr引數以及在flag中是否指定shm_rnd位有關。

1、如果addr為0,則此段連線到由核心選擇的第乙個可用位址上,這是推薦的使用方式

2、如果addr非0,並且沒有指定shm_rnd,則此段連線到addr所指的位址上。

除非指定在一種硬體上使用執行應用程式,否則是不應該指定共享記憶體所連線到的位址,所以一般addr指定位0,以便由核心選擇位址。

對共享記憶體操作結束後,呼叫函式shmdt脫離該共享記憶體。注意,這並不是從系統中刪除其識別符號以及其資料結構,該識別符號依然存在,直到某個程序呼叫shmctl特地刪除,後面介紹shmctl函式。

[html]

#include  

int shmdt(void *addr) 

返回值:成功返回0,出錯返回-1. 

#include

int shmdt(void *addr)

返回值:成功返回0,出錯返回-1.

shmctl函式對共享記憶體執行多種操作。

[html] view plaincopyprint?#include  

int shmctl(int shmid, int cmd, struct shmid_ds, *buf); 

返回值:成功返回0,失敗返回-1. 

#include

int shmctl(int shmid, int cmd, struct shmid_ds, *buf);

返回值:成功返回0,失敗返回-1.     cmd引數指定下列5個命令,使其在shmid指定的共享記憶體上執行。

1、ipc_stat 去此段的shmid_ds結構,並將它存放在由buf指向的結構中。

2、ipc_rmid從系統中刪除共享記憶體段。我們用的最多的。

下面是我寫的乙個關於共享記憶體使用的程式;

[html]

#include  

#include  

#include  

#include  

#include  

#include  

#include  

#include  

#include  

int sharememorycom; 

//除錯列印巨集定義 

#define dbg(level,fmt,para...)  \ 

#define dbg_infor                   0x01        //正常列印資訊 

#define dbg_warning             0x02        //處理警告異常 

#define dbg_error                   0x04        //處理錯誤 

#define success             1 

#define fail                    -1 

typedef struct 

sharem; 

key_t shmkey; 

int createshm()   

shm_id=shmget(shmkey,4096,ipc_creat|0666);   

if(shm_id==fail) 

return success; }  

int setshareshm(int lx,int zt) 

p_map=(sharem*)shmat(shm_id,null,0); 

(*p_map).lx=lx; 

(*p_map).zt=zt; 

if(shmdt(p_map)==fail) 

return fail; 

return 0x00; }  

int getshareshm() 

p_map = (sharem*)shmat(shm_id,null,0); 

lx = (*p_map).lx; 

zt = (*p_map).zt; 

printf("lx = %d, zt = %d\n", lx, zt); 

if(shmdt(p_map)==fail) 

return fail; 

return zt;  } 

void sharememory()   

int main(void) 

return 0; 

} #include

#include

#include

#include

#include

#include

#include

#include

#include

int sharememorycom;

//除錯列印巨集定義

#define dbg(level,fmt,para...)  \

#define dbg_infor       0x01  //正常列印資訊

#define dbg_warning    0x02  //處理警告異常

#define dbg_error       0x04  //處理錯誤

#define success    1

#define fail     -1

typedef struct

sharem;

key_t shmkey;

int createshm()

shm_id=shmget(shmkey,4096,ipc_creat|0666); 

if(shm_id==fail)

return success;

}int setshareshm(int lx,int zt)

p_map=(sharem*)shmat(shm_id,null,0);

(*p_map).lx=lx;

(*p_map).zt=zt;

if(shmdt(p_map)==fail)

return fail;

return 0x00;

}int getshareshm()

p_map = (sharem*)shmat(shm_id,null,0);

lx = (*p_map).lx;

zt = (*p_map).zt;

printf("lx = %d, zt = %d\n", lx, zt);

if(shmdt(p_map)==fail)

return fail;

return zt;

void sharememory()

int main(void)

return 0;

}

程序間通訊之共享記憶體

此程式實現兩個普通程序間通過共享記憶體來進行通訊,共享記憶體能夠進行大資料量的通訊,這一點事訊息佇列無法比擬的。在這裡同時使用了訊號量來保證兩個程序間的讀寫同步。傳送端源 include include include include include include include include ...

程序間通訊之共享記憶體

1.概念 共享記憶體就是多個程序的位址空間對映到同乙個物理記憶體,多個程序都能看到這塊物理記憶體,共享記憶體可以提供給伺服器程序和客戶程序之間進行通訊,不需要進行資料的複製,所以速度最快。2.共享記憶體操作需要的函式 1 我們需要利用ftok函式生成key識別符號。key t ftok const ...

程序間通訊之共享記憶體

ipc物件這個概念需要理解,因為好多書或者料就直接說ipc就是共享記憶體 訊息佇列 訊號燈集,其實ipc是一種機制,這種機制提供了程序間通訊的通道,那麼為什麼加個system v呢,那是因為在system v 系統的四個版本中提出的程序通訊的ipc這種機制。所以叫做system v ipc。目前li...