linux共享記憶體簡析

2021-09-08 16:58:16 字數 1311 閱讀 2125

共享記憶體是ipc的一種機制,允許兩個不相關的程序共享同一塊記憶體

//共享記憶體可以雙向通訊,但其本身沒有相應機制,需要程式編寫者設計,本例為單向通訊(分為讀端和寫端)。

共享記憶體讀端:

#include

#include

#include

#include

#include

#include

//自定義資料結構,flag為同步機制標誌:flag 為1表示讀端可讀, flag為1表示寫端可寫;str為資料儲存的字元陣列

struct my_shared

;int main(void)

//連線共享記憶體到程序位址空間

shared_memory = shmat(shmid, null, 0);

if(shared_memory == null)

shared_buff = (struct my_shared *)shared_memory;

shared_buff->flag = 0;

while(running)

}}//把共享從本程序空間分離

if(shmdt(shared_memory) == -1)

//刪除共享記憶體

if(shmctl(shmid, ipc_rmid, 0) == -1)

return 0; }

#include

#include

#include

#include

#include

#include

struct my_shared

;int main(void)

shared_memory = shmat(shmid, null, 0);

if(shared_memory == null)

shared_buff = (struct my_shared *)shared_memory;

shared_buff->flag = 0;

while(running)

printf("please input some data\n");

fgets(buffer, 512, stdin);

shared_buff->flag = 1;

strncpy(shared_buff->str, buffer, 1024);

if(strncmp(shared_buff->str, "end", 3) == 0)

}if(shmdt(shared_memory) == -1)

return 0;

}

Linux記憶體對映(mmap)簡析

14 include 15 include 16 include 17 include 18 include 19 20 21 define device name mymap 22 23 24 static unsigned char array 10 25 static unsigned cha...

Oracle共享池簡析

oracle共享池 oracle共享池 share pool 屬於sga,由庫快取記憶體 library cache 和資料字 典快取記憶體 data dictionary cache 組成。庫快取記憶體 www.2cto.com oracle引入庫快取記憶體的目的是共享sql和pl sql 伺服器...

關於Linux cached記憶體簡析

測試mbs系統,ap的weblogic 只是設定了1.5g記憶體,但是經過3次穩定性測試,記憶體的利用率達到99 nmon的監控揭發發現存在5.6g的cached記憶體,下面介紹一下 linux與windows不同,會存在快取記憶體,通常叫做cache memory。有些時候你會發現沒有什麼程式在執...