If a program creates a bigarray, then mmap it to a file, then create children
process by fork(), is this a correct way to share in read/write mode
the said bigarray?
Conceptually, I think it should work.
I am interested by processes, not threads.
Constraints: children will not resize the bigarray, they will also not write at the same indexes.
I.e. each children will work on a “slice” of the bigarray.
I am planning to have the children initialize the bigarray in parallel.
Then, after all children are done, I hope the parent process
will see the bigarray as fully initialized.
Thanks,
F.
1 Like
Answer to self: yes it works.
module BA = Bigarray
module BA1 = BA.Array1
open Printf
(* partial init of BA by child process *)
let child_fun ba start stop =
for i = start to stop - 1 do
BA1.set ba i (float i)
done
let () =
let tmp_fn = Filename.temp_file "" "" in
let fd = Unix.(openfile tmp_fn [O_RDWR] 0o600) in
let n = 10 in
let ba1 = BA.array1_of_genarray (Unix.map_file fd BA.Float64 BA.c_layout true [|n|]) in
let pid0 = Unix.fork () in
(match pid0 with
| 0 -> (* 1st child *)
(child_fun ba1 0 (n/2);
exit 0)
| _ -> (* father *) ()
);
let pid1 = Unix.fork () in
(match pid1 with
| 0 -> (* 2nd child *)
(child_fun ba1 (n/2) n;
exit 0)
| _ -> (* father *) ()
);
let _ = Unix.wait () in
let _ = Unix.wait () in
printf "father can see:\n";
for i = 0 to n - 1 do
printf "%f\n" (BA1.get ba1 i)
done
Prints:
father can see:
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
1 Like