Limbo Tips

Limbo Tips

子プロセスを待つ


UNIXではwaitpid()やsigaction()などを組み合わせて待っていましたが、 Inferno/Limboでは、チャネルがブロックすることを利用してスマートに 子プロセスを待つことができます。

implement  Pid;

include "sys.m";
	sys:	Sys;

include "draw.m";

Pid: module {
	init:	fn (ctxt: ref Draw->Context, argv: list of string);
};

init (nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;

	sys->print("Pid test program started...\n");

	pidc := chan of int;

	spawn child(pidc);

	pid := sys->pctl(0, nil);
	sys->print("Parent:	My pid is [%d]\n", pid);

	chpid := <- pidc;
	sys->print("Parent:	Child pid is [%d]\n", chpid);
}

child(pidc: chan of int)
{
	pid := sys->pctl(0, nil);
	sys->print("Child:	My pid is [%d]\n", pid);

	pidc <-= pid;

	exit;
}

実行結果

; ./pid
Pid test program started...
Parent: My pid is [66]
Child:  My pid is [67]
Parent: Child pid is [67]
;

[PR]bVヤ震炸賞歡:KIIPGET