
# Shared Memory

> Zero-copy access to the guest's `/dev/shm`.

```js
const vm = await createKernel({ shm: true });
await vm.exec("sh", ["-c", "printf result > /dev/shm/output"]);

const output = await vm.shm.open("output"); // a file a guest program made
const data = await output.read();
const views = output.views(); // zero-copy, until the kernel runs again

const input = await vm.shm.create("input", 1 << 20); // /dev/shm/input in the guest
await input.write(0, bytes);
```

`/dev/shm` exists once the guest's init has mounted it, so wait for the guest before `create()` (an `exec()` waits for it).

`openId(id)` opens a memfd that a guest program exported through `/dev/kwshm`.

`views()` and `readSync()` work where `vm.shm.inPlace` is true: inline, and in worker mode with several CPUs and shared memory (cross-origin isolated pages, Node). Elsewhere `read()` crosses to the kernel's worker.
