想来都有点恨自己,前几个月遇到的"难题".还是没有解决.一直卡死,又找不出原因.

吐个槽,那些只贴代码不附上运行结果的toturial, 我表示...我就不明白,既然有些bloger代码都给出来了

让我这种渣渣看一下肿么用的会怎么样?看一下你运行结果会怎么样?

说明:额...这个问题"贴"没有附上运行结果,是因为直接会卡死我的Linux主机,然后我压根没办法截屏给大家看,也没办法debug. 希望谅解

------------------------------------------------

问题代码一:

/***********************************************************
code writer : EOF
code date   : 2014.09.02
code file   : proc_time_delay.c
e-mail      : jasonleaster@gmail.comcode purpose:This code is programmed for how to delay 1 second by
jiffies and HZ.If there is something wrong with my code, please touch
me by e-mail. Thank you.************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>#include <linux/fs.h>
#include <linux/proc_fs.h>    /* for procfs */
#include <linux/seq_file.h>   /* for 'struct seq_file' */
#include <linux/types.h>#include <linux/jiffies.h>  /* for jiffies */#define PROC_NAME "delay_one_second"MODULE_AUTHOR("Jason Leaster");
MODULE_LICENSE("Dual BSD/GPL");static int proc_demo_seq_show(struct seq_file* sfile,void* v)
{int tmp = 0;unsigned long jif = jiffies;unsigned long one_second_later = jiffies + HZ;for(tmp = 0; tmp < 10;tmp++){while(time_after(one_second_later,jif)){jif = jiffies;}      one_second_later = jif + HZ;seq_printf(sfile,"Hello world! jiffies:%lu\n",jif);}return 0;
}static void* proc_demo_seq_start(struct seq_file* sfile,loff_t *pos)
{return NULL;
}static void proc_demo_seq_stop(struct seq_file* sfile,void* v)
{/* Nothing to be done. */
}static void* proc_demo_seq_next(struct seq_file* sfile,void* v,loff_t* pos)
{return NULL;
}static struct seq_operations proc_demo_ops =
{.start =  proc_demo_seq_start,.next   =  proc_demo_seq_next,.stop    =  proc_demo_seq_stop,.show    =  proc_demo_seq_show,
};static int proc_demo_open(struct inode* inode, struct file* filp)
{return single_open(filp,&proc_demo_seq_show,NULL);
}struct file_operations proc_demo_fops =
{.owner =  THIS_MODULE,.open   =  proc_demo_open,.read    =  seq_read,.release= seq_release,
};int proc_demo_init(void)
{struct proc_dir_entry * entry = NULL;entry = proc_create(PROC_NAME,0,NULL,&proc_demo_fops);if(!entry){printk(KERN_ALERT "line:%d proc_create failed!",__LINE__);}return 0;
}void proc_demo_exit(void)
{/***       The second parameter of 'remove_proc_entry()' is ** a pointer which point to parent directory.We create our** proc-entry-point in /proc/, so we pass NULL into it.*/remove_proc_entry(PROC_NAME,NULL);
}module_init(proc_demo_init);
module_exit(proc_demo_exit);

问题代码2:

/***********************************************************
code writer : EOF
code date   : 2014.09.02
code file   : proc_time_delay.c
e-mail      : jasonleaster@gmail.comcode purpose:This code is programmed for how to delay by kernel
timer.If there is something wrong with my code, please touch
me by e-mail. Thank you.************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>#include <linux/fs.h>
#include <linux/proc_fs.h>    /* for procfs */
#include <linux/seq_file.h>   /* for 'struct seq_file' */
#include <linux/types.h>#include <linux/jiffies.h>  /* for jiffies */
#include <linux/timer.h>  /* for timer*/#include <linux/sched.h>
#include <linux/slab.h>#define PROC_NAME "delay_kernel_timer"MODULE_AUTHOR("Jason Leaster");
MODULE_LICENSE("Dual BSD/GPL");#define LOOP 5/*
**  First of all, you should make abstract a model
** for our device driver and include the timer.
*/struct timer_delay_model
{struct timer_list timer;wait_queue_head_t wait;unsigned long prevjiffies;struct seq_file   * output;int loops ;/*  ... */
};void jif_timer_fn(unsigned long arg)
{struct timer_delay_model* p_model = (struct timer_delay_model*)arg;unsigned long jif = jiffies;seq_printf(p_model->output,"Hello world! jiffies:%lu\n",jif);printk(KERN_ALERT "in jif_timer_fc %d\n",p_model->loops);/*if(--(p_model->loops)){p_model->timer.expires += HZ;p_model->prevjiffies = jif;add_timer(&p_model->timer);}else{wake_up_interruptible(&p_model->wait);}
*/p_model->loops = 0;
}static int proc_demo_seq_show(struct seq_file* sfile,void* v)
{unsigned long jif = jiffies;struct timer_delay_model* p_model;p_model = kmalloc(sizeof(struct timer_delay_model),GFP_KERNEL);if(!p_model){printk(KERN_ALERT "kmalloc error in %d %s\n",__LINE__,__FUNCTION__);return -ENOMEM;}else{printk(KERN_ALERT "start to initialization %lu\n",jiffies);init_timer(&p_model->timer);init_waitqueue_head(&p_model->wait);p_model->prevjiffies  = jif;p_model->output       = sfile;p_model->loops      = LOOP;p_model->timer.data  = (unsigned long) sfile;p_model->timer.function = jif_timer_fn;p_model->timer.expires   = jif + HZ;// delay 1 secondprintk(KERN_ALERT "add_timer  ing %lu\n",jiffies);add_timer(&p_model->timer);}printk(KERN_ALERT "add_timer  finished %lu\n",jiffies);//    wait_event_interruptible(p_model->wait,!(p_model->loops));while(time_after(p_model->prevjiffies + HZ,jif)){jif = jiffies;}       printk(KERN_ALERT "wait finished\n");kfree(p_model);if (signal_pending(current))return -ERESTARTSYS;return 0;
}static void* proc_demo_seq_start(struct seq_file* sfile,loff_t *pos)
{return NULL;
}static void proc_demo_seq_stop(struct seq_file* sfile,void* v)
{/* Nothing to be done. */
}static void* proc_demo_seq_next(struct seq_file* sfile,void* v,loff_t* pos)
{return NULL;
}static struct seq_operations proc_demo_ops =
{.start =  proc_demo_seq_start,.next   =  proc_demo_seq_next,.stop    =  proc_demo_seq_stop,.show    =  proc_demo_seq_show,
};static int proc_demo_open(struct inode* inode, struct file* filp)
{return single_open(filp,&proc_demo_seq_show,NULL);
}struct file_operations proc_demo_fops =
{.owner =  THIS_MODULE,.open   =  proc_demo_open,.read    =  seq_read,.release= single_release,
};int proc_demo_init(void)
{struct proc_dir_entry * entry = NULL;entry = proc_create(PROC_NAME,0,NULL,&proc_demo_fops);if(!entry){printk(KERN_ALERT "line:%d proc_create failed!",__LINE__);}return 0;
}void proc_demo_exit(void)
{/***       The second parameter of 'remove_proc_entry()' is ** a pointer which point to parent directory.We create our** proc-entry-point in /proc/, so we pass NULL into it.*/remove_proc_entry(PROC_NAME,NULL);
}module_init(proc_demo_init);
module_exit(proc_demo_exit);

读过LDD3或者ELDD的读者能够一起交流讨论 : )

关于一直卡死的两段代码,望对LDD3有兴趣者戳开这个blog : )相关推荐

  1. 如何用pycharm对比两段代码(或两个文件的代码)

    右键文件,点击Compare With 选择要对比的文件,点击ok 然后就可以愉快地玩耍啦♪(∇*) 参考文章:pycharm的强大之处之两个文件代码的比对

  2. java通过代码显示特定窗体,如何把这两段代码在一个窗体显示,类似于windows自带的扫雷一样...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 第一段是菜单栏: package saolei; import java.awt.event.ActionEvent; import java.awt.e ...

  3. java 事件分发机制_用两段代码带你看懂事件分发机制

    先来看一段精简版的源码 View.java public class View { private View.OnClickListener mOnClickListener; private OnL ...

  4. 如何测试pytorch-gpu版本和tensorflow-gpu版本是否安装成功,测试代码如下,在想要测试的环境中将两段代码分别输入测试即可

    #测试pytorch-gpu是否能用 import torch flag = torch.cuda.is_available() print(flag) ngpu= 1 # Decide which ...

  5. c语言有语段不运行,各位C语言的高手,帮忙看下下面两段代码!他们不能运行!急!!!!!!...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 n=L->next; while(n->next!=NULL) { if(n->data.mathnext->data.math) ...

  6. 什么是epoll的水平触发与边缘触发?两段代码彻底理解

    Edge trigger and level trigger of epoll 水平触发 对于读操作:只要缓冲内容不为空,LT模式返回读就绪. 对于写操作:只要缓冲区还不满,LT模式会返回写就绪. # ...

  7. python中生成器的两段代码

    生产者-消费者经典单线程问题 import time def consumer(name):     print("%s 准备吃包子啦!" %name)     while Tru ...

  8. 两段用来启动/重启Linux下Tomcat的Perl脚本

    两段代码,第二段比较好些. 下面是Split输出结果方式的代码: #!/usr/local/bin/perl #Date:2015-07-07 print "Begin to restart ...

  9. 从固定管线到可编程管线:十段代码入门OpenGL

    文章目录 1. 最简单的OpenGL应用程序 2. 视点系统和投影矩阵 3. 深度缓冲区和深度测试 4. 模型的旋转和平移 5. VBO和顶点混合数组 6. 纹理映射和纹理坐标 7. 光照和法向量计算 ...

  10. 老司机写的java代码_菜鸟 or 老司机?写段代码看看吧

    有的小伙伴可能用Python写代码已经非常久了,可能觉得已经是个高手了,那么看看下面这个简单的实现需求,你会写怎样的Python代码呢?通过你写的代码,应该可以大约评估下你到底是菜鸟还是一个老司机了, ...

最新文章

  1. scss-@for 指令
  2. vasp和ms_科学网—VASP如何计算铁磁和考虑强关联作用 - 叶小球的博文
  3. python 响应代码_Python HTTP响应
  4. qt android webview,qt browser 加载一个webview过程
  5. java 格雷码_在 Java 中使用递归的方式将二进制转换为等效的格雷码
  6. Gartner的企业信息管理EIM模型
  7. 由PPP项目总结的几点项目经验
  8. mysql fatch array_辩别WEB服务程序,,了解常见的几种脚本和数据库之间的搭配组合及特点...
  9. 处女座女的爱情黑暗面 水瓶座男不爱你的表现是什么
  10. C#大恒相机采集图片时图片上下对称折叠了
  11. 2分钟部署人生模拟器,解锁人生新剧情
  12. 哪种深度学习框架发展最快?
  13. Latex最后一页文本或参考文献左右对齐(平衡)
  14. 软件设计之“信雅达”
  15. 2022视频编码招聘面经
  16. RailWay免费容器托管平台
  17. 基于uni-app开发的仿奈雪の茶小程序前端模板
  18. Navy maneuvers(dfs)
  19. ffmpeg+soundtouch实现音频变速变调
  20. Portkey——打造人人皆是艺术家的NFT市场

热门文章

  1. 去重 属性_亿万级海量数据去重软方法,spark/flink/mr等通用
  2. mysql的常见命令与语法规范
  3. c 打印二叉树_基础扩展 | 22. 遍历二叉树—前序遍历算法的VBA代码解析
  4. java用枚举代替int常量,让你的系统更安全--用枚举enum替代int常量
  5. JAVA集合系列(5):关于LinkedList
  6. # 20175333曹雅坤 第八周课程学习总结
  7. Django实现websocket完成实时通讯,聊天室,在线客服等
  8. DRF的解析器和渲染器
  9. spring boot 2使用Mybatis多表关联查询
  10. PhpStorm Live Template加PHP短语法Short Open Tags打造原生模板