raz / site / public / advanced / processes.html

213 lines · 14.9 KiB · raw · history · last change de634aad

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>Time, randomness and other programs - 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="Time, randomness and other programs - 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" aria-current="page">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">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">The library &middot; lesson 5 of 18</p><h1>Time, randomness and other programs</h1>
47<p>Everything here is in <code>Sys</code>, and is the same procedures on every target raz supports: through system calls on Linux, through the C library elsewhere.</p>
48<h2>Clocks and sleeping</h2>
49<p><code>Sys.clock</code> is a clock that only goes forward, in nanoseconds: the one to measure a duration with. <code>Sys.now</code> is the wall clock, in nanoseconds since 1970 UTC, which can be set backwards, so it is for dates, not durations. <code>Sys.sleep</code> waits some milliseconds.</p>
50<div class="file"><span>Timing.raz</span></div><pre class="code"><code><span class="kw">module</span> Timing;
51<span class="kw">import</span> Out, Sys;
52
53<span class="kw">proc</span> main*;
54<span class="kw">var</span> t0, t1, elapsed: <span class="ty">I64</span>; wall: <span class="ty">I64</span>;
55<span class="kw">begin</span>
56 t0 := Sys.clock();
57 Sys.sleep(<span class="number">25</span>);
58 t1 := Sys.clock();
59 elapsed := (t1 - t0) <span class="kw">div</span> <span class="number">1000000</span>; <span class="comment">(* milliseconds *)</span>
60 <span class="kw">if</span> (elapsed &gt;= <span class="number">25</span>) <span class="kw">and</span> (elapsed &lt; <span class="number">1000</span>) <span class="kw">then</span>
61 Out.str(<span class="string">&quot;slept about 25 ms\n&quot;</span>);
62 <span class="kw">end</span>;
63 wall := Sys.now();
64 <span class="kw">if</span> wall &gt; <span class="number">1700000000000000000</span> <span class="kw">then</span> Out.str(<span class="string">&quot;after 2023\n&quot;</span>); <span class="kw">end</span>;
65<span class="kw">end</span>;
66
67<span class="kw">end</span> Timing.</code></pre>
68<pre class="output"><code>slept about 25 ms
69after 2023
70</code></pre>
71<p>Every duration is an <code>I64</code> of nanoseconds, because a 32-bit <code>INT</code> would hold only a couple of seconds of them. The library's rule is that what may overflow a small target's <code>INT</code> is an <code>I64</code>.</p>
72<h2>Random bytes</h2>
73<p><code>Sys.random</code> fills a slice with random bytes from the system. Anything else is made from that: a number in a range, a shuffle.</p>
74<div class="file"><span>Dice.raz</span></div><pre class="code"><code><span class="kw">module</span> Dice;
75<span class="kw">import</span> Out, Sys, Str;
76
77<span class="comment">(* a number from 0 up to n, without bias *)</span>
78<span class="kw">proc</span> below(n: <span class="ty">INT</span>): <span class="ty">INT</span>;
79<span class="kw">var</span> b: [<span class="number">8</span>]<span class="ty">U8</span>; x, limit: <span class="ty">U64</span>;
80<span class="kw">begin</span>
81 limit := <span class="number">0FFFFFFFFFFFFFFFFH</span> - (<span class="number">0FFFFFFFFFFFFFFFFH</span> <span class="kw">mod</span> <span class="ty">U64</span>(n)) - <span class="number">1</span>;
82 Sys.random(b);
83 x := Str.le(b);
84 <span class="kw">while</span> x &gt; limit <span class="kw">do</span>
85 Sys.random(b);
86 x := Str.le(b);
87 <span class="kw">end</span>;
88 <span class="kw">return</span> <span class="ty">INT</span>(x <span class="kw">mod</span> <span class="ty">U64</span>(n));
89<span class="kw">end</span>;
90
91<span class="kw">proc</span> main*;
92<span class="kw">var</span> counts: [<span class="number">6</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>;
93<span class="kw">begin</span>
94 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">6000</span> <span class="kw">do</span> <span class="ty">INC</span>(counts[below(<span class="number">6</span>)]); <span class="kw">end</span>;
95 ok := <span class="ty">true</span>;
96 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span>
97 <span class="kw">if</span> (counts[i] &lt; <span class="number">800</span>) <span class="kw">or</span> (counts[i] &gt; <span class="number">1200</span>) <span class="kw">then</span> ok := <span class="ty">false</span>; <span class="kw">end</span>;
98 <span class="kw">end</span>;
99 <span class="kw">if</span> ok <span class="kw">then</span> Out.str(<span class="string">&quot;all six faces came up about a thousand times\n&quot;</span>); <span class="kw">end</span>;
100<span class="kw">end</span>;
101
102<span class="kw">end</span> Dice.</code></pre>
103<pre class="output"><code>all six faces came up about a thousand times
104</code></pre>
105<h2>Running a program</h2>
106<p><code>Sys.spawn</code> starts the program at a path with its arguments, environment, working directory and standard streams in a <code>Sys.Program</code> record. A zero field means this program's own. <code>Sys.wait</code> waits for it to end and says how: <code>exited</code> with its status, <code>killed</code> by a signal, or <code>stopped</code>.</p>
107<div class="file"><span>Run.raz</span></div><pre class="code"><code><span class="kw">module</span> Run;
108<span class="kw">import</span> Out, Sys;
109
110<span class="kw">proc</span> main*;
111<span class="kw">var</span> p: Sys.Program; pid, status: <span class="ty">INT</span>; how: Sys.Status;
112<span class="kw">begin</span>
113 p := {path: <span class="string">&quot;/bin/echo&quot;</span>, args: [<span class="string">&quot;echo&quot;</span>, <span class="string">&quot;hello from a child&quot;</span>]};
114 pid := Sys.spawn(p);
115 <span class="kw">if</span> pid &lt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;can&#x27;t start\n&quot;</span>); <span class="kw">return</span>; <span class="kw">end</span>;
116 Out.flush; <span class="comment">(* so that our text isn&#x27;t behind the child&#x27;s *)</span>
117 _, how, status := Sys.wait(pid, <span class="ty">true</span>);
118 <span class="kw">if</span> how = Sys.exited <span class="kw">then</span>
119 Out.str(<span class="string">&quot;it exited with &quot;</span>); Out.int(status); Out.ln;
120 <span class="kw">end</span>;
121<span class="kw">end</span>;
122
123<span class="kw">end</span> Run.</code></pre>
124<pre class="output"><code>hello from a child
125it exited with 0
126</code></pre>
127<h2>Capturing its output</h2>
128<p>A program's standard output can be a pipe. <code>Sys.pipe</code> gives the descriptor to read and the one to write; give the write end to the child as <code>fds[1]</code>, close it here, and read until the end. This is how a program drives another: <code>ls</code>, a compiler, a script.</p>
129<div class="file"><span>Capture.raz</span></div><pre class="code"><code><span class="kw">module</span> Capture;
130<span class="kw">import</span> Out, Sys, Buf, Heap;
131
132<span class="kw">proc</span> main*;
133<span class="kw">var</span>
134 p: Sys.Program; pid, rd, wr, n, status: <span class="ty">INT</span>; how: Sys.Status;
135 chunk: [<span class="number">256</span>]<span class="ty">U8</span>; b: Buf.Buf;
136<span class="kw">begin</span>
137 rd, wr := Sys.pipe();
138 p := {path: <span class="string">&quot;/bin/sh&quot;</span>, args: [<span class="string">&quot;sh&quot;</span>, <span class="string">&quot;-c&quot;</span>, <span class="string">&quot;echo one; echo two; exit 7&quot;</span>]};
139 p.fds[<span class="number">1</span>] := wr;
140 pid := Sys.spawn(p);
141 Sys.close(wr); <span class="comment">(* only the child writes now *)</span>
142 Buf.init(b, Heap.heap);
143 <span class="kw">defer</span> Buf.done(b);
144 n := Sys.read(rd, chunk);
145 <span class="kw">while</span> n &gt; <span class="number">0</span> <span class="kw">do</span>
146 Buf.str(b, chunk[<span class="number">0</span>..n]);
147 n := Sys.read(rd, chunk);
148 <span class="kw">end</span>;
149 Sys.close(rd);
150 _, how, status := Sys.wait(pid, <span class="ty">true</span>);
151 Out.str(<span class="string">&quot;captured: &quot;</span>); Out.str(Buf.bytes(b));
152 Out.str(<span class="string">&quot;status &quot;</span>); Out.int(status); Out.ln;
153 <span class="kw">if</span> how = Sys.exited <span class="kw">then</span> Out.str(<span class="string">&quot;(a normal exit)\n&quot;</span>); <span class="kw">end</span>;
154<span class="kw">end</span>;
155
156<span class="kw">end</span> Capture.</code></pre>
157<pre class="output"><code>captured: one
158two
159status 7
160(a normal exit)
161</code></pre>
162<p>Closing the write end in the parent matters: the read ends only when every holder of the write end has closed it.</p>
163<h2>Environment and working directory</h2>
164<p><code>Sys.env("HOME")</code> is an environment variable's value, or an empty string if it is unset. <code>Sys.workDir</code> and <code>Sys.setWorkDir</code> read and change the directory. The <code>Sys.Program</code> record's <code>env</code>, <code>onlyEnv</code> and <code>dir</code> give a child its own.</p>
165<div class="file"><span>Where.raz</span></div><pre class="code"><code><span class="kw">module</span> Where;
166<span class="kw">import</span> Out, Sys;
167
168<span class="kw">proc</span> main*;
169<span class="kw">var</span> p: Sys.Program; pid, rd, wr, n: <span class="ty">INT</span>; chunk: [<span class="number">64</span>]<span class="ty">U8</span>;
170<span class="kw">begin</span>
171 <span class="kw">if</span> Sys.env(<span class="string">&quot;RAZ_SURELY_UNSET&quot;</span>) = <span class="string">&quot;&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;unset reads as empty\n&quot;</span>); <span class="kw">end</span>;
172 rd, wr := Sys.pipe();
173 p := {path: <span class="string">&quot;/bin/sh&quot;</span>, args: [<span class="string">&quot;sh&quot;</span>, <span class="string">&quot;-c&quot;</span>, <span class="string">&quot;echo $GREETING from $(pwd)&quot;</span>],
174 env: [<span class="string">&quot;GREETING=hello&quot;</span>], dir: <span class="string">&quot;/tmp&quot;</span>};
175 p.fds[<span class="number">1</span>] := wr;
176 pid := Sys.spawn(p);
177 Sys.close(wr);
178 n := Sys.read(rd, chunk);
179 Out.str(chunk[<span class="number">0</span>..n]);
180 Sys.close(rd);
181 _, _, _ := Sys.wait(pid, <span class="ty">true</span>);
182<span class="kw">end</span>;
183
184<span class="kw">end</span> Where.</code></pre>
185<pre class="output"><code>unset reads as empty
186hello from /tmp
187</code></pre>
188<h2>Signals</h2>
189<p>A signal, Ctrl-C's <code>sigInt</code> or <code>sigTerm</code> from <code>kill</code>, ends a program unless it says otherwise. <code>Sys.catch</code> takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with <code>Sys.readyOf</code>, waits for signals the same way; <code>Sys.caught</code> gives each in turn. <code>Sys.handle</code> is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for <code>Sys.came</code>, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns.</p>
190<div class="file"><span>Signals.raz</span></div><pre class="code"><code><span class="kw">module</span> Signals;
191<span class="kw">import</span> Out, Sys;
192
193<span class="kw">proc</span> main*;
194<span class="kw">var</span> fd: <span class="ty">INT</span>;
195<span class="kw">begin</span>
196 fd := Sys.catch([Sys.sigUsr1]);
197 _ := Sys.kill(Sys.pid(), Sys.sigUsr1);
198 <span class="kw">if</span> Sys.ready(fd, <span class="number">1000</span>) <span class="kw">and</span> (Sys.caught(fd) = Sys.sigUsr1) <span class="kw">then</span> Out.str(<span class="string">&quot;caught sigUsr1\n&quot;</span>); <span class="kw">end</span>;
199 _ := Sys.handle(Sys.sigUsr2);
200 _ := Sys.kill(Sys.pid(), Sys.sigUsr2);
201 <span class="kw">if</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">&quot;sigUsr2 came\n&quot;</span>); <span class="kw">end</span>;
202 <span class="kw">if</span> <span class="kw">not</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">&quot;and was seen once\n&quot;</span>); <span class="kw">end</span>;
203<span class="kw">end</span>;
204
205<span class="kw">end</span> Signals.</code></pre>
206<pre class="output"><code>caught sigUsr1
207sigUsr2 came
208and was seen once
209</code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="files.html">&larr; Files and the command line</a><a href="index.html">Contents</a><a class="next" href="allocators.html">Writing an allocator &rarr;</a></div></article></div>
210</main>
211<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
212</body>
213</html>