共享内存:、
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);
}