sylveos

Toy Operating System
Log | Files | Refs

commit e250c356eea26ef63234d8d2798e3299daeb9806
parent 983567d8f6eacfe06970a6e28110ccdcb9cf6ab2
Author: Sylvia Ivory <git@sivory.net>
Date:   Thu, 12 Mar 2026 20:11:32 -0700

Process outline

Diffstat:
Asylveos/process.zig | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/sylveos/process.zig b/sylveos/process.zig @@ -0,0 +1,57 @@ +const std = @import("std"); +const pi = @import("pi"); + +const Registers = pi.interrupts.Registers; +const ProcMap = pi.procmap.ProcMap; +const vfs = pi.fs.vfs; + +var processes: std.DoublyLinkedList = .{}; +var allocator: std.mem.Allocator = undefined; +var pid_counter: usize = 1; + +// TODO; virtual handles (stdin, stdout, stderr) +pub const Handle = struct { + entry: vfs.DirectoryEntry, + closed: bool, +}; + +pub const Process = struct { + pid: usize, + node: std.DoublyLinkedList.Node, + registers: Registers, + proc_map: ProcMap, + handles: std.ArrayList(Handle), + + pub fn close(self: *Process) void { + allocator.free(self.proc_map.pt); + + for (self.handles.items) |handle| { + if (handle.closed) continue; + handle.entry.close(); + } + + self.handles.deinit(allocator); + allocator.destroy(self); + } +}; + +pub fn init(alloc: std.mem.Allocator) !void { + allocator = alloc; +} + +pub fn destroy() void { + var process_node = processes.first; + + while (process_node) |n| { + process_node = n.next; + get_current().close(); + } +} + +fn get_current() *Process { + return @alignCast(@fieldParentPtr("node", processes.first.?)); +} + +pub fn get_pid() *Process { + return get_current().pid; +}