2012-12-10 50 views
0

在書中Expert C Programming P101加載的共享庫,有一段話:如何使用它通過mmap的

For example, if you have eight XViewTM applications running, only one copy of the XView library text segment has to be mapped into memory. The first process's mmap [1] call will result in the kernel mapping the shared object into memory. The next seven process mmaps will cause the kernel to share the existing mapping in each process. Each of the eight processes will share one copy of the XView library in memory. If the library were statically linked, there would be eight individual copies consuming more physical memory and causing more paging.

由於MMAP只返回內存的指針,我不知道如何可以使用變量或調用庫中定義的方法?

+0

你想自己手動加載共享對象嗎?那麼你應該使用例如['dlopen'](http://pubs.opengroup.org/onlinepubs/009695399/functions/dlopen.html)和['dlsym'](http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym的.html)。內存映射和共享由加載程序和操作系統處理。對於您鏈接的庫,加載器和操作系統也可以處理這些庫。 –

+0

看看http://beej.us/guide/bgipc/output/html/multipage/shm.html –

+0

我明白了,Thx非常多:) –

回答

1

這是描述系統自動使用的機制。除了將程序與共享庫鏈接以利用此共享之外,您不需要執行任何操作。你當然不需要自己撥打mmap(),或者找出如何以某種特殊方式調用函數。