c共享内存

本文详细介绍了在C语言中如何通过`shmget`、`shmat`、`shmdt`和`shmctl`等系统调用来创建、关联、读写和删除共享内存,包括示例代码和常见操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

共享内存实现

共享内存实质是将内核中的一块内存映射到进程中的内存,操作本地内存就相当于操作共享内存。

使用共享内存步骤:

  1. 创建共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
/*
创建共享内存:
int shmget(key_t key, size_t size, int shmflg);
函数描述:
	创建或获得共享内存id	
一般用法:
	1、如果共享内存已经存在:
		key_t key = 0x1234;
		int shmId = shmget(key, 0, 0);
	2、如果共享内存不存在:
		int shmId = shmget(key, 100, IPC_CREAT|IPC_EXCL|0755);
	3、如果不知道到底存不存在:
		int shmId = shmget(key, 100, IPC_CREAT|0755);
*/
  1. 关联共享内存
#include <sys/types.h>
#include <sys/shm.h>

/*
连接共享内存:
void *shmat(int shmid, const void *shmaddr, int shmflg);
函数描述:
	连接共享内存
参数:
	shmid: shmget函数返回的共享内存的id值
	shmaddr: 传NULL,表示让内核分配地址
	shmflg: 
		SHM_RDONLY:只能对共享内存进行读操作
		0:可读可写
返回值:
	成功:返回关联共享内存的地址
	失败:(void*)(-1)
*/
  1. 使用共享内存–读写共享内存
  2. 断开与共享内存的关联
#include <sys/types.h>
#include <sys/shm.h>
/*
int shmdt(const void *shmaddr);
参数:
	shmaddr: shmat返回的内存地址
返回值:
	成功:返回0
	失败:返回-1,并设置errno

*/
  1. 删除共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
/*
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
函数描述:
	设置或删除共享内存
参数:
	shmid:shmget函数返回的共享内存的id值
	cmd:
		IPC_STAT:获得共享内存的状态信息
		IPC_SET:设置共享内存信息
		IPC_RMID:删除共享内存
	buf:
		若cmd为IPC_RMID, 则buf为NULL
*/

示例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

int main()
{
    // 创建共享内存 int shmget(key_t key, size_t size, int shmflg);
    key_t key = 0x12345678;
    int nShmId = shmget(key, 1024, IPC_CREAT | 0664);
    if (0 > nShmId)
    {
        perror("shmget error");
        return -1;
    }

    // 关联共享内存 void *shmat(int shmid, const void *shmaddr, int shmflg);
    void *pShmAddr = shmat(nShmId, NULL, 0);
    if ((void *)(-1) == pShmAddr)
    {
        perror("shmat error");
        return -1;
    }

    // 读写共享内存
    memcpy(pShmAddr, "hello world", sizeof("hello world"));
    printf("shmId: %d, shmAddr: %p, content: %s\n", nShmId, pShmAddr, pShmAddr);

    puts("按任意键:\n");
    getchar();

    // 断开共享内存关联 int shmdt(const void *shmaddr);
    if (-1 == shmdt(pShmAddr))
    {
        perror("shmdt error");
        return -1;
    }

    puts("按任意键:\n");
    getchar();

    // 删除共享内存 int shmctl(int shmid, int cmd, struct shmid_ds *buf);
    if (-1 == shmctl(nShmId, IPC_RMID, NULL))
    {
        perror("shmctl error");
        return -1;
    }

    return 0;
}
在C语言中,共享内存是一种进程间通信(IPC,Inter-Process Communication)机制,它允许两个或更多进程直接访问同一块内存区域,而无需通过文件、管道或其他通信机制。这种技术常用于性能要求高的系统中,因为它消除了消息传递的开销。 以下是创建和使用共享内存的基本步骤: 1. **打开共享内存段**: 使用`shm_open()`函数打开一个已知的共享内存标识符(如果存在),如果没有则创建新的。需要指定共享内存大小、访问权限和一些标志。 ```c int shmid = shm_open("my_shmem", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); ``` 2. **获取共享内存描述符**: 如果`shmid`是负数,表示操作失败,这时通常会检查错误码;成功后可以使用`fcntl()`函数获取共享内存的文件描述符(fd)。 ```c if (shmid < 0) { perror("Error opening shared memory"); } else { int fd = fcntl(shmid, F_GETFD); } ``` 3. **绑定到共享内存**: 使用`mmap()`函数将文件描述符映射到进程地址空间,使得程序可以直接访问共享内存内容。 ```c void *addr; size_t size; if ((addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0)) == MAP_FAILED) { perror("Error mapping shared memory"); } ``` 4. **使用共享内存**: 现在你可以像操作普通内存一样处理`addr`指向的数据了,多个进程可以通过这种方式访问同一块数据。 5. **结束访问并撤销映射**: 当不再需要共享内存时,调用`munmap()`解除进程对内存的映射,并使用`close()`关闭文件描述符。 ```c munmap(addr, size); close(fd); shm_close(shmid); // 可选,视情况是否需要删除共享内存 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值