哈希----字符串----time33

//此处只是获得了字符串的hash值,但是该如何散列到hash表中呢?哪个算法会好些?!


1
//在处理以字符串为键值的哈希时,times33哈希算法有着极快的计算效率和很好的哈希分布 2 3 //小写英文单词 4 #if 1 5 unsigned int str_hash(const char* key) 6 { 7 unsigned int hashval = 0; 8 while(*key) 9 { 10 hash = ((hash << 5) + hash) + (unsigned int)*key++; 11 } 12 13 return hash; 14 } 15 16 #else 17 18 unsigned int str_has(const char* key) 19 { 20 unsigned int hashval = 0; 21 while(*key) 22 { 23 hash = (hash << 7 + hash) + (unsigned int)*key++; 24 } 25 26 return hash; 27 } 28 29 #endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些创建Linux字符设备驱动的指导。在这个过程中,我将涉及一些基本的Linux内核编程知识。 首先,您需要了解什么是Linux字符设备和watchdog。Linux字符设备是一种与字符流进行交互的设备,例如终端、串口、打印机等。watchdog是一种硬件计时器,可以在系统出现问题时触发复位操作。 以下是创建Linux字符设备驱动的基本步骤: 1. 创建字符设备驱动的源文件。在这个文件中,你需要定义一些函数,如 file_operations 结构体中的 read 和 write 函数,以及初始化和清理函数。你需要使用字符设备驱动框架中的 API 函数来注册字符设备,分配和释放设备号等。 2. 实现字符设备的 read 和 write 函数。在这些函数中,你需要实现读写设备的逻辑。在这个特定的案例中,你需要在 write 函数中设置 watchdog 定时器的时间,以及在 read 函数中触发复位操作。 3. 在初始化函数中初始化 watchdog 定时器,并启动它。 4. 在清理函数中清理 watchdog 定时器,并停止它。 这是一个基本的框架,你需要根据你的需求来调整它。 接下来是一些参考代码,可以帮助你开始编写代码: ```c #include <linux/module.h> #include <linux/device.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/miscdevice.h> #include <linux/watchdog.h> #define DEVICE_NAME "watchdog" #define MAX_TIMEOUT 3600 static struct miscdevice watchdog_miscdev; static struct timer_list watchdog_timer; static int watchdog_timeout = MAX_TIMEOUT; static int watchdog_enabled = 0; static void watchdog_timeout_handler(unsigned long data) { printk(KERN_INFO "Watchdog: Timer expired, triggering reset\n"); watchdog_enabled = 0; watchdog_miscdev.minor = 0; misc_deregister(&watchdog_miscdev); watchdog_miscdev.minor = MISC_DYNAMIC_MINOR; misc_register(&watchdog_miscdev); watchdog_enabled = 1; watchdog_timer.expires = jiffies + watchdog_timeout * HZ; add_timer(&watchdog_timer); machine_restart(NULL); } static ssize_t watchdog_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { if (!watchdog_enabled) { printk(KERN_INFO "Watchdog: Device not enabled\n"); return -EIO; } printk(KERN_INFO "Watchdog: Triggering reset\n"); machine_restart(NULL); return 0; } static ssize_t watchdog_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { unsigned long timeout; if (count != sizeof(timeout)) { printk(KERN_INFO "Watchdog: Invalid write size\n"); return -EINVAL; } if (!watchdog_enabled) { printk(KERN_INFO "Watchdog: Device not enabled\n"); return -EIO; } if (copy_from_user(&timeout, buf, sizeof(timeout))) { printk(KERN_INFO "Watchdog: Failed to copy timeout value from user\n"); return -EFAULT; } if (timeout < 1 || timeout > MAX_TIMEOUT) { printk(KERN_INFO "Watchdog: Timeout value out of range\n"); return -EINVAL; } watchdog_timeout = timeout; mod_timer(&watchdog_timer, jiffies + watchdog_timeout * HZ); printk(KERN_INFO "Watchdog: Timeout set to %lu seconds\n", timeout); return count; } static int watchdog_open(struct inode *inode, struct file *file) { if (watchdog_enabled) { printk(KERN_INFO "Watchdog: Device already enabled\n"); return -EBUSY; } printk(KERN_INFO "Watchdog: Enabling device\n"); watchdog_timer.function = watchdog_timeout_handler; watchdog_timer.expires = jiffies + watchdog_timeout * HZ; add_timer(&watchdog_timer); watchdog_enabled = 1; return 0; } static int watchdog_release(struct inode *inode, struct file *file) { if (!watchdog_enabled) { printk(KERN_INFO "Watchdog: Device not enabled\n"); return -EIO; } printk(KERN_INFO "Watchdog: Disabling device\n"); del_timer_sync(&watchdog_timer); watchdog_enabled = 0; return 0; } static const struct file_operations watchdog_fops = { .owner = THIS_MODULE, .read = watchdog_read, .write = watchdog_write, .open = watchdog_open, .release = watchdog_release, }; static int __init watchdog_init(void) { watchdog_miscdev.minor = MISC_DYNAMIC_MINOR; watchdog_miscdev.name = DEVICE_NAME; watchdog_miscdev.fops = &watchdog_fops; misc_register(&watchdog_miscdev); printk(KERN_INFO "Watchdog: Device registered\n"); return 0; } static void __exit watchdog_exit(void) { misc_deregister(&watchdog_miscdev); printk(KERN_INFO "Watchdog: Device unregistered\n"); } module_init(watchdog_init); module_exit(watchdog_exit); MODULE_LICENSE("GPL"); ``` 这个代码实现了一个基本的 watchdog 设备驱动,它可以在 write 函数中设置定时器时间,在 read 函数中触发复位操作。当设备被打开时,定时器会被启动,在定时器时间到达时触发复位操作。当设备被关闭时,定时器会被停止。 请注意,这个代码没有经过完整的测试,您需要根据您的需求进行修改和测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值