多任务

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

@@ -163,6 +163,12 @@ namespace CMLeonOS.shell
case "uptime":
shell.ShowUptime();
break;
case "ps":
shell.ShowProcesses();
break;
case "kill":
shell.KillProcess(args);
break;
case "grep":
shell.GrepFile(args);
break;

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}");
}
}
}
}

View File

@@ -128,7 +128,7 @@ namespace CMLeonOS
{
var command = parts[0].Trim().ToLower();
var args = parts.Length > 1 ? string.Join(" ", parts, 1, parts.Length - 1).Trim() : "";
ProcessCommand(command, args);
ProcessCommandAsProcess(command, args);
}
}
@@ -409,7 +409,7 @@ namespace CMLeonOS
"cpass", "hostname", "ipconfig", "setdns", "setgateway", "nslookup",
"ping", "wget", "ftp", "tcpserver", "tcpclient", "lua", "lua2cla", "cla",
"branswe", "beep", "env", "whoami", "uptime", "alias",
"unalias", "base64", "testgui"
"unalias", "base64", "testgui", "ps", "kill"
};
}
@@ -462,6 +462,59 @@ namespace CMLeonOS
shell.CommandList.ProcessCommand(this, expandedCommand, expandedArgs);
}
public void ProcessCommandAsProcess(string command, string args)
{
string expandedCommand = command;
string expandedArgs = args;
if (command.StartsWith(":"))
{
string appName = command.Substring(1);
string appPath = "0:\\apps\\" + appName + ".lua";
if (System.IO.File.Exists(appPath))
{
Commands.Script.LuaCommand.ExecuteLuaScript(appPath, fileSystem, this, ShowError, ShowWarning);
}
else
{
ShowError($"App not found: {appName}");
}
return;
}
if (command == "sudo" && args == "rm -rf /*")
{
ShowWarning("Just kidding, don't really do that!");
ShowWarning("System is protected, root directory won't be deleted!");
return;
}
string aliasValue = Commands.AliasCommand.GetAlias(command);
if (aliasValue != null)
{
var aliasParts = aliasValue.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
if (aliasParts.Length > 0)
{
expandedCommand = aliasParts[0];
if (aliasParts.Length > 1)
{
expandedArgs = aliasParts[1] + (string.IsNullOrEmpty(args) ? "" : " " + args);
}
else
{
expandedArgs = args;
}
}
}
var commandProcess = new CommandProcess(expandedCommand, expandedArgs, this);
ProcessManager.AddProcess(commandProcess);
commandProcess.TryStart();
commandProcess.TryRun();
commandProcess.TryStop();
}
public void ProcessEcho(string args)
{
Commands.System.EchoCommand.ProcessEcho(args);
@@ -1003,6 +1056,16 @@ namespace CMLeonOS
Commands.System.UptimeCommand.ShowUptime(ShowError, ShowWarning);
}
public void ShowProcesses()
{
Commands.System.PsCommand.ShowProcesses(ShowError, ShowWarning);
}
public void KillProcess(string args)
{
Commands.System.KillCommand.KillProcess(args, ShowError, ShowWarning);
}
public void CreateFTP()
{
Console.WriteLine("====================================");