程序通訊 mmap記憶體共享

2021-08-21 08:10:30 字數 1808 閱讀 2732

回頁首if(fd<0)

...回頁首

#include #include #include #include typedef structpeople;

main(int argc, char** argv) // map a normal file as shared mem:

printf(" initialize over /n ");

sleep(10);

munmap( p_map, sizeof(people)*10 );

printf( "umap ok /n" );

}/*-------------map_normalfile2.c-----------*/

#include #include #include #include typedef structpeople;

main(int argc, char** argv) // map a normal file as shared mem:

munmap( p_map,sizeof(people)*10 );

}umap ok

name: g age 25; name: h age 26; name: i age 27; name: j age 28; name: k age 29;

name: age 0; name: age 0; name: age 0; name: age 0; name: age 0;

char name[4];

int age;

}people;

main(int argc, char** argv)

temp = 'a';

for(i = 0;i<5;i++)

sleep(5);

printf( "parent read: the first people,s age is %d/n",(*p_map).age );

printf("umap/n");

munmap( p_map,sizeof(people)*10 );

printf( "umap ok/n" );

}child read: the 2 people's age is 21

child read: the 3 people's age is 22

child read: the 4 people's age is 23

child read: the 5 people's age is 24

parent read: the first people,s age is 100

umap

umap ok

回頁首 char name[4];

int age;

}people;

main(int argc, char** argv)

munmap(p_map,sizeof(people)*10);

}access page 1 over

access page 1 edge over, now begin to access page 2

access page 2 over

access page 2 over

access page 2 edge over, now begin to access page 3

bus error //被對映檔案在程序空間中覆蓋了兩個頁面,此時,程序試圖訪問第三個頁面

access page 1 over

access page 1 edge over, now begin to access page 2

bus error //被對映檔案在程序空間中覆蓋了乙個頁面,此時,程序試圖訪問第二個頁面

Linux程序共享通訊 mmap實現

使用mmap記憶體對映實現一端寫,另一端讀的程序間通訊 寫端 write.c write.c include include include include include include include 對映記憶體大小 define maplen 0x100 定義乙個學生資訊結構體 struct ...

linux程序間記憶體共享 mmap

系統呼叫mmap 用於共享記憶體的兩種方式 1 使用普通檔案提供的記憶體對映 適用於任何程序之間 此時,需要開啟或建立乙個檔案,然後再呼叫mmap 典型呼叫 如下 fd open name,flag,mode if fd 0 ptr mmap null,len prot read prot writ...

程序通訊 共享記憶體

定義 共享記憶體,就是通過核心操作,在記憶體上開闢一塊供多個程序共同訪問的記憶體塊。這塊記憶體塊的建立和 銷毀是由核心來控制 當然,也可以在程序內部呼叫系統庫函式來建立和銷毀,類似於訊息機制和訊號 量機制 在這個記憶體塊上,程序可以像操作記憶體一樣操作共享區記憶體。作用 第乙個,就是提供程序間大資訊...