多任务

This commit is contained in:
2026-02-28 16:30:25 +08:00
parent 02ff8295ef
commit 58df162054
12 changed files with 560 additions and 20 deletions

184
System/Process.cs Normal file
View File

@@ -0,0 +1,184 @@
using System;
using System.Collections.Generic;
using Sys = Cosmos.System;
namespace CMLeonOS
{
public enum ProcessType
{
Service,
Application,
Background
}
public abstract class Process
{
private protected Process(string name, ProcessType type)
{
Name = name;
Type = type;
}
private protected Process(string name, ProcessType type, Process parent)
{
Name = name;
Type = type;
Parent = parent;
}
internal ulong Id { get; set; }
internal List<string> Args { get; private set; }
internal string Name { get; set; }
internal ProcessType Type { get; private set; }
internal DateTime Created { get; private set; } = DateTime.Now;
internal bool IsRunning { get; private set; } = false;
internal bool Swept { get; set; } = false;
internal bool Critical { get; set; } = false;
internal Process Parent { get; set; }
public virtual void Start()
{
if (Type == ProcessType.Service)
{
CMLeonOS.Logger.Logger.Instance.Info("Process", $"Service starting: {Name}");
}
IsRunning = true;
}
public abstract void Run();
public virtual void Stop()
{
if (Type == ProcessType.Service)
{
CMLeonOS.Logger.Logger.Instance.Info("Process", $"Service stopping: {Name}");
}
IsRunning = false;
foreach (Process process in ProcessManager.Processes)
{
if (process.Parent == this && process.IsRunning)
{
process.TryStop();
}
}
}
public void TryRun()
{
try
{
Run();
}
catch (Exception e)
{
if (Critical)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("========================================");
Console.WriteLine(" CRITICAL PROCESS CRASHED");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine($"Process: {Name}");
Console.WriteLine($"ID: {Id}");
Console.WriteLine();
Console.WriteLine("Error:");
Console.WriteLine(e.ToString());
Console.WriteLine();
Console.WriteLine("Press any key to reboot...");
Console.ReadKey();
Sys.Power.Reboot();
}
else
{
CMLeonOS.Logger.Logger.Instance.Error("Process", $"Process \"{Name}\" ({Id}) crashed: {e.ToString()}");
TryStop();
}
}
}
public void TryStart()
{
try
{
Start();
}
catch (Exception e)
{
if (Critical)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("========================================");
Console.WriteLine(" CRITICAL PROCESS CRASHED");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine($"Process: {Name}");
Console.WriteLine($"ID: {Id}");
Console.WriteLine();
Console.WriteLine("Error:");
Console.WriteLine(e.ToString());
Console.WriteLine();
Console.WriteLine("Press any key to reboot...");
Console.ReadKey();
Sys.Power.Reboot();
}
else
{
CMLeonOS.Logger.Logger.Instance.Error("Process", $"Process \"{Name}\" ({Id}) crashed while starting: {e.ToString()}");
TryStop();
}
}
}
public void TryStop()
{
try
{
Stop();
}
catch (Exception e)
{
IsRunning = false;
if (Critical)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("========================================");
Console.WriteLine(" CRITICAL PROCESS CRASHED");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine($"Process: {Name}");
Console.WriteLine($"ID: {Id}");
Console.WriteLine();
Console.WriteLine("Error:");
Console.WriteLine(e.ToString());
Console.WriteLine();
Console.WriteLine("Press any key to reboot...");
Console.ReadKey();
Sys.Power.Reboot();
}
else
{
CMLeonOS.Logger.Logger.Instance.Error("Process", $"Process \"{Name}\" ({Id}) crashed while stopping: {e.ToString()}");
}
}
}
}
}

92
System/ProcessManager.cs Normal file
View File

@@ -0,0 +1,92 @@
using System.Collections.Generic;
namespace CMLeonOS
{
public static class ProcessManager
{
public static List<Process> Processes = new List<Process>();
private static ulong nextProcessId = 0;
public static Process AddProcess(Process parent, Process process)
{
process.Parent = parent;
AddProcess(process);
return process;
}
public static Process AddProcess(Process process)
{
if (Processes.Contains(process))
return process;
process.Id = nextProcessId;
nextProcessId++;
Processes.Add(process);
return process;
}
public static void Sweep()
{
for (int i = Processes.Count - 1; i >= 0; i--)
{
if (!Processes[i].IsRunning)
{
Processes.Remove(Processes[i]);
}
}
}
public static void Yield()
{
for (int i = Processes.Count - 1; i >= 0; i--)
{
Process process = Processes[i];
if (process.IsRunning)
{
process.TryRun();
}
}
}
public static Process GetProcessById(ulong processId)
{
foreach (Process process in Processes)
{
if (process.Id == processId) return process;
}
return null;
}
public static Process GetProcessByName(string name)
{
foreach (Process process in Processes)
{
if (process.Name == name) return process;
}
return null;
}
public static T GetProcess<T>()
{
foreach (Process process in Processes)
{
if (process is T processT) return processT;
}
return default;
}
public static void StopAll()
{
for (int i = Processes.Count - 1; i >= 0; i--)
{
Process process = Processes[i];
process.TryStop();
}
}
}
}

63
System/ShellProcess.cs Normal file
View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using CMLeonOS.Commands;
using CMLeonOS.shell;
namespace CMLeonOS
{
public class ShellProcess : Process
{
private Shell shell;
public ShellProcess(UserSystem userSystem) : base("Shell", ProcessType.Application)
{
shell = new Shell(userSystem);
}
public override void Run()
{
CMLeonOS.Logger.Logger.Instance.Info("Shell", "Shell process started");
try
{
shell.Run();
}
catch (Exception e)
{
CMLeonOS.Logger.Logger.Instance.Error("Shell", $"Shell crashed: {e.ToString()}");
}
CMLeonOS.Logger.Logger.Instance.Info("Shell", "Shell process stopped");
}
}
public class CommandProcess : Process
{
private string command;
private string args;
private Shell shell;
public CommandProcess(string command, string args, Shell shell) : base($"Command_{command}", ProcessType.Application)
{
this.command = command;
this.args = args;
this.shell = shell;
}
public override void Run()
{
CMLeonOS.Logger.Logger.Instance.Info($"Command_{command}", $"Executing command: {command} {args}");
try
{
CommandList.ProcessCommand(shell, command, args);
}
catch (Exception e)
{
CMLeonOS.Logger.Logger.Instance.Error($"Command_{command}", $"Command failed: {e.ToString()}");
}
CMLeonOS.Logger.Logger.Instance.Info($"Command_{command}", $"Command completed: {command}");
}
}
}