Linux IPC shm

Linux IPC Shm

Linux 共享内存

Linux 不同进程之间可以通过共享内存的方式完成通信。

Linux 使用共享内存

共享内存相关API

shmget

shmget - allocates a System V shared memory segment

       #include <sys/ipc.h>
       #include <sys/shm.h>

       int shmget(key_t key, size_t size, int shmflg);

shmat/ shmdt

shmat, shmdt - System V shared memory operations, attach/deattach

       #include <sys/types.h>
       #include <sys/shm.h>

       void *shmat(int shmid, const void *shmaddr, int shmflg);

       int shmdt(const void *shmaddr);
shmctl

shmctl - System V shared memory control

       #include <sys/ipc.h>
       #include <sys/shm.h>

       int shmctl(int shmid, int cmd, struct shmid_ds *buf);

Linux Example for shm

Only one basic example. Can refer more information from linux man.

Sender
#include<stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

typedef struct shm_st {
    int available;
    char data[1024];
} SHM_ST;
int main() {
    int shmid;
    SHM_ST *shm = NULL;
    int i = 0;
    //Step 1: create or get one segment of share memory, by key value.
    shmid = shmget((key_t)0x01, sizeof(SHM_ST), 0666|IPC_CREAT);
    if(shmid == -1) {
        printf("shmget failed\n");
    }
    //Step 2: attach shared memory, and send data.
    shm = shmat(shmid, 0, 0);
    if(shm == (void *) -1) {
        printf("shmat failed\n");
    }
    for(int i = 0; i < 3; i++) {
        while(shm->available) {
            sleep(1);
        }
        strcpy(shm->data, "hello");
        shm->available = 1;
    }
    //Step 3: deattach shared memory.
    shmdt(shm);
    return 0;
}
Receiver
#include<stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

typedef struct shm_st {
    int available;
    char data[1024];
} SHM_ST;
int main() {
    int shmid;
    SHM_ST *shm = NULL;
    int i = 0;
    //Step 1: create or get one segment of share memory, by key value.
    shmid = shmget((key_t)0x01, sizeof(SHM_ST), 0666|IPC_CREAT);
    if(shmid == -1) {
        printf("shmget failed\n");
    }
    //Step 2: attach shared memory, and send data.
    shm = shmat(shmid, 0, 0);
    if(shm == (void *) -1) {
        printf("shmat failed\n");
    }
    for(int i = 0; i < 3; i++) {
        while(shm->available == 0) {
            sleep(1);
        }
        printf("shm data %s\n", shm->data);
        shm->available = 0;
    }
    //Step 3: deattach shared memory.
    shmdt(shm);
    return 0;
}

共享内存Kernel实现

Data Structure

shmid_kernel 有一个文件的指针,指向一段共享内存。
在这里插入图片描述

shmget

shmget 是查找或者创建一块share memory。如果相应的key在shm_ids 数组中不存在,newseg创建一个shmid_kernel structure, 并指向新创建一块share memory。

asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
{
	struct shmid_kernel *shp;
	int err, id = 0;

	down(&shm_ids.sem);
	if (key == IPC_PRIVATE) {
		err = newseg(key, shmflg, size);
	} else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
		if (!(shmflg & IPC_CREAT))
			err = -ENOENT;
		else
			err = newseg(key, shmflg, size);
	} else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
		err = -EEXIST;
	}
	up(&shm_ids.sem);

	return err;
}
static int newseg (key_t key, int shmflg, size_t size)
{
	int error;
	struct shmid_kernel *shp;
	int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
	struct file * file;
	char name[13];
	int id;
	shp = ipc_rcu_alloc(sizeof(*shp));
	if (!shp)
		return -ENOMEM;

	shp->shm_perm.key = key;
	shp->shm_flags = (shmflg & S_IRWXUGO);

	shp->shm_perm.security = NULL;
	error = security_shm_alloc(shp);
	if (error) {
		ipc_rcu_free(shp, sizeof(*shp));
		return error;
	}

	if (shmflg & SHM_HUGETLB)
		file = hugetlb_zero_setup(size);
	else {
		sprintf (name, "SYSV%08x", key);
		file = shmem_file_setup(name, size, VM_ACCOUNT);
	}
	shp->shm_cprid = current->tgid;
	shp->shm_lprid = 0;
	shp->shm_atim = shp->shm_dtim = 0;
	shp->shm_ctim = get_seconds();
	shp->shm_segsz = size;
	shp->shm_nattch = 0;
	shp->id = shm_buildid(id,shp->shm_perm.seq);
	shp->shm_file = file;
	file->f_dentry->d_inode->i_ino = shp->id;
}

shmat

shmat 主要是把shmget 创建的内存文件在当前进程的逻辑内存地址做一次映射。进程对这个地址的操作最终会反映到物理内存上。
在这里插入图片描述

long sys_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
{
	shp = shm_lock(shmid);
	err = shm_checkid(shp,shmid);
	if (ipcperms(&shp->shm_perm, acc_mode)) {
		shm_unlock(shp);
		err = -EACCES;
		goto out;
	}
	err = security_shm_shmat(shp, shmaddr, shmflg);
		
	file = shp->shm_file;
	size = i_size_read(file->f_dentry->d_inode);
	shp->shm_nattch++;
	shm_unlock(shp);
	down_write(&current->mm->mmap_sem);
	if (addr && !(shmflg & SHM_REMAP)) {
		user_addr = ERR_PTR(-EINVAL);
		if (find_vma_intersection(current->mm, addr, addr + size))
			goto invalid;
		/*
		 * If shm segment goes below stack, make sure there is some
		 * space left for the stack to grow (at least 4 pages).
		 */
		if (addr < current->mm->start_stack &&
		    addr > current->mm->start_stack - size - PAGE_SIZE * 5)
			goto invalid;
	}
	user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
	*raddr = (unsigned long) user_addr;
	err = 0;
	if (IS_ERR(user_addr))
		err = PTR_ERR(user_addr);
out:
	return err;
}

shmdt

shmdt 作用跟shmat的作用相反,将本进程内的逻辑内存地址和共享内存的实际物理地址做unmap。

asmlinkage long sys_shmdt(char __user *shmaddr)
{
	do_munmap();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值