17.共享内存的操作
2020-04-22 本文已影响0人
陈忠俊
system v IPC
,通过ftok
创建共享内存键值
ftok - convert a pathname and a project identifier to a System V IPC key
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
int main(int argc, char *argv[]){
//get a system v ipc key's value
key_t key = ftok(argv[1], 31);
if(key == -1) return -1;
printf("key: 0x%x\n", key);
return 0;
}
输出:
zhongjun@eclipse:~/projects$ gcc ftok.c -o ftok
zhongjun@eclipse:~/projects$ ./ftok ftok.i
key: 0x1f02f9ff
获取共享内存段的键值:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, char *argv[]){
//get a system v ipc key's value
key_t key = ftok(argv[1], 31);
if(key == -1) return -1;
printf("key: 0x%x\n", key);
//using current key to get the share memory segment
int shmid = shmget(key, 1024, IPC_CREAT | IPC_EXCL | 0644);
if(shmid == -1) return -1;
printf("shmid: %d \n", shmid);
return 0;
}
输出:
zhongjun@eclipse:~/projects$ gcc shmget.c -o shmget
zhongjun@eclipse:~/projects$ ./shmget ftok.i
key: 0x1f02f9ff
shmid: 163841
zhongjun@eclipse:~/projects$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 98304 lightdm 600 524288 2 dest
0x1f02f9ff 163841 zhongjun 644 1024 0
利用两个程序,交互使用共享内存段
shareA.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
int main(int argc, char *argv[]){
//get a system v ipc key's value
key_t key = ftok(argv[1], 31);
if(key == -1) return -1;
printf("key: 0x%x\n", key);
//using current key to get the share memory segment
int shmid = shmget(key, 1024, IPC_CREAT | 0644);
if(shmid == -1) return -1;
printf("shmid: %d \n", shmid);
//使用共享内存段的ID,将共享内存段关联到进程的地址空间
void *p = shmat(shmid, NULL, 0);
if(p == (void *) -1) return -1;
strcpy(p, "hello sky walker!\n");
//使用完成后,释放该共享内存段
shmdt(p);
return 0;
}
shareB.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, char *argv[]){
//get a system v ipc key's value
key_t key = ftok(argv[1], 31);
if(key == -1) return -1;
printf("key: 0x%x\n", key);
//using current key to get the share memory segment
int shmid = shmget(key, 1024, IPC_CREAT | 0644);
if(shmid == -1) return -1;
printf("shmid: %d \n", shmid);
//使用共享内存段的ID,将共享内存段关联到进程的地址空间
void *p = shmat(shmid, NULL, 0);
if(p == (void *) -1) return -1;
printf("%s", (char *)p);
//使用完成后,释放该共享内存段
shmdt(p);
return 0;
}
输出:
zhongjun@eclipse:~/projects$ gcc shareA.c -o shareA
zhongjun@eclipse:~/projects$ gcc shareB.c -o shareB
zhongjun@eclipse:~/projects$ ./shareA ftok.i
key: 0x1f02f9ff
shmid: 163841
zhongjun@eclipse:~/projects$ ./shareB ftok.i
key: 0x1f02f9ff
shmid: 163841
hello sky walker!
zhongjun@eclipse:~/projects$
最后,通过ipcrm
删除共享内存段
ipcrm -h
Options:
-m, --shmem-id <id> remove shared memory segment by id
-M, --shmem-key <key> remove shared memory segment by key
zhongjun@eclipse:~/projects$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 98304 lightdm 600 524288 2 dest
0x1f02f9ff 163841 zhongjun 644 1024 0
zhongjun@eclipse:~/projects$ ipcrm -M 0x1f02f9ff
zhongjun@eclipse:~/projects$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 98304 lightdm 600 524288 2 dest