共享內存:、

共享內存:、



http://blog.csdn.net/sctq8888/article/details/7494298



http://www.360doc.com/content/08/0702/11/67071_1389854.shtml



第一步:是創建共享內存,這裏用到的函數是shmget,也是從內存中獲得一段共享內存區域。


第二步:映射共享內存,也是把這段創建的共享內存映射到具體的進程空間去,這裏使用的函數是shmat


到這裏,可以使用這段共享內存了,也是可以使用不帶緩衝的I/O讀寫命令對其進行操作。


除此之外,當然還有撤銷映射的操作,其函數爲shmdt


函數介紹:


shmget

shmget- allocates a shared memory segment

頭文件:

#include<sys/ipc.h>

#include<sys/shm.h>

函數原型:

intshmget(key_t key, size_t size, int shmflg);

returnsthe identifier of the shared memory segment associated with the valueof the argument key.

返回共享內存的標識


shmget()創建了一塊新的共享內存後,返回一個可以用於引用該共享內存的shmid_ds數據結構的標識符。

structshmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */
__kernel_time_tshm_atime; /* last attach time */
__kernel_time_t shm_dtime; /* last detach time */
__kernel_time_t shm_ctime; /* lastchange time */
__kernel_ipc_pid_t shm_cpid; /* pid of creator */
__kernel_ipc_pid_t shm_lpid; /* pid of last operator */
unsigned short shm_nattch; /* no. of current attaches */
unsignedshort shm_unused; /* compatibility */
void *shm_unused2; /*ditto - used by DIPC */
void *shm_unused3; /* unused */
};

ipcs-m命令查看


A new shared memory segment, with size equal to the value of sizerounded up to a multiple of PAGE_SIZE, is created if key hasthe value IPC_PRIVATE or key isn't IPC_PRIVATE, no shared memory segment corresponding to

keyexists, and IPC_CREAT is specified in shmflg.


key的取值爲IPC_PRIVATE,則函數shmget()將創建一塊新的共享內存;如果key的取值爲0,而參數shmflg中設置了IPC_PRIVATE這個標誌,則同樣將創建一塊新的共享內存。


Thevalue shmflg is composed of:

IPC_CREAT to create a new segment. If this flag is not used,

thenshmget() will find the segment associated with key

and check to see if the user has permission to access

thesegment.

IPC_EXCL used with IPC_CREAT to ensure failure if the segment

alreadyexists.

mode_flags (least significant 9 bits) specifying the permissions

grantedto the owner, group, and world. These bits

havethe same format, and the same meaning, as the mode

argumentof open(2). Presently, the execute permis‐

sionsare not used by the system.




shmat

shmat,shmdt - shared memory operations

頭文件:

#include<sys/types.h>

#include<sys/shm.h>

函數原型:

void*shmat(int shmid, const void *shmaddr, int shmflg);


If shmaddr is NULL, the system chooses a suitable (unused)address

atwhich to attach the segment.


Ifshmaddr isn't NULL and SHM_RND is specified in shmflg, the

attach occurs at the address equal to shmaddr rounded down to the

nearestmultiple of SHMLBA. Otherwise shmaddr must be a page-

alignedaddress at which the attach occurs.




shmid:要映射的共享內存區標識符。

hmaddr:將共享內在映射到指定位置(若爲0則表示把該段共享內存映射到調用進程的地址空間)。

shmflg SHM_RDONLY:共享內存只讀。默認0:共享內存可讀寫。


intshmdt (const void * shmaddr)


shmaddr:被映射的共享內存段地址



實例:

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include<stdio.h>

#include<stdlib.h>

#defineBUFSZ 2048

intmain()

{

intshmid;

char*shmadd;

if((shmid = shmget(IPC_PRIVATE,BUFSZ,0666))<0) {

perror("shmgeterror");

exit(1);

}

else

printf("Createdshared-memory: %d\n", shmid);

system("ipcs-m");

if((shmadd = shmat(shmid,0,0))<(char *)0) {

perror("shmat error");

exit(1);

}else

printf("Attachedshared memory\n");

system("ipcs-m");

if((shmdt(shmadd))< 0) {

perror("shmdt");

exit(1);

}

else

printf("Deletedshared-memory.\n");

system("ipcs-m");

exit(0);

}





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