You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.6 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.12">
<meta name="description" content="Linux process management">
<meta name="keywords" content="ps aux">
<meta name="author" content="Apostolos rootApostolos@swarmlab.io">
<title>Linux process management !</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/asciidoctor.js/1.5.9/css/asciidoctor.min.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/styles/github.min.css">
</head>
<body class="article toc2 toc-right">
<div id="header">
<h1>Linux process management !</h1>
<div class="details">
<span id="author" class="author">Apostolos rootApostolos@swarmlab.io</span><br>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of contents</div>
<ul class="sectlevel1">
<li><a href="#_process">1. process</a></li>
<li><a href="#_linux_process_management">2. Linux process management</a>
<ul class="sectlevel2">
<li><a href="#_life_cycle_of_a_process">2.1. Life cycle of a process</a></li>
<li><a href="#_process_state">2.2. process state</a></li>
<li><a href="#_inter_process_communication">2.3. Inter-process communication</a>
<ul class="sectlevel3">
<li><a href="#_communication_between_processes">2.3.1. communication between processes</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div class="sect1">
<h2 id="_process">1. process</h2>
<div class="sectionbody">
<div class="paragraph">
<p>An instance of a running program is called a process.
Every time you run a shell command, a program is run and a process is created for it.</p>
</div>
<div class="paragraph">
<p>In any modern operating system there can be more than one instance of a program loaded in memory at the same time.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_linux_process_management">2. Linux process management</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Operating systems need some ways to create processes.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>In a very simple system designed for running only a single application (e.g., the controller in a microwave oven), it may be possible to have all the processes that will ever be needed be present when the system comes up.</p>
</li>
<li>
<p>In general-purpose systems, however, some way is needed to create and terminate processes as needed during operation.</p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p><strong>There are four principal events that cause a process to be created:</strong></p>
</div>
<div class="ulist">
<ul>
<li>
<p>System initialization.</p>
</li>
<li>
<p>Execution of process creation system call by a running process.</p>
</li>
<li>
<p>A user request to create a new process.</p>
</li>
<li>
<p>Initiation of a batch job.</p>
</li>
</ul>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Process creation in UNIX and Linux are done through <strong>fork()</strong> or <strong>clone()</strong> system calls.</p>
</div>
<div class="paragraph">
<p>There are several steps involved in process creation.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>process scheduling,</p>
</li>
<li>
<p>interrupt handling,</p>
</li>
<li>
<p>signaling,</p>
</li>
<li>
<p>process prioritization,</p>
</li>
<li>
<p>process switching,</p>
</li>
<li>
<p>process state,</p>
</li>
<li>
<p>process memory</p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="_life_cycle_of_a_process">2.1. Life cycle of a process</h3>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<strong>1&#8594;</strong> creation <strong>2&#8594;</strong> execution <strong>3&#8594;</strong> termination <strong>4&#8594;</strong> removal
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="_process_state">2.2. process state</h3>
<div class="paragraph">
<p>An operating system kernel that allows multitasking needs processes to have certain states. Names for these states are not standardised, but they have similar functionality.</p>
</div>
<div class="openblock float-group">
<div class="content">
<div class="imageblock related thumb right">
<div class="content">
<img src="http://localhost:3080/images/Process_states.png" alt="350" width="350">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>the process is <strong>"created"</strong>.</p>
</li>
<li>
<p>After that the process scheduler assigns it the <strong>"waiting"</strong> state.</p>
</li>
<li>
<p>While the process is <strong>"waiting"</strong>, it waits for the scheduler to do a so-called context switch.</p>
</li>
<li>
<p>The context switch loads the process into the processor and changes the state to <strong>"running"</strong> while the previously <strong>"running"</strong> process is stored in a <strong>"waiting"</strong> state.</p>
</li>
<li>
<p>If a process in the <strong>"running"</strong> state needs to wait for a resource, it is assigned the <strong>"blocked"</strong> state.</p>
</li>
<li>
<p>The process state is changed back to <strong>"waiting"</strong> when the process no longer needs to wait (in a blocked state).</p>
</li>
<li>
<p>Once the process <strong>finishes execution</strong>, or is <strong>terminated</strong> by the operating system, it is no longer needed.</p>
</li>
<li>
<p>The process is <strong>removed instantly</strong> or is moved to the <strong>"terminated"</strong> state.</p>
</li>
<li>
<p>When removed, it just waits to be <strong>removed from main memory</strong>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_inter_process_communication">2.3. Inter-process communication</h3>
<div class="paragraph">
<p>When processes need to communicate with each other they must share parts of their address spaces or use other forms of inter-process communication (IPC).</p>
</div>
<div class="ulist">
<ul>
<li>
<p>For instance in a shell pipeline, the output of the first process need to pass to the second one.</p>
</li>
<li>
<p>another example is a task that can be decomposed into cooperating but partially independent processes which can run at once</p>
<div class="ulist">
<ul>
<li>
<p>(i.e., using concurrency, or true parallelism - the latter model is a particular case of concurrent execution and is feasible whenever enough CPU cores are available for all the processes that are ready to run).</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
It is even possible for two or more processes to be running on different machines that may run different operating system (OS), therefore some mechanisms for communication and synchronization (called communications protocols for distributed computing) are needed (e.g., the <strong>Message Passing Interface, often simply called MPI</strong>).
</td>
</tr>
</table>
</div>
<div class="sect3">
<h4 id="_communication_between_processes">2.3.1. communication between processes</h4>
<div class="paragraph">
<p>Processes can communicate with each other through:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Shared Memory</p>
</li>
<li>
<p>Message passing</p>
</li>
</ul>
</div>
<div class="openblock float-group">
<div class="content">
<div class="imageblock related thumb right">
<div class="content">
<img src="http://localhost:3080/images/1-76.png" alt="350" width="350">
</div>
</div>
<div class="imageblock related thumb left">
<div class="content">
<img src="http://localhost:3080/images/2-50.png" alt="350" width="350">
</div>
</div>
</div>
</div>
<div class="paragraph">
<p><sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes">
<hr>
<div class="footnote" id="_footnotedef_1">
<a href="#_footnoteref_1">1</a>. <a href="https://www.geeksforgeeks.org/inter-process-communication-ipc/" class="bare" target="_blank" rel="noopener">https://www.geeksforgeeks.org/inter-process-communication-ipc/</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
<script>
if (!hljs.initHighlighting.called) {
hljs.initHighlighting.called = true
;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
}
</script>
</body>
</html>