{"id":246,"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:18","modified_gmt":"2020-08-22T20:26:18","slug":"this-is-the-page-title-toplevel-81","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=246","title":{"rendered":"Process Scheduling"},"content":{"rendered":"\n<title>Process Scheduling<\/title>\n<p>\nLike many dialects of <glossary>UNIX<\/glossary>,\nthe process scheduler is a function inside the <glossary>kernel<\/glossary>, not a\nseparate process. Actually, it&#8217;s better to say that process scheduling is done by two functions\nworking together, both of which are a part of <file type=\"\">sched.c<\/file>. The first function is schedule(), which does\nthe actual scheduling. The other is do_timer(), which is called at different times and whose\nfunction is to update the times of each process. Essentially, this is used to keep track of how long\neach process has been running, how long it has had the processors, how long it has been in user\nmode, how long it has been in <glossary>kernel<\/glossary>\nmode, etc.\n<\/p>\n<p>\nIn the <tutorial id=3>section on operating system basics<\/tutorial>,\nI mentioned that each process gets a <glossary>time slice<\/glossary>\nthat&#8217;s 1\/100<sup>th<\/sup> of a second long. At the end of each <glossary>time slice<\/glossary>,\nthe do_timer() function is called and priorities are recalculated. Each time a <glossary>system call<\/glossary>\nreturns to <glossary>user mode<\/glossary>,   do_timer() is\nalso called to update the times.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"Linux uses a modified 'round-robin' algorithm to schedule processes.\" \/>\n<concept id=\"\" description=\"Linux uses a modified 'round-robin' algorithm to schedule processes.\" \/>\n<p>\nScheduling\nprocesses is not as easy as finding the process that has been waiting the longest. Some operating\nsystems do this kind of scheduling, which is referred to as &#8220;round-robin.&#8221; The processes\ncould be thought of as sitting in a circle. The scheduling algorithm could then be though of as a\npointer that moves around the circle, getting to each process in turn. The Linux scheduler does a\nmodified version of round-robin scheduling, however, so that processes with a higher priority get to run more often and longer.\n<\/p>\n<p>\nLinux also allows you to be nice to your fellow processes. If you feel\nthat your work is not as important as someone else&#8217;s, you might want to consider being nice to them.\nThis is done with the nice command, the syntax of which is\n<\/p>\n<p>\n<commandexample>nice &lt;nice_value&gt; &lt;command&gt;<\/commandexample>\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What command would you use to start another command with a different priority?\" \/>\n<concept id=\"\" description=\"The 'nice' command is used to start another command with a different priority.\" \/>\n<p>\nFor example, if you wanted to run the <command>date<\/command> command with a lower priority, you could run it like this:<\/p>\n<p>\n<commandexample>nice -10 date<\/commandexample>\n<\/p>\n<question id=\"\" type=\"tf\" text=\"Only root can increase a process' priority.\" \/>\n<concept id=\"\" description=\"Only root can increase a process' priority.\" \/>\n<p>\nThis decreases the start priority of the <command>date<\/command>\ncommand by 10. Nice values range from 19 (lowest priority) to -20 (highest priority).\nNote that only root can increase a process&#8217; priority, that is, use a negative nice\nvalue. The <glossary>nice value<\/glossary>\nonly affects running processes, but child processes inherit the nice value of\ntheir parent. By default, processes that users start have a <glossary>nice value<\/glossary>\nof 20.\n<\/p>\n<p>\nThe numeric\nvalue calculated for the priority is the opposite of what we normally think of as priority. A\nbetter way of thinking about it is like the pull-down number tickets you get at the ice cream\nstore. The lower the number, the sooner you&#8217;ll be served. So it is for processes as well.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What command would you use to change the priority of a running process?\" \/>\n<concept id=\"137\" description=\"The 'renice' command can be used to change the priority of a running process.\" \/>\n<p>\nAlthough the <command>nice<\/command> command only works when you start a process, you can change\nthe nice value on a running process by using the <command>renice<\/command> command. It uses\nthe same priorities as <command>nice<\/command>, but is used on processes that are already\nrunning. It can take the <keyinput>-p<\/keyinput> option for a specific PID, the\n<keyinput>-g<\/keyinput> option for a process group, or <keyinput>-u<\/keyinput> for the\nprocesses belonging to a specific user.\n<\/p>\n<p>\nThe number of times the clock interrupts per second, and therefore the numbers of times the\npriority is recalculated, is defined by the HZ system <glossary>variable<\/glossary>.\nThis is defined by default to be 100HZ, or 100\ntimes a second. However, we are assuming that the priorities are only calculated once a second\ninstead of 100 times.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Process Scheduling Like many dialects of UNIX, the process scheduler is a function inside the kernel, not a separate process. Actually, it&#8217;s better to say that process scheduling is done by two functions working together, both of which are a &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=246\">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-246","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/246","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=246"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/246\/revisions"}],"predecessor-version":[{"id":669,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/246\/revisions\/669"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}