Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- non Lwt version:
- let () =
- let parent_pid = Unix.getpid () in
- Printf.sprintf "Parent pid:%d" parent_pid |> print_endline;
- let some_func () =
- Printf.sprintf "Process: %d" (Unix.getpid ())
- |> print_endline
- in
- let marshaled = Marshal.to_bytes some_func [Marshal.Closures] in
- let (read, write) = Unix.pipe () in
- let pid = Unix.fork () in
- (* Child *)
- if pid = 0 then begin
- let in_chan = Unix.in_channel_of_descr read in
- let func : unit -> unit = Marshal.from_channel in_chan in
- func ()
- end
- else begin
- Unix.write write marshaled 0 (Bytes.length marshaled) |> ignore;
- some_func ();
- exit 0
- end
- prints:
- Parent pid:3760
- Process: 3760
- Process: 3761
- Lwt version:
- let program =
- let parent_pid = Unix.getpid () in
- Printf.sprintf "Parent pid:%d" parent_pid
- |> Lwt_io.printl
- |> Lwt.ignore_result;
- let some_func () =
- Printf.sprintf "Process: %d" (Unix.getpid ())
- |> Lwt_io.printl
- in
- let marshaled = Marshal.to_bytes some_func [Marshal.Closures] in
- let (read, write) = Lwt_unix.pipe () in
- let pid = Lwt_unix.fork () in
- (if pid = 0 then begin
- let in_chan = Unix.in_channel_of_descr (Lwt_unix.unix_file_descr read) in
- let func : unit -> unit Lwt.t = Marshal.from_channel in_chan in
- func ()
- end
- else begin
- Lwt_unix.write write marshaled 0 (Bytes.length marshaled)
- |> Lwt.ignore_result
- |> some_func
- |> Lwt.ignore_result;
- exit 0
- end)
- let () = Lwt_main.run program
- Parent pid:3648
- Process: 3648
- Parent pid:3648
- Process: 3649
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement