I’m working on a custom shell and a sort of pseudo-OS (process management etc., not virtualizing resources) in Unity. At the moment, it’s set up so the different processes inherit from an abstract Process class, and override Start, Update, and Exit, which are called right after creation, once per frame, and right before the process ends respectively. This is of course similar to how Unity script/components work (tho Unity’s OnDestroy is probably most similar to my process Exit). Now, everything is working fine at the moment (woo!), but there’s an edge case I’d like to be able to handle.
I have system in place to stop/cancel processes (with ctrl+C as expected). However, if someone were to create and then run a process like this:
[Process("exampleprocess", IsCommand = true, IsCritical = false, IsRoot = false)]
class ExampleProcess : Process
{
public override void Update()
{
while (true)
{
//infinite loop BABYYYYYY!!!!
}
}
public override void Exit() { }
}
//elsewhere in the code
ProcessManager.Run<ExampleProcess>();
Then the entire Unity application would crash, as it’s all in 1 thread. An OS(-ish) that entirely fails when one process encounters an error is kind of a shitty OS. In this sort of system, you should be able to ctrl+C the process and force quit it, even if the process is as poorly designed as the example above.
I currently have 2 thoughts on how to fix this issue. The first is running each process in its own thread, and then somehow cancelling/aborting the thread on ctrl+C as a last resort (and then still calling Exit so it can handle open resources). This feels like it would get cumbersome as more and more threads start running simultaneously and accessing shared resources. The second approach is leaving almost everything single-threaded, and just running the input manager in a separate thread, so it can continuously monitor for ctrl+C. Of course, then there’s the issue of how do you kill the infinite loop with this approach. Probably some reflection and potentially IL-related stuff. I think of the two, I prefer the 2nd if I can get it working.
If you have an idea for another approach, please feel free to share and I’ll happily consider it!
It’s worth noting that this was built with Unity 2017.4.13 (.NET Framework 4.7.2). Yes, I have a reason for using a 7 year old version. No, it’s not really relevant to the problem I’m proposing, though it may change what tools & features I have available at my disposal.