6 ms·
An OS with async/await sounds awfully similar to Windows 3.1 with its cooperative multitasking model. What's old is new again ...
by amelius 7y ago
An OS with async/await sounds awfully similar to Windows 3.1 with its cooperative multitasking model.
What's old is new again ...
- steveklabnik 7y agoWell, there's a big difference: > Using async/wait, we now have basic support for cooperative multitasking in our kernel. While cooperative multitasking is very efficient, it leads to latency problems when individual tasks keep running for too long and thus prevent other tasks to run. For this reason, it makes sense to also add support for preemptive multitasking to our kernel. > In the next post, we will introduce threads as the most common form of preemptive multitasking. In addition to resolving the problem of long running tasks, threads will also prepare us for utilizing multiple CPU cores and running untrusted user programs in the future. It seems the plan here is to use this for internal work, and still give a preemptive multitasking API to userspace.
- jklehm 7y agoIsn't that driving against the lesson learned from the past though? 3rd party drivers can not be trusted to behave and hang the kernel when doing so with cooperative multitasking.
- steveklabnik 7y agoI am not sure what Phil's overall kernel design is; you're assuming a monolithic kernel, but not every kernel has drivers in kernel space.
- jklehm 7y agoFair point, looking forward to following along :)
- eximius 7y agoToy OS, so he has less to worry about. But also maybe just be more careful around kernel modules, if that eventually exists?
- mtnygard 7y agoPhil's series is meant for learning rather than production use. So sometimes he takes a "shortcut" in one post then removes the restrictions or workarounds later.
- Koshkin 7y agoGP is not wrong, then: with this implementation, async/await still amounts to the good old cooperative multitasking and does not (yet?) take advantage of threads. (In C#, for example, async/await is indeed different from this, being based on TAP - Task-based Asynchronous Pattern, where tasks are executed "asynchronously on a thread pool thread rather than synchronously on the main application thread.")
- steveklabnik 7y agoAsync/await lets you build tasks, but is completely orthogonal to threads. You could set up tasks with a 1:1 or N:M or even single threaded model. And yes, the GP is not wrong that if this were the only mechanism provided, it would be similar. It does not seem like the plan is to expose this externally whatsoever, though.
- AnIdiotOnTheNet 7y agoWell, I'm not well versed in the details, but I do wonder if cooperative multitasking isn't a better idea nowadays in the age of ubiquitous multicore.
- bluejekyll 7y agoAs Steve’s sibling comment points out, this is only for the kernel. The article mentions preemptive threading for the user space as still being desirable.
- AnIdiotOnTheNet 7y agoI'm not convinced that's actually true though, is what I mean. The big drawback of cooperative is just that a single process can monopolize resources, which in the singlecore days mean the system became unresponsive and you had no way to recover from it. That isn't really true now with multicore. As long as the OS has a core to work on it can terminate or otherwise deal with misbehaving processes. Again, this is based on my admittedly limited understanding, but I haven't yet seen good reasoning why pre-emptive is still preferred.
- steveklabnik 7y agoMy understanding is that you've now moved the monopolized case from 1 to N, where N is the number of cores. N is usually pretty small. The laptop I'm writing this on has four cores, and the Chrome instance has 17 threads running...
- Bigpet 7y ago> Error: you cannot open another Chrome tab because all your cores are already used up by Slack and VSCode You would probably still want to preempt, because you're not going to rewrite all the widely used software to actually yield. Because that's the thing about cooperative multithreading, the participating parties need to cooperate. And if you look at the amount of threads that some software open it's just crazy. Looking on my machine the top5 are: 1. 260 threads System (ok, that's basically the OS that could be changed). 2. 145 threads Dropbox.exe (that's enough to lock all cores on any single consumer CPU). 3. 87 threads SearchIndexer.exe (also OS, could be rectified). 4. 74 threads EpicGamesLauncher (looks like an i9 is no longer enough) 5. 70 threads MemoryCompression (also OS) I know not all of those threads are active at all time an I guess all the blocking threads could be said to be cooperating but even Dropbox alone regularly has 3-5 threads running. There's still so many 2 and 4 core consumer machines out there that cooperative multi-tasking with the current software ecosystem would be a disaster. Not only would applications keep each other from making regular steady progress but even single applications would regularly hang themselves from all the threads they themselves created if there was no pre-emptive yielding beyond using any OS-call as a yield-point
- hope-striker 7y agoDidn't most old operating systems use cooperative multitasking? I remember, at least, that classic Mac OS (i.e. pre-OSX) didn't use preemptive multitasking, either. Anyway, this SO answer[0] explains why early Linux, much like the hobby kernel in this article, used cooperative scheduling inside the kernel, and only preempted user-space stuff. [0]: https://stackoverflow.com/a/16766562 https://stackoverflow.com/a/16766562
- bestouff 7y agoMacOS may have been a little late. Windows NT, OS/2, Linux did preemptive multitasking since beginning of 90s, even AmigaOS had it in the 80s.
- saagarjha 7y agoMac OS badly needed preemptive multitasking (and memory protection!), and got it when the NeXT bits were transformed into Mac OS X.
- renox 7y agoThanks for the link! I don't understand why this would be different for a Rust based OS..
- fortran77 7y agoOr MacOS up to and including OS9
- dfox 7y agoThere is one significant difference: Windows did context-switches in the blocking calls and did not rely on the program code having "the right structure" needed for straight co-routines to work. The difference between preemptive and cooperative multitasking is not whether you do full context switches, but whether there is a way to do context switch at a point where the process does not expect it (ie. by handling some kind of timer interrupt as scheduling opportunity).
- amelius 7y agoDoes Rust allow a computational for-loop to be interrupted somehow? Computation can also be viewed as a blocking operation.
- steveklabnik 7y agoThe only yield points are .await points. If there's an .await in a loop, then sure, but otherwise no.
- amelius 7y agoWhat is the cost of an .await point? For this to work, perhaps Rust should cooperate too, inserting .await points at strategic places in the code, to keep cost low but still guaranteeing a certain responsiveness of the overall system.
- jfkebwjsbx 7y agoThat would explode the state machine size and make its performance characterization very hard. And you would need to avoid having any kind of call to external code (or yield before and after every such call, and pray for the best).
- deleted 7y ago[deleted]
- mwcampbell 7y ago
- imtringued 7y agoJust because a kernel is using something internally doesn't mean it is exposing it to user space applications.