1. Makefile
2. workQueue.c (如果kernel module名字是workqueue, 会引起问题)
KVERSION = $(shell uname -r)
obj-m = workQueue.o
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
/*
* Copyright (C) 2016,2017 Fernando Vanyo Garcia
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Fernando Vanyo Garcia
* Mi Shuang
*/
#include
#include
#include
#include
#include
#include
#include
#define AUTHOR "Mi Shuang
" #define DESC "Simple example of kernel Work Queues" MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR(AUTHOR); MODULE_DESCRIPTION(DESC); struct work_ent { struct work_struct real_work; struct delayed_work timeout_work; int arg; } work_ent; static void thread_function(struct work_struct *work); struct work_ent *test_wq; static void thread_function(struct work_struct *work_arg) { struct delayed_work *dwork = container_of(work_arg, struct delayed_work, work); struct work_ent *ent = container_of(dwork, struct work_ent, timeout_work); printk(KERN_INFO "%s: the data is: %d\n", __func__, ent->arg); schedule_delayed_work(&ent->timeout_work, HZ); return; } static int __init entry_point(void) { test_wq = kmalloc(sizeof(*test_wq), GFP_KERNEL); INIT_DELAYED_WORK(&test_wq->timeout_work, thread_function); test_wq->arg = 1000; schedule_delayed_work(&test_wq->timeout_work, HZ); return 0; } static void __exit exit_point(void) { //just in case: flush_delayed_work(&test_wq->timeout_work); cancel_delayed_work(&test_wq->timeout_work); kfree(test_wq); return; } module_init(entry_point); module_exit(exit_point);