![运行 dmesg 命令时无法查看设备注册](https://linux22.com/image/197053/%E8%BF%90%E8%A1%8C%20dmesg%20%E5%91%BD%E4%BB%A4%E6%97%B6%E6%97%A0%E6%B3%95%E6%9F%A5%E7%9C%8B%E8%AE%BE%E5%A4%87%E6%B3%A8%E5%86%8C.png)
我正在编译下面的代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
unsigned long js,je,diff;
static int sample_open(struct inode *inode, struct file *file)
{
pr_info("I have been awoken\n");
return 0;
}
static int sample_close(struct inode *inodep, struct file *filp)
{
pr_info("Sleepy time\n");
return 0;
}
static ssize_t sample_write(struct file *file, const char __user *buf,
size_t len, loff_t *ppos)
{
pr_info("Yummy - I just ate %d bytes\n", len);
return 1;
}
static ssize_t sample_read(struct file *filp, char __user *buf,
size_t count, loff_t *f_pos)
{
pr_info("I'm reading something\n");
return 0;
}
static const struct file_operations sample_fops = {
.owner = THIS_MODULE,
.write = sample_write,
.open = sample_open,
.release = sample_close,
.read = sample_read,
};
struct miscdevice sample_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "lab3",
.fops = &sample_fops,
};
static int hello_init(void)
{
js = jiffies;
printk(KERN_ALERT "Hello, world\n");
printk("This is the tick time %u", 1000/HZ);
return 0;
}
static void hello_exit(void)
{
je = jiffies;
diff = (je - js)* 1000/HZ;
printk(KERN_ALERT "Goodbye, cruel world\n");
printk("This is time difference in seconds %ld %ld s\n", diff / 1000, diff % 1000);
}
static int __init misc_init(void)
{
int error;
error = misc_register(&sample_device);
if (error) {
pr_err("can't misc_register :(\n");
return error;
}
hello_init();
return 0;
}
static void __exit misc_exit(void)
{
misc_deregister(&sample_device);
hello_exit();
}
module_init(misc_init)
module_exit(misc_exit)
make
很好,我正在按预期收到输出。但是当我运行时,dmesg | tail -1
我看不到注册的设备号。另外,当我运行时,cat /proc/misc
我会看到以下输出
58 mymisc1
232 kvm
235 autofs
234 btrfs-control
59 cpu_dma_latency
227 mcelog
236 device-mapper
223 uinput
1 psaux
196 vfio
200 tun
60 udmabuf
237 loop-control
61 lightnvm
183 hw_random
228 hpet
229 fuse
62 ecryptfs
231 snapshot
242 rfkill
63 vga_arbiter
我也应该lab3
在这里看到我的。我缺少什么建议吗?