> For the complete documentation index, see [llms.txt](https://docs.sacruda.nl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sacruda.nl/developers/sacruda-apis/process-handler.md).

# Process Handler

Sacruda has a sophisticated kernel module for handling processes: the **ProcessHandler**. This module handles the spawning and killing of processes. To render an application, it uses the **AppRenderer**, a process that's spawned before the Sacruda boot screen appears.

### Spawning a process

To spawn another application, use the `AppProcess.spawnApp` or `AppProcess.spawnOverlayApp` functions, which are part of every app process:

```typescript
spawnApp<T = AppProcess>(id: string, parentPid?: number | undefined, ...args: any[]): Promise<T | undefined>;
spawnOverlayApp<T = AppProcess>(id: string, parentPid?: number | undefined, ...args: any[]): Promise<T | undefined>;
```

{% hint style="info" %}
The `spawnApp` and `spawnOverlayApp` methods are asynchronous, they have to be awaited to prevent process stack conflicts.
{% endhint %}

For example, to open a file in the **Writer** app with the current process as the parent, you'd call `spawnApp` as such:

```javascript
const path = "U:/Documents/test.txt";
const process = await this.spawnApp("writer", this.pid, path);
```

If successful, the functions return the process that was just created. To see what these processes contain, consult the `IProcess` and `IAppProcess` interfaces in the `arcos.d.ts` file, located in the root of your project.

### Killing a process

To kill a process, use `ProcessHandler.kill`. This function terminates the process and its children. Here's the syntax:

```typescript
kill(pid: number, force?: boolean): Promise<ProcessKillResult>;
```

{% hint style="info" %}
The `kill` method is asynchronous, it has to be awaited.
{% endhint %}

It can be accessed by referencing the global `stack` TPA prop:

{% code lineNumbers="true" %}

```typescript
const pid = foo.bar.pid;
const result = await stack.kill(pid);
```

{% endcode %}

`ProcessKillResult` can be any one of these strings:

<table><thead><tr><th width="168">Result</th><th>Description</th></tr></thead><tbody><tr><td>success</td><td>The process was killed successfully.</td></tr><tr><td>err_elevation</td><td>Elevation is required, but wasn't provided</td></tr><tr><td>err_criticalProcess</td><td>The process is required for Sacruda to run properly.</td></tr><tr><td>err_disposed</td><td>The process is already killed</td></tr><tr><td>err_noExist</td><td>The process doesn't exist</td></tr><tr><td>err_killCancel</td><td>The process didn't permit the kill. It might still have pending operations.</td></tr></tbody></table>

### Getting a process

If you need to get a process instance, use the `ProcessHandler.getProcess` method. It takes a PID and returns the process if it can be found:

```typescript
getProcess<T = Process>(pid: number, disposedToo?: boolean): T | undefined;
```

`disposedToo` decides if the method can return a process that has already been killed. Disposed processes remain on the stack with their `_disposed` property set to `true`.

{% code lineNumbers="true" %}

```typescript
const process = stack.getProcess(pid);
```

{% endcode %}
