{"id":1738,"date":"2010-10-13T22:14:26","date_gmt":"2010-10-13T14:14:26","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1738"},"modified":"2012-06-06T10:35:25","modified_gmt":"2012-06-06T02:35:25","slug":"%e5%86%85%e6%a0%b8%e5%ae%9a%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1738","title":{"rendered":"\u5185\u6838\u4e2d\u7684\u52a8\u6001\u5b9a\u65f6\u5668"},"content":{"rendered":"<h4>\u5185\u6838\u4e2d\u65f6\u95f4\u7684\u76f8\u5173\u6982\u5ff5<\/h4>\n<p>\u8282\u62cd\u7387\uff1a\u7cfb\u7edf\u5b9a\u65f6\u5668\u4ee5\u67d0\u79cd\u9891\u7387\u89e6\u53d1\u65f6\u949f\u4e2d\u65ad\uff0c\u8fd9\u4e2a\u9891\u7387\u5c31\u79f0\u4e3a\u8282\u62cd\u7387\uff08tick rate\uff09\u3002\u8282\u62cd\u7387\u662f\u901a\u8fc7\u9759\u6001\u9884\u5904\u7406\u5b9a\u4e49\u7684\uff0c\u88ab\u5b9a\u4e49\u4e3aHZ\u3002\u5bf9\u4e8ex86\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4\uff0c\u5b83\u7684HZ\u4e3a100\u3002<\/p>\n<p>\u8282\u62cd\uff1a\u4e24\u6b21\u65f6\u949f\u4e2d\u65ad\u7684\u95f4\u9694\u5c31\u79f0\u4e3a\u8282\u62cd\uff08tick\uff09\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u8282\u62cd\u7b49\u4e8e\u8282\u62cd\u7387\u5206\u4e4b\u4e00\u3002<\/p>\n<p>jiffies\uff1a\u5168\u5c40\u53d8\u91cfjiffies\u7528\u6765\u8bb0\u5f55\u7cfb\u7edf\u81ea\u542f\u52a8\u4ee5\u6765\u4ea7\u751f\u7684\u8282\u62cd\u603b\u6570\u3002\u901a\u8fc7jiffies\/HZ\u5c31\u53ef\u83b7\u53d6\u7cfb\u7edf\u81ea\u542f\u52a8\u4ee5\u6765\u7684\u79d2\u6570\u3002<\/p>\n<h4>\u5185\u6838\u5b9a\u65f6\u5668<\/h4>\n<p>\u5185\u6838\u5b9a\u65f6\u5668\uff0c\u4e5f\u79f0\u4e3a\u52a8\u6001\u5b9a\u65f6\u5668\uff0c\u662f\u7ba1\u7406\u5185\u6838\u65f6\u95f4\u7684\u57fa\u7840\uff0c\u5b83\u662f\u4e00\u79cd\u7528\u6765\u63a8\u8fdf\u6267\u884c\u7a0b\u5e8f\u7684\u5de5\u5177\u3002\u524d\u9762\u4e2d\u65ad\u7684\u6587\u7ae0\u4e2d\u6211\u4eec\u8bf4\u5230\u5185\u6838\u5c06\u5728\u7a0d\u5019\u7684\u65f6\u95f4\u6267\u884c\u4e0b\u90e8\u5206\u5de5\u4f5c\uff0c\u5177\u4f53\u662f\u4f55\u65f6\u6765\u6267\u884c\u5c31\u9700\u8981\u5185\u6838\u5b9a\u65f6\u5668\u3002\u6211\u4eec\u5728\u4f7f\u7528\u5173\u4e8e\u5b9a\u65f6\u5668\u7684\u76f8\u5173\u51fd\u6570\u65f6\uff0c\u5148\u6765\u770b\u770b\u5185\u6838\u662f\u5982\u4f55\u63cf\u8ff0\u5b9a\u65f6\u5668\u7684\uff1a<\/p>\n<pre class=\"brush:c\">  10struct tvec_base;\r\n  11\r\n  12struct timer_list {\r\n  13        \/*\r\n  14         * All fields that change during normal runtime grouped to the\r\n  15         * same cacheline\r\n  16         *\/\r\n  17        struct list_head entry;\/\/\u5b9a\u65f6\u5668\u94fe\u8868\u5165\u53e3\r\n  18        unsigned long expires;\/\/\u4ee5jiffies\u4e3a\u5355\u4f4d\u7684\u5b9a\u65f6\u503c\r\n  19        struct tvec_base *base;\/\/\r\n  20\r\n  21        void (*function)(unsigned long);\/\/\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\r\n  22        unsigned long data;\/\/\u4f20\u7ed9\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u7684\u53c2\u6570\uff0c\u65e0\u7b26\u53f7\u957f\u6574\u578b\r\n  23\r\n  24        int slack;\r\n  25\r\n  26#ifdef CONFIG_TIMER_STATS\r\n  27        void *start_site;\r\n  28        char start_comm[16];\r\n  29        int start_pid;\r\n  30#endif\r\n  31#ifdef CONFIG_LOCKDEP\r\n  32        struct lockdep_map lockdep_map;\r\n  33#endif\r\n  34};<\/pre>\n<p>\u5185\u6838\u5b9a\u65f6\u5668\u4f7f\u7528\u8d77\u6765\u4e5f\u662f\u76f8\u5f53\u7b80\u5355\u7684\uff0c\u57fa\u672c\u7684\u4f7f\u7528\u601d\u8def\u662f\uff1a\u5b9a\u4e49\u4e00\u4e2atimer_list\uff1b\u521d\u59cb\u5316\u5b9a\u65f6\u5668\uff0c\u5373\u5bf9timer_list\u7ed3\u6784\u4e2d\u7684\u76f8\u5173\u5b57\u6bb5\u8fdb\u884c\u8d4b\u503c\uff1b\u5b9a\u4e49\u4f60\u81ea\u5df1\u7684\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\uff1b\u6fc0\u6d3b\u5b9a\u65f6\u5668\uff1b\u4fee\u6539\u5b9a\u65f6\u5668\uff0c\u5982\u679c\u6709\u5fc5\u8981\u7684\u8bdd\uff1b\u5220\u9664\u5b9a\u65f6\u5668\u3002\u4e0a\u8ff0\u8fc7\u7a0b\u901a\u8fc7\u4ee5\u4e0b\u7684API\u5c31\u53ef\u7b80\u5355\u7684\u5b8c\u6210\u3002<\/p>\n<h4>\u79d2\u5b57\u7b26\u9a71\u52a8\u4e0e\u5185\u6838\u5b9a\u65f6\u5668API<\/h4>\n<p>\u4e0b\u9762\u8981\u5206\u6790\u7684\u8fd9\u4e2a\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u4e00\u4e2a\u79d2\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\uff0c\u7a0b\u5e8f\u6574\u4f53\u7684\u6846\u67b6\u4e0e\u4e4b\u524d\u6211\u4eec\u5206\u6790\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u76f8\u540c\u3002\u5185\u6838\u6a21\u5757\u52a0\u8f7d\u51fd\u6570\u548c\u5378\u8f7d\u51fd\u6570\u7684\u529f\u80fd\u548c\u4e4b\u524d\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u5b8c\u5168\u76f8\u540c\uff0c\u56e0\u6b64\u63a5\u4e0b\u6765\u4e3b\u8981\u5206\u6790\u8fd9\u4e2a\u8bbe\u5907\u7684\u64cd\u4f5c\u51fd\u6570\u3002\u6211\u4eec\u4f7f\u7528\u4e0b\u9762\u7684\u7ed3\u6784\u6765\u63cf\u8ff0\u6211\u4eec\u5f53\u524d\u6240\u8ba8\u8bba\u7684\u79d2\u5b57\u7b26\u8bbe\u5907\u3002<\/p>\n<pre class=\"brush:c\">struct second_dev\r\n{\r\n\tstruct cdev cdev;\r\n\tatomic_t counter;\r\n\tstruct timer_list s_timer;\r\n};<\/pre>\n<p>cdev\u5373\u4e3a\u5185\u6838\u4e2d\u63cf\u8ff0\u5b57\u7b26\u8bbe\u5907\u7684\u7ed3\u6784\u4f53\uff1bcounter\u7528\u6765\u8bb0\u5f55\u79d2\u6570\uff0c\u5b83\u5728\u6d4b\u8bd5\u7a0b\u5e8f\u4e2d\u7528\u6765\u663e\u793a\u5f53\u524d\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\u3002\u540c\u65f6\u4e5f\u662f\u6b64\u8bbe\u5907\u9a71\u52a8\u8fdb\u884c\u8bfb\u5199\u7684\u6570\u636e\uff1bs_timer\u662f\u4e3a\u6b64\u8bbe\u5907\u6240\u5b9a\u4e49\u7684\u5b9a\u65f6\u5668\u3002\u6bcf\u6b21\u5b9a\u65f6\u4e00\u4e2aHZ\uff0c\u4e5f\u5c31\u662f1\u79d2\uff0c\u5f53\u5b9a\u65f6\u5230\u540e\uff0c\u901a\u8fc7\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff0c\u6bcf\u6b21\u589e\u52a0\u4e00\u4e2aHZ\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4e00\u8fb9\u5206\u6790\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u7684\u8bbe\u5907\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4e00\u8fb9\u5b66\u4e60\u5185\u6838\u5b9a\u65f6\u5668\u7684API\u3002<\/p>\n<p>1.\u6253\u5f00\u51fd\u6570<\/p>\n<p>\u79d2\u5b57\u7b26\u8bbe\u5907\u5728\u6587\u4ef6\u6253\u5f00\u51fd\u6570\u4e2d\u5b9e\u73b0\u5bf9\u5b9a\u65f6\u5668\u7684\u521d\u59cb\u5316\u4ee5\u53ca\u8d4b\u503c\u3002<\/p>\n<pre class=\"brush:c\">static int second_open(struct inode*inode,struct file*filp)\r\n{\r\n\t\/\/initializing the timer\r\n\tinit_timer(&amp;second_devp-&gt;s_timer);\r\n\tsecond_devp-&gt;s_timer.function=&amp;second_timer_handler;\r\n\tsecond_devp-&gt;s_timer.expires=jiffies+HZ;\r\n\tadd_timer(&amp;second_devp-&gt;s_timer);\/\/add a timer\r\n\tatomic_set(&amp;second_devp-&gt;counter,0);\/\/clear the counter\r\n\treturn 0;\r\n}<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u5185\u6838\u5b9a\u65f6\u5668\u51fd\u6570\u901a\u8fc7\uff1ainit_timer(&amp;t);\u6765\u8fdb\u884c\u5b9a\u65f6\u5668\u7684\u521d\u59cb\u5316<\/strong>\uff0c\u5176\u4e2dt\u4e3atimer_list\u7c7b\u578b\u7684\u6307\u9488\u3002expires\u662f\u7528\u6765\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7684\u5b9a\u65f6\u503c\uff0c\u5355\u4f4d\u662f\u8282\u62cd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u521d\u59cb\u5b9a\u65f6\u503c\u4e3a\u4e00\u79d2\uff0c\u5373\u5728\u5f53\u524d\u65f6\u949f\u8282\u62cd\u6570\u4e0a\u52a0\u4e0a\u4e00\u4e2a\u8282\u62cd\u7387\u3002function\u5219\u662f\u4e0a\u9762\u63d0\u5230\u7684\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728open\u51fd\u6570\u4e2d\uff0c\u8fd8<strong>\u901a\u8fc7add_timer(&amp;t);\u6765\u8fdb\u884c\u5b9a\u65f6\u5668\u7684\u6fc0\u6d3b<\/strong>\uff0c\u4e5f\u5c31\u662f\u628a\u5f53\u524d\u8fd9\u4e2a\u5b9a\u65f6\u5668\u52a0\u5165\u5230\u5185\u6838\u5b9a\u65f6\u5668\u94fe\u8868\u5f53\u4e2d\u3002\u7531\u4e8e\u6211\u4eec\u5728\u8be5\u8bbe\u5907\u5bf9\u5e94\u7684\u8857\u5934\u4f53\u4e2d\u5c06counter\u8bbe\u7f6e\u4e3aatomic_t\uff08\u5173\u4e8eatomic.h\u5934\u6587\u4ef6\uff0c\u672c\u535a\u5ba2\u7a0d\u5019\u4f1a\u4e13\u95e8\u6765\u5206\u6790\uff0c\u8bf7\u6301\u7eed\u5173\u6ce8\uff5e <img src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-includes\/images\/smilies\/8fec2697a17d5a6254fb9611.gif\" alt=\"e91\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> \uff09\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528atomic_set\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u503c\u8bbe\u7f6e\u4e3a0\u3002<\/p>\n<p>2.\u8bfb\u51fd\u6570<\/p>\n<pre class=\"brush:c\">static ssize_t second_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos)\r\n{\r\n\tint counter;\r\n\r\n\tcounter=atomic_read(&amp;second_devp-&gt;counter);\r\n\tif(put_user(counter,(int*)buf))\r\n\t{\r\n\t\treturn -EFAULT;\r\n\t}\r\n\telse\r\n\t{\r\n\t\treturn sizeof(unsigned int);\r\n\t}\r\n\r\n}<\/pre>\n<p>\u8fd9\u4e2a\u8bfb\u51fd\u6570\u7684\u610f\u601d\u662f\u4ece\u5185\u6838\u4e2d\u901a\u8fc7atomic_read\u51fd\u6570\u8bfb\u53d6counter\uff0c\u518d\u901a\u8fc7put_user\u51fd\u6570\u5c06\u5176\u503c\u8fd4\u56de\u81f3\u7528\u6237\u7a7a\u95f4\u3002<\/p>\n<p>3.\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570<\/p>\n<pre class=\"brush:c\">static void second_timer_handler(unsigned long arg)\r\n{\r\n\tmod_timer(&amp;second_devp-&gt;s_timer,jiffies+HZ);\r\n\tatomic_inc(&amp;second_devp-&gt;counter);\r\n\tprintk(\"Current jiffies is %ld\\n\",jiffies);\r\n}<\/pre>\n<p>\u6267\u884c\u6b64\u5904\u7406\u51fd\u6570\u65f6\uff0c\u8bf4\u660e\u5148\u524d\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u5df2\u7ecf\u5230\u671f\u3002\u6211\u4eec<strong>\u901a\u8fc7mod_timer(&amp;t,jiffies+newdelay);\u6765\u6539\u53d8\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4<\/strong>\u3002\u8fd9\u91cc\u6211\u4eec\u6bcf\u6b21\u589e\u52a0\u4e00\u79d2\u7684\u65f6\u95f4\u3002\u63a5\u7740\u901a\u8fc7atomic_inc\u51fd\u6570\u5bf9counter\u52a0\u4e00\uff0c\u5e76\u4e14\u5728\u5185\u6838\u4e2d\u663e\u793a\u5f53\u524d\u7684\u8282\u62cd\u6570\u3002<\/p>\n<p>4.\u6587\u4ef6\u91ca\u653e\u51fd\u6570<\/p>\n<pre class=\"brush:c\">static int second_release(struct inode*inode,struct file*filp)\r\n{\r\n\tdel_timer(&amp;second_devp-&gt;s_timer);\r\n\treturn 0;\r\n}<\/pre>\n<p>\u5728\u6587\u4ef6\u91ca\u653e\u51fd\u6570\u4e2d\uff0c<strong>\u901a\u8fc7del_timer(&amp;t);\u5c31\u53ef\u4ee5\u5220\u9664\u8fd9\u4e2a\u5b9a\u65f6\u5668<\/strong>\u3002<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p><strong><em>1.make\u7f16\u8bd1\u5e76\u5c06\u6b64\u6a21\u5757\u63d2\u5165\u5185\u6838\uff1asudo insmod secondcdev.ko<br \/>\n2.\u67e5\u770b\/proc\/devices\u6587\u4ef6\u4e2d\u662f\u5426\u6709\u4e3b\u8bbe\u5907\u53f7\u4e3a250\u7684\u8bbe\u5907<br \/>\n3.\u521b\u5efa\u8be5\u8bbe\u5907\u6587\u4ef6\u8282\u70b9\uff0c\u4f7f\u7528\u547d\u4ee4\uff1asudo mknod \/dev\/secondcdev \u00a0c \u00a0250 \u00a00<br \/>\n4.\u4fee\u6539\u8be5\u8bbe\u5907\u6587\u4ef6\u6743\u9650\uff1asudo chmod 777 \/dev\/secondcdev<br \/>\n5.\u7f16\u8bd1\u6d4b\u8bd5\u6587\u4ef6\uff1agcc -o test test.c<br \/>\n6.\u8fd0\u884c\u6d4b\u8bd5\u7a0b\u5e8f\uff1a.\/test\uff1b<\/em><\/strong><\/p>\n<p><strong><em><\/em><\/strong>\u5173\u4e8e\u6d4b\u8bd5\u7a0b\u5e8f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">#include &lt; stdio.h &gt;\r\n#include &lt; sys\/stat.h &gt;\r\n#include &lt; sys\/types.h &gt;\r\n#include &lt; fcntl.h &gt;\r\n#include &lt; stdlib.h &gt;\r\nint main()\r\n{\r\n\tint fd;\r\n\tint counter=0;\r\n\tint old_counter=0;\r\n\r\n\t\/\/open the \/dev\/secondcdev device file\r\n\tif((fd=open(\"\/dev\/secondcdev\",O_RDONLY))&lt;0)\r\n\t{\r\n\t\tprintf(\"Device open failed\\n\");\r\n\t\treturn -1;\r\n\t}\r\n\r\n\twhile(1)\r\n\t{\r\n\t\tread(fd,&amp;counter,sizeof(unsigned int));\/\/read the current second number\r\n\t\tif(counter!=old_counter)\r\n\t\t{\r\n\t\t\tprintf(\"seconds after open \/dev\/second:%d\\n\",counter);\r\n\t\t\told_counter=counter;\r\n\t\t}\r\n\t}\r\n\r\n\treturn 0;<\/pre>\n<p>\u5728\u6d4b\u8bd5\u7a0b\u5e8f\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u8bbe\u5907\u6587\u4ef6\uff1b\u7136\u540e\u5faa\u73af\u8bfb\u53d6counter\u7684\u503c\uff0c\u5982\u679c\u524d\u540e\u4e24\u6b21\u8bfb\u53d6\u7684counter\u503c\u4e0d\u4e00\u6837\uff0c\u90a3\u4e48\u5c31\u8f93\u51fa\uff1b\u7531\u4e8e\u6211\u4eec\u5728\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u4e2d\u662f\u6bcf\u9694\u4e00\u79d2\u4fee\u6539\u4e00\u6b21\u5b9a\u65f6\u5668\uff0c\u56e0\u6b64\u6d4b\u8bd5\u7a0b\u5e8f\u5c06\u6bcf\u4e2a\u4e00\u79d2\u8f93\u51fa\u4e00\u6b21counter\u503c\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u6240\u7f16\u5199\u7684\u79d2\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u4f7f\u5f97\u6570\u636e\uff08\u5373counter\uff09\u5728\u5185\u6838\u7a7a\u95f4\u548c\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u4e2d\u65f6\u95f4\u7684\u76f8\u5173\u6982\u5ff5 \u8282\u62cd\u7387\uff1a\u7cfb\u7edf\u5b9a\u65f6\u5668\u4ee5\u67d0\u79cd\u9891\u7387\u89e6\u53d1\u65f6\u949f\u4e2d\u65ad\uff0c\u8fd9\u4e2a\u9891\u7387\u5c31\u79f0\u4e3a\u8282\u62cd\u7387\uff08tick rate\uff09\u3002\u8282 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[134,157,162],"class_list":["post-1738","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kernel","tag-157","tag-162"],"views":11272,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=1738"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1738\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}