{"id":1841,"date":"2010-10-25T20:38:52","date_gmt":"2010-10-25T12:38:52","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1841"},"modified":"2010-10-25T21:00:22","modified_gmt":"2010-10-25T13:00:22","slug":"%e9%81%8d%e5%8e%86%e8%bf%9b%e7%a8%8b%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1841","title":{"rendered":"\u904d\u5386\u8fdb\u7a0b\u94fe\u8868"},"content":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u662f\u7531\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09\uff0c\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\u7ec4\u6210\u7684\uff1b\u5e76\u4e14\uff0cOS\u901a\u5e38\u662f\u901a\u8fc7PCB\u6765\u611f\u77e5\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5b58\u5728\u3002\u5176\u5b9ePCB\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u4ee3\u7801\u63cf\u8ff0\u3002linux\u5185\u6838\u4e2d\u4f7f\u7528task_struct\u7ed3\u6784\u6765\u63cf\u8ff0\u4e00\u4e2aPCB\uff08\u5177\u4f53\u53ef\u4ee5\u5728linux\/kernel\/sched.c\u67e5\u770b\u6e90\u7801\uff09\uff1b\u591a\u4e2a\u8fdb\u7a0b\u5219\u5e38\u5e38\u4f7f\u7528\u53cc\u94fe\u8868\u7b49\u6765\u8fdb\u884c\u7ec4\u7ec7\u3002\u6bd4\u5982\u53ef\u8fd0\u884c\u72b6\u6001\u7684\u8fdb\u7a0b\u7ec4\u6210\u53ef\u8fd0\u884c\u961f\u5217\uff0c\u7b49\u5f85\u72b6\u6001\u7684\u8fdb\u7a0b\u7ec4\u6210\u7b49\u5f85\u961f\u5217\u7b49\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4f7f\u7528\u524d\u6587\u4e2d\u6240\u5206\u6790\u7684list_head\u7ed3\u6784\u6765\u904d\u5386\u5185\u6838\u4e2d\u7684\u8fdb\u7a0b\u94fe\u8868\u3002<span style=\"font-size: 13.3333px;\">task_struct\u7ed3\u6784\u4e2d\u4f7f\u7528\u591a\u4e2a\u5b57\u6bb5\u6765\u8be6\u7ec6\u7684\u63cf\u8ff0\u4e00\u4e2a\u8fdb\u7a0b\u3002\u672c\u6587\u4e2d\u6240\u5c5e\u7684\u904d\u5386\u51fd\u6570\u53ea\u6253\u5370\u4e00\u4e9b\u5e38\u7528\u7684\u4fe1\u606f\uff0c\u5373\u8fdb\u7a0b\u540d\u79f0\uff0c\u8fdb\u7a0b\u7684pid\u3002<\/span><\/p>\n<p>\u6211\u4eec\u5229\u7528list.h\u4e2d\u7684\u4e0b\u8ff0\u904d\u5386\u5b8f\u5bf9\u6574\u4e2a\u8fdb\u7a0b\u94fe\u8868\u8fdb\u884c\u904d\u5386\uff1a<\/p>\n<pre class=\"brush:c\">420#define list_for_each_entry(pos, head, member)                          \\\r\n421        for (pos = list_entry((head)-&gt;next, typeof(*pos), member);      \\\r\n422             prefetch(pos-&gt;member.next), &amp;pos-&gt;member != (head);        \\\r\n423             pos = list_entry(pos-&gt;member.next, typeof(*pos), member))<\/pre>\n<p>\u5148\u7b80\u5355\u4e86\u89e3\u4e00\u4e0b\u8fd9\u4e2a\u5b8f\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u7531\u4e8elist_head\u7ed3\u6784\u4e2d\u6ca1\u6709\u6570\u636e\u5b57\u6bb5\uff0c\u6240\u4ee5\u5b83\u7ecf\u5e38\u88ab\u5d4c\u5957\u5728\u5176\u4ed6\u7684\u7ed3\u6784\u4f53\u5f53\u4e2d\u3002<span style=\"font-size: 13.3333px;\">pos\u6307\u5411\u5305\u542blist_struct\u7ed3\u6784\u7684\u7ed3\u6784\u4f53\uff1bhead\u4e3alist_head\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u4e2d\u4efb\u610f\u4e00\u4e2a\u7ed3\u70b9\u7684\u6307\u9488\uff1bmember\u5373\u4e3alist_head\u7c7b\u578b\u7684\u53d8\u91cf\u540d\u3002<\/span><\/p>\n<p>\u5bf9\u5e94\u5230\u6211\u4eec\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0cpos\u4e3a\u6307\u5411task_struct\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6211\u4eec\u901a\u8fc7\u904d\u5386\u5b8f\u5373\u5f97\u5230\u6bcf\u4e2a\u8fdb\u7a0b\u5bf9\u5e94\u7684task_struct\u7c7b\u578b\u7684\u6307\u9488\uff1b\u6211\u4eec\u5c06current_head\u8d4b\u4e88\u904d\u5386\u5b8f\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0ccurrent\u662f\u4e00\u4e2a\u5b8f\uff0c\u5373\u4e3a\u7cfb\u7edf\u5185\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff1b\u7531\u4e8elist_struct\u7ed3\u6784\u5728task_struct\u7ed3\u6784\u4e2d\u7684\u53d8\u91cf\u660e\u4e3atasks\uff0c\u56e0\u6b64\u6211\u4eec\u5c06tasks\u4f20\u9012\u7ed9\u4fbf\u5229\u5b8f\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u3002<\/p>\n<pre class=\"brush:c\">#include&lt; linux\/init.h &gt;\r\n#include&lt; linux\/module.h &gt;\r\n#include&lt; linux\/sched.h &gt;\r\n#include&lt; linux\/sem.h &gt;\r\n#include&lt; linux\/list.h &gt;\r\n\r\nstatic int __init  traverse_init(void)\r\n{\r\n      struct task_struct *pos;\r\n      struct list_head *current_head;\r\n      int count=0;\r\n\r\n      printk(\"Traversal module is working..\\n\");\r\n      current_head=&amp;(current-&gt;tasks);\r\n      list_for_each_entry(pos,current_head,tasks)\r\n      {\r\n             count++;\r\n             printk(\"[process %d]: %s\\'s pid is %d\\n\",count,pos-&gt;comm,pos-&gt;pid);\r\n      }\r\n      printk(KERN_ALERT\"The number of process is:%d\\n\",count);\r\n      return 0;\r\n}<\/pre>\n<p>\u4e86\u89e3\u4e86\u4fbf\u5229\u5b8f\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u90a3\u4e48\u7406\u89e3\u4e0a\u8ff0\u4ee3\u7801\u5c31\u7b80\u5355\u7684\u591a\u4e86\u3002\u5173\u4e8e\u4fbf\u5229\u5b8f\u7684\u4ee3\u7801\u8be6\u89e3\u53ef\u53c2\u8003<a href=\"http:\/\/edsionte.com\/techblog\/archives\/1098\">\u8fd9\u91cc<\/a>\u7684\u6587\u7ae0\u3002<\/p>\n<p>\u52a0\u8f7d\u51fd\u6570\u8981\u505a\u7684\u5de5\u4f5c\u5c31\u662f\u8fd9\u4e9b\uff0c\u90a3\u4e48\u5378\u8f7d\u51fd\u6570\u7684\u529f\u80fd\u5c31\u76f8\u5bf9\u4e8e\u7b80\u5355\u591a\u4e86\u3002<\/p>\n<pre class=\"brush:c\">static void __exit traverse_exit(void)\r\n{\r\n  printk(\"hello world exit\\n\");\r\n}<\/pre>\n<p>OK\uff0c\u7f16\u8bd1\uff0c\u63d2\u5165\u5185\u6838\uff0c\u518d\u67e5\u770b\u5185\u6838\u65e5\u5fd7\u6587\u4ef6\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u662f\u7531\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09\uff0c\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\u7ec4\u6210\u7684\uff1b\u5e76\u4e14\uff0cOS\u901a\u5e38\u662f\u901a\u8fc7PCB\u6765\u611f\u77e5\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5b58 [&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,136,123],"class_list":["post-1841","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kernel","tag-list","tag-123"],"views":15253,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1841","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=1841"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1841\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1841"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}