{"id":466,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:00","modified_gmt":"2020-08-22T20:26:00","slug":"this-is-the-page-title-toplevel-299","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=466","title":{"rendered":"Creating a Process"},"content":{"rendered":"\n<title>Creating a Process<\/title>\nWhen the system starts up it is running in kernel mode and there is, in a sense, only\none process, the initial process.\nLike all processes, the initial process has a machine state represented by stacks, registers\nand so on.\nThese will be saved in the initial process&#8217;s <tt>task_struct<\/tt> data structure when other\nprocesses in the system are created and run.\nAt the end of system initialization, the initial process starts up a kernel thread (called\n<tt>init<\/tt>) and then sits in an idle loop doing nothing.\nWhenever there is nothing else to do the scheduler will run this, idle, process.\nThe idle process&#8217;s <tt>task_struct<\/tt> is the only one that is not dynamically allocated, it is\nstatically defined at kernel build time and is, rather confusingly, called <tt>init_task<\/tt>.\n<p>\nThe <tt>init<\/tt> kernel thread or process has a process identifier of 1 as it is the system&#8217;s\nfirst real process.\nIt does some initial setting up of the system (such as opening the system console and\nmounting the root file system) and then executes the system initialization program.\nThis is one of <tt>\/etc\/init<\/tt>, <tt>\/bin\/init<\/tt> or <tt>\/sbin\/init<\/tt> depending on your system.\nThe <tt>init<\/tt> program uses <tt>\/etc\/inittab<\/tt> as a script file to create new processes within\nthe system.\nThese new processes may themselves go on to create new processes.\nFor example the <tt>getty<\/tt> process may create a <tt>login<\/tt> process when a user attempts to\nlogin.\nAll of the processes in the system are descended from the <tt>init<\/tt> kernel thread.\n<p>\nNew processes are created by cloning old processes, or rather by cloning the current\nprocess.\nA new task is created by a system call (<em>fork<\/em> or <em>clone<\/em>)\n<p>\nand the cloning happens within the kernel in kernel mode.\nAt the end of the system call there is a new process waiting to run once the scheduler\nchooses it.\nA new <tt>task_struct<\/tt> data structure is allocated from the system&#8217;s physical memory with\none or more physical pages for the cloned process&#8217;s stacks (user and kernel).\nA new process identifier may be created, one that is unique within the set of process\nidentifiers in the system.\nHowever, it is perfectly reasonable for the cloned process to keep its parents process\nidentifier.\nThe new <tt>task_struct<\/tt> is entered into the <tt>task<\/tt> vector and the contents of the\nold (<tt>current<\/tt>) process&#8217;s <tt>task_struct<\/tt> are copied into the cloned <tt>task_struct<\/tt>.\n<p>\nWhen cloning processes Linux allows the two processes to share resources rather than\nhave two separate copies.\nThis applies to the process&#8217;s files, signal handlers and virtual memory.\nWhen the resources are to be shared their respective <tt>count<\/tt> fields are incremented\nso that Linux will not deallocate these resources until both processes have finished\nusing them.\nSo, for example, if the cloned process is to share virtual memory, its <tt>task_struct<\/tt>\nwill contain a pointer to the <tt>mm_struct<\/tt> of the original process and that <tt>mm_struct<\/tt>\nhas its <tt>count<\/tt> field incremented to show the number of current processes sharing it.\n<p>\nCloning a process&#8217;s virtual memory is rather tricky.\nA new set of <tt>vm_area_struct<\/tt> data structures must be generated together with their\nowning <tt>mm_struct<\/tt> data structure and the cloned process&#8217;s page tables.\nNone of the process&#8217;s virtual memory is copied at this point.\nThat would be a rather difficult and lengthy task for some of that virtual memory would\nbe in physical memory, some in the executable image that the process is currently\nexecuting and possibly some would be in the swap file.\nInstead Linux uses a technique called &#8220;copy on write&#8221; which means that virtual memory\nwill only be copied when one of the two processes tries to write to it.\nAny virtual memory that is not written to, even if it can be, will be shared between the\ntwo processes without any harm occuring.\nThe read only memory, for example the executable code, will always be shared.\nFor &#8220;copy on write&#8221; to work, the writeable areas have their page table entries marked\nas read only and the <tt>vm_area_struct<\/tt> data structures describing them are marked as\n&#8220;copy on write&#8221;.\nWhen one of the processes attempts to write to this virtual memory a page fault will\noccur.\nIt is at this point that Linux will make a copy of the memory and fix up the two processes&#8217;\npage tables and virtual memory data structures.\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Process When the system starts up it is running in kernel mode and there is, in a sense, only one process, the initial process. Like all processes, the initial process has a machine state represented by stacks, registers &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=466\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-466","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=466"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/466\/revisions"}],"predecessor-version":[{"id":534,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/466\/revisions\/534"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}