diff options
| author | seh <henesy.dev@gmail.com> | 2019-03-18 15:42:43 -0500 |
|---|---|---|
| committer | seh <henesy.dev@gmail.com> | 2019-03-18 15:42:43 -0500 |
| commit | 577f8a21ded3a49c799ee49b90508801f1361842 (patch) | |
| tree | 6a6023343ba7fd3653e5a574dafe904606eb85ac /Spawn | |
| parent | 5bd50f13c315024164a221eb5a86edabf412b4d1 (diff) | |
add spawn and channels examples
Diffstat (limited to 'Spawn')
| -rw-r--r-- | Spawn/README.md | 39 | ||||
| -rw-r--r-- | Spawn/spawn.b | 43 |
2 files changed, 82 insertions, 0 deletions
diff --git a/Spawn/README.md b/Spawn/README.md new file mode 100644 index 0000000..ed942d7 --- /dev/null +++ b/Spawn/README.md @@ -0,0 +1,39 @@ +# Spawn + +Limbo supports the creation of processes operating on a given function via the `spawn` statement. Functions passed to spawn cannot have a return value. + +Inferno processes share memory with their parent processes which spawn them, bar the process's respective stack. Inferno processes are pre-emptively scheduled by the Inferno kernel. These processes are analogous, but not equivocal to, threads in unix-like systems. + +Synchronization between processes is recommended to be done through [channels](../Channels). + +For further reading on potential inspirations for Inferno processes, see [rfork(2)](http://man.cat-v.org/9front/2/fork). + +## Source + +### spawn.b:31,40 + +This section spawns four processes all of which will attempt to print to their standard output. Two functions spawn running the `quacker()` function and two function spawn running the `summer()` function with varying arguments. + +Note: The call to `sleep()` in this context within the `init()` function. + +## Demo + + ; limbo spawn.b + ; spawn + quack! + Sum (2): 0 + quack! + Sum (3): 0 + Sum (2): 1 + Sum (3): 1 + Sum (2): 3 + Sum (3): 3 + Final sum (2): 3 + Sum (3): 6 + Final sum (3): 6 + ; + +## Exercises + +- How many different orders do you see the print statements occur in? +- What happens if you remove the sleep? diff --git a/Spawn/spawn.b b/Spawn/spawn.b new file mode 100644 index 0000000..b375f6c --- /dev/null +++ b/Spawn/spawn.b @@ -0,0 +1,43 @@ +implement Spawn; + +include "sys.m"; +include "draw.m"; + +sys: Sys; +print: import sys; + +Spawn: module { + init: fn(nil: ref Draw->Context, nil: list of string); +}; + +summer(n: int) { + sum := 0; + + for(i := 0; i <= n; i++) { + sum += i; + print("Sum (%d): %d\n", n, sum); + } + + print("Final sum (%d): %d\n", n, sum); +} + +quacker() { + print("quack!\n"); +} + +init(nil: ref Draw->Context, nil: list of string) { + sys = load Sys Sys->PATH; + + spawn quacker(); + + spawn summer(2); + + spawn quacker(); + + n := 3; + spawn summer(n); + + sys->sleep(10); + + exit; +} |
