Plan B User's Manual


spawn, die ­ process handling functions
#include <ulib.h>

typedef void (*Spawnf)(char*, char*);
void spawn(char* arg0, char* args, int dup, Spawnf f, char* a)
void tspawn(void (*f)(void*), void *a, char* sel)
void die(char* reason)


Spawn is a convenience function used to start a new process. It uses the proc(4) boxes to do so. The program executed by the new process is arg0 , with the command line set as passed in args . Before returning to the spawn copies to args the name of the box for the process just started.

Dup is a set of bits used to control how virtual memory, name space, and program are initialized:
DPROG   when set, causes the program arg0 to be copied (not linked) to the new process program box. Even when DPROC is not set, the program will be copied if the link operation fails—to allow callers to try link(2) and still get the process created when the program does not share the machine with the processor. DVM     when set, causes the /b/proc/me/vm virtual memory box to be copied (not linked) to the new process.
DNS     when set, causes the /b/proc/me/ns name space to be copied (not linked) to the process.

If the parameter f is nil, a is considered to be the name for a box to link to the process legacy; it may be nil. Otherwise, f is a function to be called before copying/linking the program, and a is an argument to be

given to f. This is useful to modify the new process environment before it is allowed to run.

Tspawn is another convenience function used to create a process that shares the virtual memory with the caller (i.e. what other systems would call a kernel thread). The user must supply the startint address for the thread, f, and its initial argument. Before returning to the caller, tspawn copies to sel the name of the box for the process just started, in case sel was not nil.

Die is a convenience function that terminates process execution. The string given is copied to the process legacy box.


char buf[MAXSEL];
strcpy(buf, "sh");
maken("/b/term/shterm!text"nil, nil);
spawn("sh", buf, DVM, termio, term);
spawn("sh", buf, DVM, nil, "/b/chan/towait");
copy("/b/chan/towait", "/b/proc/me/io1");
The first call to spawn creates a new process to execute /bin/sh so that it runs at a different address space. The I/O boxes for the new process are set by termio, which is supposed to be a function that links I/O boxes in the new process to the argument given (that is shterm in the example).

The second call to spawn creates a process using the same I/O devices in use by the caller process. Here, the new process legacy box gets linked to a channel, so that the copy(2) call would both block until the new process terminates and copy the process legacy to the stardard output device.


/src/b/port/ulib.c and /src/b/port/plib.c

See also

proc(4) and sh(1).

Plan B User's Manual. First edition.