哈尔滨理工大学软件工程专业08-7李万鹏原创作品,转载请标明出处
http://blog.csdn.net/woshixingaaa/archive/2010/06/10/5661588.aspx
内存共享分为两个步骤:
一.创建共享内存使用shmget函数
二.映射到共享内存,将这段共享内存映射到具体的进程空间去,使用shmat函数
系统调用:shmget();
原型: int shmget(key_t key,int size, int shmflg);
返回值:如果成功返回共享内存段标识符
如果失败返回-1
系统调用:shmat();
原型:int shmat(int shmid, char *shmaddr, int shmflag);
返回值:如果成功,返回到共享内存段连接到进程段的地址.
如果失败返回-1
当一个进程不在需要共享内存段时,他将会把内存段从地之空间脱离
系统调用:shmdt();
原型;int shmdt(char *shmaddr);
如果返回失败返回-1
看一个例子:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFSZ 2023
int main()
{
int shmid;
char *shmadd;
if((shmid=shmget(IPC_PRIVATE,BUFSZ,0666))<0){ //表明由系统选用的关键字
perror("shmget");
exit(1);
}
else
printf("created shared-memory: %d/n",shmid);
system("ipcs -m");
if((shmadd=shmat(shmid,0,0))<(char *)0){
perror("shmat");
exit(1);
}
else
printf("attached shared-memory/n");
system("ipcs -m");
if((shmdt(shmadd))<0){
perror("shmdt");
exit(1);
}
else
printf("deleted shared-memory/n");
system("ipcs -m");
exit(0);
}
下面是输出结果
fighter@fighter:~$ ./sharememory
creat share_memory
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 root 777 135168 2
0x00000000 32769 fighter 600 393216 2 dest
0x00000000 65538 fighter 600 393216 2 dest
0x00000000 98307 fighter 600 393216 2 dest
0x00000000 131076 fighter 600 393216 2 dest
0x00000000 163845 fighter 600 393216 2 dest
0x00000000 196614 fighter 600 393216 2 dest
0x00000000 229383 fighter 600 393216 2 dest
0x00000000 262152 fighter 600 393216 2 dest
0x00000000 294921 fighter 600 393216 2 dest
0x00000000 327690 fighter 600 393216 2 dest
0x00000000 360459 fighter 600 393216 2 dest
0x00000000 393228 fighter 600 393216 2 dest
0x00000000 5832717 fighter 600 393216 2 dest
0x00000000 5865486 fighter 777 720000 2 dest
0x00000000 5898255 fighter 777 720000 2 dest
0x00000000 851984 fighter 600 393216 2 dest
0x00000000 720913 fighter 600 393216 2 dest
0x00000000 1933330 fighter 600 393216 2 dest
0x00000000 1966099 fighter 600 393216 2 dest
0x00000000 6455316 fighter 666 2048 0
0x00000000 4096021 fighter 600 50400 2 dest
0x00000000 1900566 fighter 600 393216 2 dest
0x00000000 6488087 fighter 666 2026 0
0x00000000 6094872 fighter 600 393216 2 dest
0x00000000 3735577 fighter 600 393216 2 dest
0x00000000 3768346 fighter 600 393216 2 dest
0x00000000 3801115 fighter 600 12288 2 dest
0x00000000 3833884 fighter 600 12288 2 dest
0x00000000 6127645 fighter 666 2024 0
0x00000000 6160414 fighter 666 2024 0
0x00000000 6357023 fighter 666 2024 0
0x00000000 6389792 fighter 666 2026 0
0x00000000 6422561 fighter 600 393216 2 dest
0x00000000 6717474 fighter 666 2049 0
0x00000000 6750243 fighter 666 2026 0
0x00000000 6783012 fighter 666 2027 0
0x00000000 6946853 fighter 600 393216 2 dest
0x00000000 6979622 fighter 600 393216 2 dest
0x00000000 6881319 fighter 666 2027 0
0x00000000 6914088 fighter 666 2023 0
0x00000000 7012393 fighter 666 2023 0
share_memory attach
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 root 777 135168 2
0x00000000 32769 fighter 600 393216 2 dest
0x00000000 65538 fighter 600 393216 2 dest
0x00000000 98307 fighter 600 393216 2 dest
0x00000000 131076 fighter 600 393216 2 dest
0x00000000 163845 fighter 600 393216 2 dest
0x00000000 196614 fighter 600 393216 2 dest
0x00000000 229383 fighter 600 393216 2 dest
0x00000000 262152 fighter 600 393216 2 dest
0x00000000 294921 fighter 600 393216 2 dest
0x00000000 327690 fighter 600 393216 2 dest
0x00000000 360459 fighter 600 393216 2 dest
0x00000000 393228 fighter 600 393216 2 dest
0x00000000 5832717 fighter 600 393216 2 dest
0x00000000 5865486 fighter 777 720000 2 dest
0x00000000 5898255 fighter 777 720000 2 dest
0x00000000 851984 fighter 600 393216 2 dest
0x00000000 720913 fighter 600 393216 2 dest
0x00000000 1933330 fighter 600 393216 2 dest
0x00000000 1966099 fighter 600 393216 2 dest
0x00000000 6455316 fighter 666 2048 0
0x00000000 4096021 fighter 600 50400 2 dest
0x00000000 1900566 fighter 600 393216 2 dest
0x00000000 6488087 fighter 666 2026 0
0x00000000 6094872 fighter 600 393216 2 dest
0x00000000 3735577 fighter 600 393216 2 dest
0x00000000 3768346 fighter 600 393216 2 dest
0x00000000 3801115 fighter 600 12288 2 dest
0x00000000 3833884 fighter 600 12288 2 dest
0x00000000 6127645 fighter 666 2024 0
0x00000000 6160414 fighter 666 2024 0
0x00000000 6357023 fighter 666 2024 0
0x00000000 6389792 fighter 666 2026 0
0x00000000 6422561 fighter 600 393216 2 dest
0x00000000 6717474 fighter 666 2049 0
0x00000000 6750243 fighter 666 2026 0
0x00000000 6783012 fighter 666 2027 0
0x00000000 6946853 fighter 600 393216 2 dest
0x00000000 6979622 fighter 600 393216 2 dest
0x00000000 6881319 fighter 666 2027 0
0x00000000 6914088 fighter 666 2023 0
0x00000000 7012393 fighter 666 2023 1
delete share_memory
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 root 777 135168 2
0x00000000 32769 fighter 600 393216 2 dest
0x00000000 65538 fighter 600 393216 2 dest
0x00000000 98307 fighter 600 393216 2 dest
0x00000000 131076 fighter 600 393216 2 dest
0x00000000 163845 fighter 600 393216 2 dest
0x00000000 196614 fighter 600 393216 2 dest
0x00000000 229383 fighter 600 393216 2 dest
0x00000000 262152 fighter 600 393216 2 dest
0x00000000 294921 fighter 600 393216 2 dest
0x00000000 327690 fighter 600 393216 2 dest
0x00000000 360459 fighter 600 393216 2 dest
0x00000000 393228 fighter 600 393216 2 dest
0x00000000 5832717 fighter 600 393216 2 dest
0x00000000 5865486 fighter 777 720000 2 dest
0x00000000 5898255 fighter 777 720000 2 dest
0x00000000 851984 fighter 600 393216 2 dest
0x00000000 720913 fighter 600 393216 2 dest
0x00000000 1933330 fighter 600 393216 2 dest
0x00000000 1966099 fighter 600 393216 2 dest
0x00000000 6455316 fighter 666 2048 0
0x00000000 4096021 fighter 600 50400 2 dest
0x00000000 1900566 fighter 600 393216 2 dest
0x00000000 6488087 fighter 666 2026 0
0x00000000 6094872 fighter 600 393216 2 dest
0x00000000 3735577 fighter 600 393216 2 dest
0x00000000 3768346 fighter 600 393216 2 dest
0x00000000 3801115 fighter 600 12288 2 dest
0x00000000 3833884 fighter 600 12288 2 dest
0x00000000 6127645 fighter 666 2024 0
0x00000000 6160414 fighter 666 2024 0
0x00000000 6357023 fighter 666 2024 0
0x00000000 6389792 fighter 666 2026 0
0x00000000 6422561 fighter 600 393216 2 dest
0x00000000 6717474 fighter 666 2049 0
0x00000000 6750243 fighter 666 2026 0
0x00000000 6783012 fighter 666 2027 0
0x00000000 6946853 fighter 600 393216 2 dest
0x00000000 6979622 fighter 600 393216 2 dest
0x00000000 6881319 fighter 666 2027 0
0x00000000 6914088 fighter 666 2023 0
0x00000000 7012393 fighter 666 2023 0