共享内存
博文
https://www.cnblogs.com/fangshenghui/p/4039720.html
编程步骤
步骤 | 进程1 | 进程2 | 使用函数 |
---|---|---|---|
1 | 使用约定的文件创建key | 使用约定的文件创建key | ftok |
2 | 使用key创建共享内存 | 使用key获取到共享内存的id | shmget |
3 | 挂载共享内存 | 挂载共享内存 | shmat |
4 | 使用内存 | 使用内存 | |
5 | 卸载共享内存 | 卸载共享内存 | shmdt |
6 | 释放共享内存 | shmctl |
使用实例
进程1:读
int running = 1; //程序是否继续运行的标志
void *shm = NULL; //分配的共享内存的原始首地址
struct shared_use_st *shared; //指向shm
int shmid; //共享内存标识符
//创建共享内存
shmid = shmget((key_t)MEM_KEY, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
//将共享内存连接到当前进程的地址空间
shm = shmat(shmid, 0, 0);
if (shm == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("\nMemory attached at %X\n", (int)shm);
//设置共享内存
shared = (struct shared_use_st *)shm;
shared->written = 0;
while (running) //读取共享内存中的数据
{
//没有进程向共享内存定数据有数据可读取
if (shared->written != 0)
{
printf("You wrote: %s", shared->text);
sleep(rand() % 3);
//读取完数据,设置written使共享内存段可写
shared->written = 0;
//输入了end,退出循环(程序)
if (strncmp(shared->text, "end", 3) == 0)
running = 0;
}
else //有其他进程在写数据,不能读取数据
sleep(1);
}
//把共享内存从当前进程中分离
if (shmdt(shm) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
//删除共享内存
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
进程2:写
int running = 1;
void *shm = NULL;
struct shared_use_st *shared = NULL;
char buffer[BUFSIZ + 1]; //用于保存输入的文本
int shmid;
//创建共享内存
shmid = shmget((key_t)MEM_KEY, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
//将共享内存连接到当前进程的地址空间
shm = shmat(shmid, (void *)0, 0);
if (shm == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shm);
//设置共享内存
shared = (struct shared_use_st *)shm;
while (running) //向共享内存中写数据
{
//数据还没有被读取,则等待数据被读取,不能向共享内存中写入文本
while (shared->written == 1)
{
sleep(1);
printf("Waiting...\n");
}
//向共享内存中写入数据
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared->text, buffer, TEXT_SZ);
//写完数据,设置written使共享内存段可读
shared->written = 1;
//输入了end,退出循环(程序)
if (strncmp(buffer, "end", 3) == 0)
running = 0;
}
//把共享内存从当前进程中分离
if (shmdt(shm) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
sleep(2);
exit(EXIT_SUCCESS);