{"id":213,"date":"2016-02-11T14:43:24","date_gmt":"2016-02-11T14:43:24","guid":{"rendered":"http:\/\/emacslisp.com\/?p=213"},"modified":"2016-02-12T01:30:12","modified_gmt":"2016-02-12T01:30:12","slug":"linux-kernel-task_struct-for-process-management","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=213","title":{"rendered":"Linux Kernel &#8211; start_kernel in main.c and  task_struct for process management"},"content":{"rendered":"<p>Linux start with start_kernel function in main.c. It has lots of function, such mm_init for kernel memory allocators. Let&#8217;s start with some very basic components firstly.<\/p>\n<p>task_struct is an basic and important struct for process.<br \/>\nIt is defined in &#8220;include\/linux\/sched.h&#8221;<\/p>\n<p>One process will be linked with others, for example, parent process and child process are linked together.<\/p>\n<p>each member of task_struct is very critical and it is better to go though all of them to understand its better.<\/p>\n<p>Check the definition of task_struct, you could print more information you want,<\/p>\n<pre lang=\"c\" line=\"1\">\r\n#include <linux\/module.h>\r\n#include <linux\/kernel.h> \/* printk() *\/\r\n#include <linux\/errno.h>   \/* error codes *\/\r\n#include <linux\/sched.h>\r\n \r\n \r\nMODULE_LICENSE(\"Dual BSD\/GPL\");\r\n \r\n\/* Declaration of functions *\/\r\nvoid device_exit(void);\r\nint device_init(void);\r\n   \r\n\/* Declaration of the init and exit routines *\/\r\nmodule_init(device_init);\r\nmodule_exit(device_exit);\r\n \r\nint device_init(void)\r\n{\r\n    struct task_struct *task = current; \/\/ getting global current pointer\r\n    printk(KERN_NOTICE \"assignment: current process: %s, PID: %d\", task->comm, task->pid);\r\n    do\r\n    {\r\n        task = task->parent;\r\n        printk(KERN_NOTICE \"assignment: parent process: %s, PID: %d\", task->comm, task->pid);\r\n    } while (task->pid != 0);\r\n    return 0;\r\n}\r\n \r\nvoid device_exit(void) {\r\n  printk(KERN_NOTICE \"assignment: exiting module\");\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/emacslisp.com\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-12-013943.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/emacslisp.com\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-12-013943.png\" alt=\"Screenshot from 2016-02-12 01:39:43\" width=\"848\" height=\"914\" class=\"alignnone size-full wp-image-216\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux start with start_kernel function in main.c. It has lots of function, such mm_init for kernel memory allocators. Let&#8217;s start with some very basic components firstly. task_struct is an basic and important struct for process. It is defined in &#8220;include\/linux\/sched.h&#8221; One process will be linked with others, for example, parent process and child process are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-linux-kernel-analysis"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":6,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":220,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/220"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}