Skip to content

Implement fork/exec process creation system #93

@pbalduino

Description

@pbalduino

Goal

Implement fork() and exec() system calls to enable proper process creation and program execution in userspace.

Context

Fork/exec is the fundamental process creation mechanism in Unix-like systems. This is essential for the shell to launch programs, including Doom, and for general application execution in meniOS.

Definition of Done

  • fork() syscall: Create child process with copy-on-write memory
  • exec() family: Execute new programs (execve, execl, execp, etc.)
  • Process isolation: Proper memory space separation between parent/child
  • File descriptor inheritance: Handle FD inheritance and close-on-exec
  • Process hierarchy: Parent/child relationships and process trees
  • Resource management: Proper cleanup and resource allocation
  • Error handling: Comprehensive error handling for edge cases

Fork Implementation

Copy-on-Write (COW)

  • Virtual memory duplication with COW semantics
  • Page fault handler for COW page splitting
  • Efficient memory sharing between parent/child
  • Proper reference counting for shared pages
  • Memory protection and isolation

Process State Duplication

  • Thread/process control block copying
  • File descriptor table duplication
  • Signal handler inheritance
  • Working directory and environment inheritance
  • Process group and session handling

Return Value Handling

  • Return 0 to child process
  • Return child PID to parent process
  • Proper error handling and cleanup on failure

Exec Implementation

Program Loading

  • ELF loader integration for new program execution
  • Memory space replacement (not duplication)
  • Stack and heap initialization for new program
  • Entry point setup and initial register state
  • Command line and environment variable passing

File Descriptor Handling

  • Close-on-exec flag processing
  • File descriptor cleanup and inheritance
  • Standard streams (stdin/stdout/stderr) preservation
  • Resource limit inheritance

Security and Validation

  • Executable permission checking
  • Path resolution and validation
  • Security context transition
  • Resource limit enforcement

System Call Interface

pid_t fork(void);
int execve(const char *pathname, char *const argv[], char *const envp[]);
int execl(const char *pathname, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *pathname, const char *arg, ..., char *const envp[]);

Implementation Strategy

Phase 1: Basic Fork

  • Implement basic process duplication
  • Simple memory copying (before COW optimization)
  • Basic parent/child relationship
  • Integration with existing process management

Phase 2: Copy-on-Write

  • Implement COW page fault handling
  • Optimize memory sharing efficiency
  • Add proper reference counting
  • Performance optimization

Phase 3: Exec Family

  • Basic execve implementation
  • ELF loader integration
  • File descriptor and environment handling
  • Complete exec family implementation

Phase 4: Optimization and Polish

  • Performance optimization
  • Edge case handling
  • Integration with shell and applications
  • Comprehensive testing

Files to Create/Modify

  • syscall/fork.c - Fork system call implementation
  • syscall/exec.c - Exec family system call implementation
  • src/kernel/process_create.c - Process creation infrastructure
  • src/kernel/cow.c - Copy-on-write implementation
  • include/sys/wait.h - Process wait interface
  • src/kernel/elf_exec.c - ELF execution for exec

Technical Challenges

Memory Management

  • Efficient COW implementation
  • Page fault handling for COW pages
  • Memory space switching for exec
  • Resource cleanup on failure
  • Memory accounting and limits

Process Management

  • Process ID allocation and management
  • Parent/child relationship tracking
  • Orphan process handling
  • Resource inheritance and cleanup

Synchronization

  • Race condition prevention
  • SMP safety for process creation
  • Signal handling during fork/exec
  • File descriptor locking

Testing Strategy

  • Basic fork/exec functionality
  • Shell integration testing
  • Memory usage and COW efficiency
  • Error condition handling
  • Multi-process stress testing
  • Performance benchmarking

Performance Goals

  • Fork latency < 1ms for typical processes
  • COW overhead < 10% memory increase
  • Exec latency < 5ms for small programs
  • Scalable with process count
  • Efficient memory utilization

Dependencies

Related Issues

Use Cases

  • Shell launching programs
  • Application spawning child processes
  • System service management
  • Build systems and development tools
  • General Unix-like process model

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions