多任务

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

View File

@@ -260,6 +260,18 @@ namespace CMLeonOS.Commands
Description = "Show system uptime"
},
new CommandInfo
{
Command = "ps",
Parameters = "",
Description = "List all running processes"
},
new CommandInfo
{
Command = "kill",
Parameters = "<process_id>",
Description = "Kill a process by ID"
},
new CommandInfo
{
Command = "matrix",
Parameters = "",
@@ -403,7 +415,7 @@ namespace CMLeonOS.Commands
{
Command = "help",
Parameters = "<page>",
Description = "Show help page (1-4)",
Description = "Show help page (1-5)",
SubCommands = new[] { new SubCommandInfo { Command = "help all", Description = "Show all help pages" } }
}
};

View File

@@ -0,0 +1,57 @@
using System;
namespace CMLeonOS.Commands.System
{
public static class KillCommand
{
public static void KillProcess(string args, Action<string> showError, Action<string> showWarning)
{
try
{
if (string.IsNullOrWhiteSpace(args))
{
showError("Usage: kill <process_id>");
showError("Example: kill 123");
return;
}
if (!ulong.TryParse(args, out ulong processId))
{
showError($"Invalid process ID: {args}");
showError("Process ID must be a number.");
return;
}
Process process = ProcessManager.GetProcessById(processId);
if (process == null)
{
showError($"Process not found: {processId}");
showError("Use 'ps' command to list all processes.");
return;
}
if (process.Name == "Shell")
{
showError("Cannot kill Shell process.");
showError("Use 'exit' command instead.");
return;
}
if (!process.IsRunning)
{
showWarning($"Process {process.Name} ({processId}) is already stopped.");
return;
}
CMLeonOS.Logger.Logger.Instance.Info("Kill", $"Killing process: {process.Name} ({processId})");
process.TryStop();
Console.WriteLine($"Process {process.Name} ({processId}) stopped successfully.");
}
catch (Exception ex)
{
showError($"Error killing process: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
namespace CMLeonOS.Commands.System
{
public static class PsCommand
{
public static void ShowProcesses(Action<string> showError, Action<string> showWarning)
{
try
{
Console.WriteLine("====================================");
Console.WriteLine(" Process List");
Console.WriteLine("====================================");
Console.WriteLine();
if (ProcessManager.Processes.Count == 0)
{
Console.WriteLine("No processes running.");
Console.WriteLine();
return;
}
Console.Write("ID ");
Console.Write("Name ");
Console.Write("Type ");
Console.Write("Parent ");
Console.WriteLine("Status ");
Console.WriteLine("----------------------------------------------------------------------");
foreach (var process in ProcessManager.Processes)
{
string parentId = process.Parent != null ? process.Parent.Id.ToString() : "N/A";
string status = process.IsRunning ? "Running" : "Stopped";
string type = process.Type.ToString();
Console.Write(process.Id.ToString().PadRight(8));
Console.Write(" ");
Console.Write(process.Name.PadRight(20));
Console.Write(" ");
Console.Write(type.PadRight(12));
Console.Write(" ");
Console.Write(parentId.PadRight(8));
Console.Write(" ");
Console.WriteLine(status);
}
Console.WriteLine();
Console.WriteLine("Total processes: " + ProcessManager.Processes.Count);
Console.WriteLine();
}
catch (Exception ex)
{
showError($"Error listing processes: {ex.Message}");
}
}
}
}