linux内核中的kfifo,Linux内核中kfifo使用

/*

* Sample kfifo byte stream implementation

*

* Copyright (C) 2010 Stefani Seibold *

* Released under the GPL version 2 only.

*

*/

#include

#include

#include

#include

#include

#include

#include

#include

/*

* 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 ");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值