您的位置:首页 > 运维架构 > Linux

Linux内核中kfifo使用

2015-06-17 18:02 465 查看
/*
 * Sample kfifo byte stream implementation
 *
 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
 *
 * Released under the GPL version 2 only.
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/mutex.h>
#include <linux/kfifo.h>
#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/delay.h>

/*
 * This module shows how to create a byte stream fifo.
 */

/* fifo size in elements (bytes) */
#define FIFO_SIZE   32

/* name of the proc entry */
#define PROC_FIFO   "bytestream-fifo"

/* lock for procfs read access */
static DEFINE_MUTEX(read_lock);

/* lock for procfs write access */
static DEFINE_MUTEX(write_lock);

/*
 * define DYNAMIC in this example for a dynamically allocated fifo.
 *
 * Otherwise the fifo storage will be a part of the fifo structure.
 */
#if 1
#define DYNAMIC
#endif

#ifdef DYNAMIC
static struct kfifo test;
#else
static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
#endif

static const unsigned char expected_result[FIFO_SIZE] = {
     3,  4,  5,  6,  7,  8,  9,  0,
     1, 20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 31, 32, 33, 34,
    35, 36, 37, 38, 39, 40, 41, 42,
};

static int __init testfunc(void)
{
    unsigned char   buf[32]={'0'};
    unsigned char   i, j;
    unsigned int    ret;

    printk(KERN_INFO "byte stream fifo test start\n");

    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO"1111not empty \n!");
    }else{
            printk(KERN_INFO "empty ! \n");
    }
    /* put string into the fifo */
    kfifo_in(&test, "hello",5);

    /* get max of 2 elements from the fifo */
    ret = kfifo_in(&test, "121212121212121212121212121212121212",36);
    printk(KERN_INFO "ret: %d buf = %s\n" , ret,buf);

    if(!kfifo_is_full(&test)){

            printk(KERN_INFO"2222not kfifo_is_full \n!");
    }else{
            printk(KERN_INFO "kfifo_is_full ! \n");
    }
    /* and put it back to the end of the fifo */
    ret = kfifo_out(&test,buf,9);
    printk(KERN_INFO "ret: %d buf = %s\n", ret,buf);
    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO"2222not empty ! \n");
    }else{
            printk(KERN_INFO "empty ! \n");
    }
  memset(buf,0,32);
    ret = kfifo_out(&test,buf,8);
    printk(KERN_INFO "test passed buf = %s\n",buf);

  memset(buf,0,32);
    ret = kfifo_out(&test,buf,8);
    printk(KERN_INFO "test passed buf = %s\n",buf);

    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO "3333333333not empty ! \n");
    }else{
            printk(KERN_INFO "empty ! \n");
    }

    return 0;
}

static ssize_t fifo_write(struct file *file, const char __user *buf,
                        size_t count, loff_t *ppos)
{
    int ret;
    unsigned int copied;

    if (mutex_lock_interruptible(&write_lock))
        return -ERESTARTSYS;

    ret = kfifo_from_user(&test, buf, count, &copied);

    mutex_unlock(&write_lock);

    return ret ? ret : copied;
}

static ssize_t fifo_read(struct file *file, char __user *buf,
                        size_t count, loff_t *ppos)
{
    int ret;
    unsigned int copied;

    if (mutex_lock_interruptible(&read_lock))
        return -ERESTARTSYS;

    ret = kfifo_to_user(&test, buf, count, &copied);

    mutex_unlock(&read_lock);

    return ret ? ret : copied;
}

static const struct file_operations fifo_fops = {
    .owner      = THIS_MODULE,
    .read       = fifo_read,
    .write      = fifo_write,
    .llseek     = noop_llseek,
};

static int productor_thread(void *p){

    unsigned char mac[7]={0x00};
    int i = 0;
    unsigned int    ret;

    while(i<100){

        mac[0] = i;
        mac[1] = i+1;
        mac[2] = i+2;
        mac[3] = i+3;
        mac[4] = i+4;
        mac[5] = i+5;
        mac[6] = i+6;

         ret = kfifo_in(&test,mac,7);

     i++;

    msleep(1000);

  }

    return 0;
}

static int consumer_thread(void *p){

    unsigned char mac[7]={0x00};
    unsigned int    ret;
    int i = 0;
    while(i<10){

        ret = kfifo_out(&test,mac,7);

        msleep(1000);
        if(kfifo_is_empty(&test)){
            i++;
        }else{
                printk(KERN_INFO "mac =0x%x:0x%x:0x%x:0x%x:0x%x:0x%x:0x%x \n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],mac[6]);
        }

    }

    printk("iiiiiiiiiiiiiiiiiiiiiiii=%d \n",i);
    return 0;
}

static struct task_struct *ps_tsks1;
static struct task_struct *ps_tsks2;

static int __init example_init(void)
{
#ifdef DYNAMIC
    int ret;

    ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
    if (ret) {
        printk(KERN_ERR "error kfifo_alloc\n");
        return ret;
    }
#else
    INIT_KFIFO(test);
#endif

    ps_tsks1 = kthread_run(productor_thread,(void *)(unsigned long)NULL,"productor_thread");

    ps_tsks2 = kthread_run(consumer_thread,(void *)(unsigned long)NULL,"consumer_thread");

/*
    if (testfunc() < 0) {
#ifdef DYNAMIC
        kfifo_free(&test);
#endif
        return -EIO;
    }
*/

    if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
#ifdef DYNAMIC
        kfifo_free(&test);
#endif
        return -ENOMEM;
    }
    return 0;
}

static void __exit example_exit(void)
{

    //kthread_stop(ps_tsks1);
    //kthread_stop(ps_tsks2);
    remove_proc_entry(PROC_FIFO, NULL);
#ifdef DYNAMIC
    kfifo_free(&test);
#endif
}

module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: