-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create processes and waitpid with Miou #49
Comments
Miou is currently based on certain Picos principles such as Triggers and Computations. In addition, Miou can manage signals via Miou.sys_signal. It might be conceivable to combine the two in order to continue a task if a process receives such a signal from the system. let () = Miou.run ~domains:0 @@ fun () ->
let c = Miou.Computation.create () in
let fn _sigchld = assert (Miou.Computation.try_return c ()) in
let _ = Miou.sys_signal Sys.sigchld (Sys.Signal_handle fn) in
Miou.Computation.await_exn c $ ocamlfind opt -linkpkg -package miou main.ml
$ ./a.out &
[1] xxxxx
$ kill -SIGCHLD $!
[1] + xxxxx done ./a.out Note that dom0 tries to observe any changes in the system and that's all it does - it loops until something happens.
Miou.syscall is higher level and concerns interactions with a function such as
I really recommend reading this short book, which explains the basics of asynchronous programming and Miou's objectives. This book will be extended with concrete examples, but I'm currently working on other writing projects.
From what I can tell and as far as the exception Timeout
open Bos
let () = Miou_unix.run ~domains:1 @@ fun () ->
let spawn seconds () =
let cmd = Cmd.(v "sleep" % (Fmt.str "%d" seconds)) in
let res = OS.Cmd.run_out cmd in
match OS.Cmd.to_null res with
| Ok () -> ()
| Error (`Msg msg) -> failwith msg in
let with_timeout fn seconds =
let prm0 = Miou.async @@ fun () -> Miou_unix.sleep seconds; raise Timeout in
let prm1 = Miou.call fn in
Miou.await_first [ prm0; prm1 ] in
match with_timeout (spawn 6) 5. with
| Ok () -> ()
| Error exn -> raise exn In this example, the command is run on a domain that is already launched by Miou (via $ ocamlfind opt -linkpkg -package bos,miou.unix test.ml
$ ./a.out
Fatal error: exception Test.Timeout The disadvantage of this proposal is that the program must run with at least 2 cores. Otherwise, More generally, Miou is more rudimentary than other schedulers. That doesn't mean it can't be used to create an http server, an email client or even a unikernel! The idea is to take an interest in the basics, so that you can work with other projects in a home-made way.
Unfortunately, decisions (which are not mine) have been taken on this subject which exclude Miou from any joint efforts to ensure compatibility with Picos. There is, however, a PR (#26) that begins a form of compatibility, feel free to complete it and I can help in the best way I can.
Thank you for taking the time to test Miou! |
It didn't occur to me that blocking calls run in another domain would not block the whole process. Now that you are showing these examples, this makes sense! I suppose my brain is still not quite on OCaml 5 yet! This actually gives me pause, because I suspect that there is a class of executables that probably can go a long way simply by using Back to the subject at hand, for now I would be OK assuming the presence of at least one extra core, and explored a first miou backend for vcs based on running blocking calls in a I looked a bit in tests in conditions that result in I am likely to have follow-up questions about |
As far as
There is an invariant in Miou where let () = Miou.run @@ fun () ->
let rec fn () =
assert ((Stdlib.Domain.self () :> int) <> 0);
let prm = Miou.call @@ fun () ->
assert ((Stdlib.Domain.self () :> int) <> 0) in
Miou.await_exn prm in
let domains = Miou.Domain.available () in
Miou.parallel fn (List.init domains (Fun.const ()))
|> List.iter (function Ok () -> () | Error exn -> raise exn) Another invariant is that the domain (randomly) chosen to execute the task by let () = Miou.run @@ fun () ->
let p =
Miou.call @@ fun () ->
let v = Stdlib.Domain.self () in
let q = Miou.call @@ fun () -> Stdlib.Domain.self () in
(v, Miou.await_exn q)
in
let v, u = Miou.await_exn p in
assert (v <> u) In the example above, It is important to understand that So, 2 tasks let dom1 = Miou.Domain.Uid.of_int 1
let () = Miou.run @@ fun () ->
let prm0 = Miou.call ~pin:dom1 @@ fun () ->
assert ((Stdlib.Domain.self () :> int) = 1)in
let prm1 = Miou.call ~pin:dom1 @@ fun () ->
assert ((Stdlib.Domain.self () :> int) = 1)in
Miou.await_exn prm0;
Miou.await_exn prm1 NOTE: It's advisable not to use |
Very interesting about the use of 3 domains for By the way, while reading more about domains and cores, I had a little exchange with a robot. Here is an extract from it: mbarbin:
robot:
Do you agree with the robot, or is it hallucinating? I understand you lose true parallelism doing this, but if you focus on cooperation, does allocating 3 domains on a single core, with the |
To cut a long story short, it is indeed possible to allocate 3 domains even if you only have one core, but you will run into a contention problem in the kernel, which will try to make you believe that these domains are running at the same time when in fact it will decide to pause them frequently and run them one by one on the only core it has available. It's in this case that an asynchronous |
Atm it looks like I'll be doing some exploration of Miou with enough cores for this simpler approach to work OK, so for now it looks as if the async |
I would like to do some experimentation and learning with Miou. In particular, I'd like to run external processes and wait for them cooperatively. I've looked at the Picos implementation of waitpid as source of inspiration and I wonder if it is possible to implement something equivalent in Miou (e.g. could the
Miou.syscall
mechanism be used as an equivalent to Picos'Select.return_on_sigchld
?).I am very new to Miou, and still have a lot of reading and exploration to do. Perhaps it doesn't make sense, or would be very unusual, to want to run external processes in a Miou app. Let me know! On the other hand, perhaps this ability will be inherited in the future by the work to make Miou compatible with
Picos_io
? I don't know much at this time, and would be interested to read your thoughts on this, before I embark on an perilous journey. Thank you for Miou!The text was updated successfully, but these errors were encountered: