| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Starting, ending, and signals - More Raz</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Starting, ending, and signals - More Raz"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | <li><a href="collections.html">Lists, arrays, maps and sorting</a></li> |
| 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| 28 | </ol> |
| 29 | <h3>Standing alone</h3><ol start='7'> |
| 30 | <li><a href="static.html">A program with no run time</a></li> |
| 31 | <li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li> |
| 32 | <li><a href="lifecycle.html" aria-current="page">Starting, ending, and signals</a></li> |
| 33 | <li><a href="targets.html">One module, one file per target</a></li> |
| 34 | </ol> |
| 35 | <h3>The machine</h3><ol start='11'> |
| 36 | <li><a href="asm.html">Procedures in assembly</a></li> |
| 37 | <li><a href="syscalls.html">System calls</a></li> |
| 38 | <li><a href="listing.html">What the compiler makes</a></li> |
| 39 | </ol> |
| 40 | <h3>Shared memory</h3><ol start='14'> |
| 41 | <li><a href="volatile.html">Volatile</a></li> |
| 42 | <li><a href="atomics.html">Atomics</a></li> |
| 43 | <li><a href="ordering.html">Ordering and publication</a></li> |
| 44 | <li><a href="lockfree.html">A lock-free stack</a></li> |
| 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | </ol></aside><article><p class="kicker">Standing alone · lesson 9 of 18</p><h1>Starting, ending, and signals</h1> |
| 47 | <p>With no run time, the life of a program is short enough to say in a few lines. What happens, in order:</p> |
| 48 | <p>1. The entry code sets up a stack for signal handlers and installs the handlers for <code>SIGSEGV</code> and <code>SIGFPE</code>. A program that uses C starts through C instead, so that C's own set-up runs. 2. The bodies of the modules run, each once, imported modules first. 3. The main module's <code>main</code> runs. 4. <code>Rt.exit</code> runs the procedures registered with <code>Rt.atExit</code>, the last registered first, and ends the program with <code>main</code>'s status.</p> |
| 49 | <h2>Bodies, then main</h2> |
| 50 | <p>The first tour showed bodies running before <code>main</code>. They are the place to set a module up, and they can end early with <code>return;</code>.</p> |
| 51 | <div class="file"><span>Order.raz</span></div><pre class="code"><code><span class="kw">module</span> Order; |
| 52 | <span class="kw">import</span> Out; |
| 53 | |
| 54 | <span class="kw">var</span> steps*: <span class="ty">INT</span>; |
| 55 | |
| 56 | <span class="kw">proc</span> note(what: []<span class="kw">const</span> <span class="ty">U8</span>); |
| 57 | <span class="kw">begin</span> |
| 58 | <span class="ty">INC</span>(steps); |
| 59 | Out.int(steps); Out.str(<span class="string">". "</span>); Out.str(what); Out.ln; |
| 60 | <span class="kw">end</span>; |
| 61 | |
| 62 | <span class="kw">begin</span> |
| 63 | note(<span class="string">"the body runs before main"</span>); |
| 64 | <span class="kw">if</span> steps > <span class="number">5</span> <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 65 | note(<span class="string">"and carries on if it isn't ended early"</span>); |
| 66 | <span class="kw">end</span> Order.</code></pre> |
| 67 | <pre class="output"><code>1. the body runs before main |
| 68 | 2. and carries on if it isn't ended early |
| 69 | </code></pre> |
| 70 | <p>That file is a module with a body and no <code>main</code>: it can be imported, or its body run as a program. Add the <code>main</code> and see both:</p> |
| 71 | <div class="file"><span>Both.raz</span></div><pre class="code"><code><span class="kw">module</span> Both; |
| 72 | <span class="kw">import</span> Out, Order; |
| 73 | |
| 74 | <span class="kw">proc</span> main*; |
| 75 | <span class="kw">begin</span> |
| 76 | Out.str(<span class="string">"main: "</span>); Out.int(Order.steps); Out.str(<span class="string">" steps happened first\n"</span>); |
| 77 | <span class="kw">end</span>; |
| 78 | |
| 79 | <span class="kw">end</span> Both.</code></pre> |
| 80 | <pre class="output"><code>1. the body runs before main |
| 81 | 2. and carries on if it isn't ended early |
| 82 | main: 2 steps happened first |
| 83 | </code></pre> |
| 84 | <h2>Things to do at the end</h2> |
| 85 | <p><code>Rt.atExit(p)</code> registers a procedure with no parameters to run when the program ends by <code>Rt.exit</code>: at the end of <code>main</code>, by a call to <code>Rt.exit</code>, or by a failed check, after its message. It is where a program restores a terminal or removes a lock file. At most 32 are registered, and each runs at most once. Out's buffer is flushed this way, which is why a program that ends in a failure still shows what it printed.</p> |
| 86 | <div class="file"><span>Cleanup.raz</span></div><pre class="code"><code><span class="kw">module</span> Cleanup; |
| 87 | <span class="kw">import</span> Out, Rt; |
| 88 | |
| 89 | <span class="kw">proc</span> first; |
| 90 | <span class="kw">begin</span> |
| 91 | Out.str(<span class="string">"registered first, runs last\n"</span>); |
| 92 | <span class="kw">end</span>; |
| 93 | |
| 94 | <span class="kw">proc</span> second; |
| 95 | <span class="kw">begin</span> |
| 96 | Out.str(<span class="string">"registered second, runs first\n"</span>); |
| 97 | <span class="kw">end</span>; |
| 98 | |
| 99 | <span class="kw">proc</span> main*; |
| 100 | <span class="kw">begin</span> |
| 101 | _ := Rt.atExit(first); |
| 102 | _ := Rt.atExit(second); |
| 103 | Out.str(<span class="string">"main is done\n"</span>); |
| 104 | <span class="kw">end</span>; |
| 105 | |
| 106 | <span class="kw">end</span> Cleanup.</code></pre> |
| 107 | <pre class="output"><code>main is done |
| 108 | registered second, runs first |
| 109 | registered first, runs last |
| 110 | </code></pre> |
| 111 | <h2>Two ways to leave in a hurry</h2> |
| 112 | <p><code>Rt.exit(status)</code> ends the program after the <code>atExit</code> procedures. <code>Sys.exit(status)</code> ends it at once, without them. A failed check and <code>Rt.exit</code> also skip the <code>defer</code> statements still pending in the calls under way. <code>Sys.exit</code> is for a child process that must not run what it inherited, and for nothing else.</p> |
| 113 | <div class="file"><span>Hurry.raz</span></div><pre class="code"><code><span class="kw">module</span> Hurry; |
| 114 | <span class="kw">import</span> Out, Rt, Sys; |
| 115 | |
| 116 | <span class="kw">proc</span> bye; |
| 117 | <span class="kw">begin</span> |
| 118 | Out.str(<span class="string">"atExit ran\n"</span>); |
| 119 | <span class="kw">end</span>; |
| 120 | |
| 121 | <span class="kw">proc</span> main*; |
| 122 | <span class="kw">begin</span> |
| 123 | _ := Rt.atExit(bye); |
| 124 | <span class="kw">defer</span> Out.str(<span class="string">"a deferred statement ran\n"</span>); |
| 125 | Out.str(<span class="string">"leaving\n"</span>); |
| 126 | Out.flush; |
| 127 | Sys.exit(<span class="number">3</span>); |
| 128 | <span class="kw">end</span>; |
| 129 | |
| 130 | <span class="kw">end</span> Hurry.</code></pre> |
| 131 | <pre class="output term"><code><span class="cmd">$ raz Hurry.raz</span> |
| 132 | <span class="cmd">$ ./Hurry; echo "status $?"</span> |
| 133 | leaving |
| 134 | status 3</code></pre> |
| 135 | <p>Neither the deferred statement nor <code>bye</code> ran. That's the rule, and the reason to keep cleanup that matters (a terminal, a lock file) in <code>atExit</code> and use <code>Rt.exit</code> for ending.</p> |
| 136 | <h2>Signals</h2> |
| 137 | <p>A program is stopped by a signal it doesn't catch, with a message that names the place for <code>SIGSEGV</code> and <code>SIGFPE</code>, and without one for the others, and ends with status 2. A program that wants to handle one, say <code>SIGTERM</code> for a clean shutdown, doesn't write a handler that runs at an arbitrary moment. It asks for the signals to arrive as data: <code>Sys.catch</code> takes a list of signals and gives a descriptor that is ready to read when one comes, and <code>Sys.caught</code> says which. So the program waits for signals the way it waits for input.</p> |
| 138 | <div class="file"><span>Terms.raz</span></div><pre class="code"><code><span class="kw">module</span> Terms; |
| 139 | <span class="kw">import</span> Out, Sys; |
| 140 | |
| 141 | <span class="kw">proc</span> main*; |
| 142 | <span class="kw">var</span> fd, sig: <span class="ty">INT</span>; |
| 143 | <span class="kw">begin</span> |
| 144 | fd := Sys.catch([Sys.sigTerm, Sys.sigWinch]); |
| 145 | Out.str(<span class="string">"asking myself to stop\n"</span>); |
| 146 | _ := Sys.kill(Sys.pid(), Sys.sigTerm); |
| 147 | <span class="kw">if</span> Sys.ready(fd, <span class="number">1000</span>) <span class="kw">then</span> |
| 148 | sig := Sys.caught(fd); |
| 149 | <span class="kw">if</span> sig = Sys.sigTerm <span class="kw">then</span> Out.str(<span class="string">"caught SIGTERM, stopping cleanly\n"</span>); <span class="kw">end</span>; |
| 150 | <span class="kw">end</span>; |
| 151 | sig := Sys.caught(fd); |
| 152 | Out.int(sig); Out.str(<span class="string">" waiting after that\n"</span>); <span class="comment">(* 0: none *)</span> |
| 153 | <span class="kw">end</span>; |
| 154 | |
| 155 | <span class="kw">end</span> Terms.</code></pre> |
| 156 | <pre class="output"><code>asking myself to stop |
| 157 | caught SIGTERM, stopping cleanly |
| 158 | 0 waiting after that |
| 159 | </code></pre> |
| 160 | <p><code>Sys.ready(fd, ms)</code> waits up to <code>ms</code> milliseconds for something to read; with <code>Sys.readyOf</code> it waits on several descriptors, so one loop serves a socket, a terminal and a stop signal together. <code>Sys.ignore(sig, true)</code> makes a signal do nothing, as a shell ignores <code>SIGTTOU</code>.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="linkstatic.html">← Threads without C, and keeping the two apart</a><a href="index.html">Contents</a><a class="next" href="targets.html">One module, one file per target →</a></div></article></div> |
| 161 | </main> |
| 162 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 163 | </body> |
| 164 | </html> |