Packages
Multi-surface application runtime for Elixir. One TEA module renders to terminal, browser (LiveView), SSH, and MCP (agents). 30+ widgets, flexbox + CSS grid, AI agent runtime, distributed swarm with CRDTs, time-travel debugging, session recording, sandboxed REPL, and agentic commerce.
Current section
Files
Jump to
Current section
Files
examples/guides/02_core_concepts/terminal/ProcessManagement.md
---
title: Process Management Component
description: Documentation for the process management component in Raxol Terminal Emulator
date: 2023-04-04
author: Raxol Team
section: components
tags: [process, terminal, documentation]
---
# Process Management Component
The process management component handles terminal process creation, monitoring, and control.
## Features
- Process creation and execution
- Process monitoring
- Process termination
- Signal handling
- Environment variables
- Working directory management
- Process groups
- Job control
- Process priority
- Resource limits
## Usage
```elixir
# Create a new process manager
processes = Raxol.Terminal.Processes.new()
# Start a new process
{:ok, pid} = Raxol.Terminal.Processes.spawn(processes, "ls", ["-la"])
# Send signal to process
:ok = Raxol.Terminal.Processes.signal(processes, pid, :sigterm)
# Wait for process completion
{:ok, status} = Raxol.Terminal.Processes.wait(processes, pid)
```
## Configuration
The process manager can be configured with the following options:
```elixir
config = %{
shell: "/bin/bash",
env: %{
"TERM" => "xterm-256color",
"LANG" => "en_US.UTF-8"
},
cwd: "/home/user",
process_limit: 100,
default_priority: 0
}
processes = Raxol.Terminal.Processes.new(config)
```
## Implementation Details
### Process Types
1. **Shell Processes**
- Interactive shells
- Command execution
- Shell integration
- Shell history
2. **Background Processes**
- Daemon processes
- Job control
- Process groups
- Session management
3. **Special Processes**
- PTY master/slave
- Signal handlers
- Zombie reaping
- Init process
### Process Management
1. **Lifecycle Management**
- Process creation
- Process monitoring
- Process termination
- Exit status handling
2. **Resource Management**
- CPU scheduling
- Memory limits
- File descriptors
- Process priorities
### Process State
1. **Process Information**
- Process ID
- Parent process
- Process group
- Session ID
2. **Resource Usage**
- CPU usage
- Memory usage
- Open files
- Network connections
## API Reference
### Process Management
```elixir
# Initialize process manager
@spec new() :: t()
# Spawn new process
@spec spawn(processes :: t(), command :: String.t(), args :: [String.t()]) :: {:ok, pid()} | {:error, String.t()}
# Terminate process
@spec terminate(processes :: t(), pid :: pid()) :: :ok | {:error, String.t()}
# Wait for process
@spec wait(processes :: t(), pid :: pid()) :: {:ok, integer()} | {:error, String.t()}
```
### Process Control
```elixir
# Send signal to process
@spec signal(processes :: t(), pid :: pid(), signal :: atom()) :: :ok | {:error, String.t()}
# Set process priority
@spec set_priority(processes :: t(), pid :: pid(), priority :: integer()) :: :ok | {:error, String.t()}
# Get process info
@spec get_info(processes :: t(), pid :: pid()) :: {:ok, map()} | {:error, String.t()}
```
### Environment Management
```elixir
# Set environment variable
@spec set_env(processes :: t(), key :: String.t(), value :: String.t()) :: t()
# Get environment variable
@spec get_env(processes :: t(), key :: String.t()) :: {:ok, String.t()} | :error
# Set working directory
@spec set_cwd(processes :: t(), path :: String.t()) :: :ok | {:error, String.t()}
```
## Events
The process management component emits the following events:
- `:process_spawned` - When a new process is created
- `:process_exited` - When a process terminates
- `:process_signaled` - When a signal is sent to a process
- `:process_error` - When a process encounters an error
- `:env_changed` - When environment variables change
- `:cwd_changed` - When working directory changes
## Example
```elixir
defmodule MyTerminal do
alias Raxol.Terminal.Processes
def example do
# Create a new process manager
processes = Processes.new()
# Configure environment
processes = processes
|> Processes.set_env("TERM", "xterm-256color")
|> Processes.set_env("PATH", "/usr/local/bin:/usr/bin")
# Start a shell process
{:ok, shell_pid} = Processes.spawn(processes, "/bin/bash", [])
# Start a background process
{:ok, bg_pid} = Processes.spawn(processes, "long-running-task", [])
# Monitor process state
{:ok, info} = Processes.get_info(processes, bg_pid)
IO.inspect(info.status)
# Send signal to process
:ok = Processes.signal(processes, bg_pid, :sigterm)
# Wait for process to exit
{:ok, status} = Processes.wait(processes, bg_pid)
end
end
```
## Testing
The process management component includes comprehensive tests:
```elixir
defmodule Raxol.Terminal.ProcessesTest do
use ExUnit.Case
alias Raxol.Terminal.Processes
test "spawns processes correctly" do
processes = Processes.new()
{:ok, pid} = Processes.spawn(processes, "echo", ["test"])
{:ok, status} = Processes.wait(processes, pid)
assert status == 0
end
test "handles signals correctly" do
processes = Processes.new()
{:ok, pid} = Processes.spawn(processes, "sleep", ["10"])
:ok = Processes.signal(processes, pid, :sigterm)
{:ok, status} = Processes.wait(processes, pid)
assert status != 0
end
test "manages environment variables" do
processes = Processes.new()
processes = Processes.set_env(processes, "TEST", "value")
assert {:ok, "value"} = Processes.get_env(processes, "TEST")
end
test "handles working directory changes" do
processes = Processes.new()
:ok = Processes.set_cwd(processes, "/tmp")
{:ok, cwd} = Processes.get_cwd(processes)
assert cwd == "/tmp"
end
end
```