进程间通信:KPIPE

简介: 源代码:#include #include #include #include #include #include #include #include #include #include //module_param(dev_major, int, S_...

源代码:

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/module.h>  
#include <linux/fs.h>  
#include <linux/errno.h>  
#include <linux/mm.h>  
#include <linux/cdev.h>  

//module_param(dev_major, int, S_IRUGO);  
#define MY_DEV_MAJOR        260     /*预设的mem的主设备号*/  
static int dev_major = 0; //MY_DEV_MAJOR;  

#define MY_DEV_NR_DEVS      2       /*设备数*/  

#define CHAR_DEV_NAME       "kpipe"
  
/*
'k'为幻数,要按照Linux内核的约定方法为驱动程序选择ioctl编号,
应该首先看看include/asm/ioctl.h和Documentation/ioctl-number.txt这两个文件.

对幻数的编号千万不能重复定义,如ioctl-number.txt已经说明‘k'的编号已经被占用的范围为:
'k'    00-0F    linux/spi/spidev.h    conflict!
'k'    00-05    video/kyro.h        conflict!
所以我们在这里分别编号为0x1a和0x1b
*/
#define CMD_MAGIC   'k'
#define MEM_CMD1    _IO(CMD_MAGIC, 0x1a)
#define MEM_CMD2    _IO(CMD_MAGIC, 0x1b)
#define MEM_CMD3    _IO(CMD_MAGIC, 0x1c)

#if 1
#define MAX_UID_NUM     32

typedef struct tagUSER_MSG_INFO
{
    int  uid;
    int  param1;
    int  param2;
    int  param3;
    char *msg;
}USER_MSG_INFO;

typedef struct tagUSER_QUEUE_INFO
{
    int  rd_ptr;
    int  wt_ptr;
    int  max_msg;
    int  msg_max_len;
    char *msg_queue;
}USER_QUEUE_INFO;

USER_QUEUE_INFO usr_queue[MAX_UID_NUM];

static int user_queue_init(void)
{
    int i;

    for(i = 0; i < MAX_UID_NUM; i++) {
        memset(&usr_queue[i], 0, sizeof(USER_QUEUE_INFO));
    }
    return 0;
}

static int user_queue_alloc(int uid, int max_msg, int msg_max_len)
{
    int total_size;
    
    if (uid >= MAX_UID_NUM) return -1;
    if (max_msg < 1 || msg_max_len < 1) return -1;
    if (usr_queue[uid].msg_queue != NULL) return -2;

    total_size = max_msg * msg_max_len;
    usr_queue[uid].msg_queue = kmalloc(total_size, GFP_KERNEL);  
    if (NULL == usr_queue[uid].msg_queue) return -3;

    usr_queue[uid].max_msg = max_msg;
    usr_queue[uid].msg_max_len = msg_max_len;
    usr_queue[uid].rd_ptr = 0;
    usr_queue[uid].wt_ptr = 0;
    
    return 0;
}

static int user_queue_push(USER_MSG_INFO *usr_msg)
{
    int uid;
    char *ptr;
    
    if (NULL == usr_msg) return -1;
    uid = usr_msg->uid;
    if (usr_queue[uid].msg_queue == NULL) return -2;

    if ( (usr_queue[uid].wt_ptr + 1) % usr_queue[uid].max_msg == usr_queue[uid].rd_ptr)
        return -3; // full

    ptr = usr_queue[uid].msg_queue + usr_queue[uid].wt_ptr * usr_queue[uid].msg_max_len;
    memset(ptr, 0, usr_queue[uid].msg_max_len);
    copy_from_user((int *)ptr, (int *)usr_msg->msg, usr_queue[uid].msg_max_len);
    usr_queue[uid].wt_ptr = (usr_queue[uid].wt_ptr + 1) % usr_queue[uid].max_msg;
        
    return 0;
}

static int user_queue_pop(USER_MSG_INFO *usr_msg)
{
    int uid;
    char *ptr;
    
    if (NULL == usr_msg) return -1;
    uid = usr_msg->uid;
    if (usr_queue[uid].msg_queue == NULL) return -2;

    if ( usr_queue[uid].rd_ptr == usr_queue[uid].wt_ptr)
        return -3; // empty

    ptr = usr_queue[uid].msg_queue + usr_queue[uid].rd_ptr * usr_queue[uid].msg_max_len;
    copy_to_user((int *)usr_msg->msg, (int *)ptr, usr_queue[uid].msg_max_len);
    usr_queue[uid].rd_ptr = (usr_queue[uid].rd_ptr + 1) % usr_queue[uid].max_msg;
        
    return 0;
}

static int user_queue_destroy(void)
{
    int i;

    for(i = 0; i < MAX_UID_NUM; i++) {
        usr_queue[i].max_msg = 0;
        if (NULL != usr_queue[i].msg_queue)
            kfree(usr_queue[i].msg_queue);
        usr_queue[i].msg_queue = NULL;
    }
    return 0;
}


#endif

#if 1
int my_dev_open(struct inode *inode, struct file *filp)  
{  
    return 0;   
}  
  
int my_dev_release(struct inode *inode, struct file *filp)  
{  
    return 0;  
}  
static int my_dev_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
{    
    USER_MSG_INFO user_msg;
    int ret = 0;
    
    switch(cmd)
    {
        case MEM_CMD1: // init mq for uid
            if(copy_from_user(&user_msg,  (int *)arg, sizeof(USER_MSG_INFO))) 
                return -EFAULT;
            
            ret = user_queue_alloc(user_msg.uid, user_msg.param1, user_msg.param2);
            break;
            
        case MEM_CMD2: // send msg to uid
            if(copy_from_user(&user_msg,  (int *)arg, sizeof(USER_MSG_INFO))) 
                return -EFAULT;

            ret = user_queue_push(&user_msg);
            break;
        
        case MEM_CMD3: // get msg from uid
            if(copy_from_user(&user_msg,  (int *)arg, sizeof(USER_MSG_INFO))) 
                return -EFAULT;
            
            ret = user_queue_pop(&user_msg);
            break;
    }
    
    return ret;
}

static const struct file_operations my_dev_fops =  
{  
    .owner = THIS_MODULE,  
    .unlocked_ioctl = my_dev_ioctl,
    .open = my_dev_open,  
    .release = my_dev_release,  
};  

struct class *pclass = NULL;  
  
struct cdev my_dev;   

static int my_dev_init(void)  
{  
    int result;  
    dev_t devno = MKDEV(dev_major, 0);  

    if (dev_major) { /* 静态申请设备号*/  
        result = register_chrdev_region(devno, 2, CHAR_DEV_NAME);  
    } else { /* 动态分配设备号 */  
        result = alloc_chrdev_region(&devno, 0, 2, CHAR_DEV_NAME);  
        dev_major = MAJOR(devno);  
    }   

    if (result < 0)  
        return result;  

    cdev_init(&my_dev, &my_dev_fops);  
    my_dev.owner = THIS_MODULE;  
    my_dev.ops = &my_dev_fops;  
    cdev_add(&my_dev, MKDEV(dev_major, 0), MY_DEV_NR_DEVS);  

    pclass = class_create(THIS_MODULE, CHAR_DEV_NAME);  
    if (IS_ERR(pclass))  
    {  
        printk("class_create failed!/n");  
        goto failed;  
    }  

    user_queue_init();
    device_create(pclass, NULL, devno, NULL, CHAR_DEV_NAME);  
    return 0;  

failed:   
    cdev_del(&my_dev);
    unregister_chrdev_region(devno, 1);  
    return result;  
}  
  
static void my_dev_exit(void)  
{  
    device_destroy(pclass, MKDEV(dev_major, 0));  
    class_destroy(pclass);
    
    cdev_del(&my_dev);
    unregister_chrdev_region(MKDEV(dev_major, 0), 2); 
    user_queue_destroy();
}  

#endif

MODULE_AUTHOR("derek.yi");  
MODULE_LICENSE("GPL");  
  
module_init(my_dev_init);  
module_exit(my_dev_exit);  

 

测试代码:

#include <stdio.h>  
#include <fcntl.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define CMD_MAGIC   'k'
#define MEM_CMD1    _IO(CMD_MAGIC, 0x1a) // init
#define MEM_CMD2    _IO(CMD_MAGIC, 0x1b) // write
#define MEM_CMD3    _IO(CMD_MAGIC, 0x1c) // read

typedef struct tagUSER_MSG_INFO
{
    int  uid;
    int  param1;
    int  param2;
    int  param3;
    char *msg;
}USER_MSG_INFO;


#define _APP_001_

#ifdef _APP_001_
int main()
{
    int fd;
    int ret;
    char buff[256];
    USER_MSG_INFO usr_msg;

    fd = open("/dev/kpipe", O_RDWR);
    if( fd < 0 ) {
        printf("open kpipe WRONG!\n");
        return 0;
    }

    usr_msg.uid = 0;
    usr_msg.param1 = 64; // max msg
    usr_msg.param2 = 128; // max len
    
    ret = ioctl(fd, MEM_CMD1, &usr_msg);
    printf("ioctl: ret=%d\n", ret);

    usr_msg.msg = &buff[0];
    sprintf(buff, "hello,pipe\n");
    ret = ioctl(fd, MEM_CMD2, &usr_msg);
    printf("ioctl: ret=%d\n", ret);

    usr_msg.msg = &buff[0];
    memset(buff, 0, 256);
    ret = ioctl(fd, MEM_CMD3, &usr_msg);
    printf("ioctl: ret=%d rdata=%s\n", ret, buff);
    
    close(fd);
    return 0;
}

#endif

 

目录
相关文章
|
存储 并行计算 算法
C++进程间通信之共享内存
C++进程间通信之共享内存
756 0
|
5月前
|
消息中间件 负载均衡 安全
进程间通信
数据传输:一个进程需要将它的数据发送给另一个进程资源共享:多个进程之间共享同样的资源。通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。进程间通信的本质:让不同的进程先看到同一份资源,但是这份资源不能由双方进程提供,而通常是由操作系统所提供!
38 9
进程间通信
|
存储 消息中间件 Linux
IPC(进程间通信)(上)
IPC(进程间通信)
71 0
|
API
IPC(进程间通信)(下)
IPC(进程间通信)(下)
51 0
|
Unix
进程间通信(一)
进程间通信
68 0
|
消息中间件
进程间通信(非常实用)
进程间通信(非常实用)
|
Linux
2.10~2.29 进程间通信(下)
2.10~2.29 进程间通信
108 0
2.10~2.29 进程间通信(下)
|
Linux
2.10~2.29 进程间通信(上)
2.10~2.29 进程间通信
89 0
2.10~2.29 进程间通信(上)
|
开发者
进程间通信 | 学习笔记
快速学习进程间通信,介绍了进程间通信系统机制, 以及在实际应用过程中如何使用。