本文关注的重点是,避免内核线程的无效唤醒,并且主要是关注消费者线程的设计。
因此,为了省事,这里关与生产者,消费者本身的处理流程可能不够严密。
1. 生产者
一个内核线程,每生产一个商品后,就唤醒消费者,然后自己睡眠1秒钟。
2. 消费者
一个内核线程,每当被唤醒后,就消费商品,然后进入睡眠。
对于消费者线程的这种设计,有几个好处:响应快,平时不占任何cpu。
但这种设计有一点要注意,那就是要避免线程的无效唤醒。如何实现,看看消费者线程的代码就知道了。
/*
* kernel programming test code
*
* Copyright (C) 2014 Sun Mingbao <sunmingbao@126.com>
* Dual licensed under the MIT and/or GPL licenses.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
MODULE_AUTHOR("Sun Mingbao <sunmingbao@126