sys v从内核模块到用户空间进程的共享内存 [英] sys v shared memory from kernel module to user space process

查看:247
本文介绍了sys v从内核模块到用户空间进程的共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是linux内核模块开发中的新手,我正在寻找从内核模块到用户空间进程共享内存段的方法,以避开复制数据的延迟.

i am new in linux kernel module developpement and i am searching for sharing a memory segment from kernel module to user space process to escape latency of copying data.

我正在使用sys v共享内存api,当我在两个进程之间共享内存时,它可以正常工作,但是我无法在进程和内核模块之间共享内存.

i am using the sys v shared memory api, when i share memory between two process it's work fine, but i am not able to share memory between process and kernel module.

bellow是我的内核模块和用户空间应用程序的代码

bellow is my code of the kernel module and the user space app

服务器端:模块

#include <linux/module.h> // init_module, cleanup_module //
#include <linux/kernel.h> // KERN_INFO //
#include <linux/types.h> // uint64_t //
#include <linux/kthread.h> // kthread_run, kthread_stop //
#include <linux/delay.h> // msleep_interruptible //
#include <linux/syscalls.h> // sys_shmget //

#define BUFSIZE 100
#define SHMSZ BUFSIZE*sizeof(char)
key_t KEY = 5678;

static struct task_struct *shm_task = NULL;


static char *shm = NULL;
static int shmid;

static int run_thread( void *data )
{
    char strAux[BUFSIZE];
    shmid = sys_shmget(KEY, SHMSZ, IPC_CREAT | 0666);
    if( shmid < 0 )
    {
        printk( KERN_INFO "SERVER : Unable to obtain shmid\n" );
        return -1;
    }
    shm = sys_shmat(shmid, NULL, 0);
    if( !shm )
    {
        printk( KERN_INFO "SERVER : Unable to attach to memory\n" );
        return -1;
    }
    strncpy( strAux, "hello world from kernel module", BUFSIZE );
    memcpy(shm, strAux, BUFSIZE);
    return 0;
}

int init_module()
{
    printk( KERN_INFO "SERVER : Initializing shm_server\n" );
    shm_task = kthread_run( run_thread, NULL, "shm_server" );
    return 0;
}

void cleanup_module()
{
    int result;
    printk( KERN_INFO "SERVER : Cleaning up shm_server\n" );
    result = kthread_stop( shm_task );
    if( result < 0 )
    {
        printk( KERN_INFO "SERVER : Unable to stop shm_task\n" );
    }
    result = sys_shmctl( shmid, IPC_RMID, NULL );
    if( result < 0 )
    {
        printk( KERN_INFO
        "SERVER : Unable to remove shared memory from system\n" );
    }

}


MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( " MBA" );
MODULE_DESCRIPTION( "Shared memory  server" );

客户端:流程

#include <sys/ipc.h> // IPC_CREAT, ftok //
#include <sys/shm.h> // shmget, ... //
#include <sys/sem.h> // semget, semop //
#include <stdio.h> // printf //
#include <string.h> // strcpy //
#include <stdint.h> // uint64_t //

#define BUFSIZE 4096
key_t KEY = 5678;

int main(int argc, char *argv[]) {
    int shmid, result;
    char *shm = NULL;

    shmid = shmget(KEY, BUFSIZE, 0666);
    if (shmid == -1) {
        perror("shmget");
        exit(-1);
    }
    shm = shmat(shmid, NULL, 0);
    if (!shm) {
        perror("shmat");
        exit(-1);
    }

    printf("%s\n", shm);
    result = shmdt(shm);
    if (result < 0) {
        perror("shmdt");
        exit(-1);
    }
}

任何建议或文档都可以提供帮助.

any suggestion or document can help.

推荐答案

内核不打算使用系统调用:它们仅用于用户程序.另外,sys v内存共享不太可能适用于内核线程.

System calls are not intended for being use by the kernel: they are for user programs only. Also, it is unlikely that is sys v memory sharing works for kernel threads.

内核和内核模块具有与用户进行交互的机制 空间.为了共享内存,您的内核模块可以为其实现字符设备和mmap方法,该设备将内核分配的内存映射到用户.请参见 Linux设备驱动程序(3d版)第15章中的mmap实现示例.

Kernel and kernel modules have their own mechanism for interract with user space. For sharing memory, your kernel module may implement character device and mmap method for it, which maps kernel's allocated memory to user. See example of such mmap implementation in Linux Device Drivers(3d edition), Chapter 15.

这篇关于sys v从内核模块到用户空间进程的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆