What's the usual way to implement process time slices?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
j4cobgarby
Member
Member
Posts: 64
Joined: Fri Jan 26, 2018 11:43 am

What's the usual way to implement process time slices?

Post by j4cobgarby »

I couldn't think of a good way to phrase the title, but I'm going to implement multiprocessing in my operating system. Every certain time period, I need some kernel code to run to switch which process is currently running. I assume a good way of doing this would be setting up a timer such as the PIT, and then switch processes whenever that interrupts. Is this an acceptable way of doing this?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: What's the usual way to implement process time slices?

Post by iansjack »

It's more normal to count a number of ticks before switching, rather than switching on every interrupt. Apart from anything else this allows flexibility in how often the switches occur. And you'll want a clock that has finer granularity than task switches for other timing tasks. You'll also, probably, want to implement different priorities so that some processes get more CPU time than others.

Don't forget the case where there are no other processes ready to run. And think about the possibility that no process is ready to run.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: What's the usual way to implement process time slices?

Post by Korona »

"Priorities" are usually used in a real-time scenario, where the highest-priority thread always gets to run. Giving some processes longer time slices then others does not seem to be that useful to me: it would only matter if there are multiple non-interactive ("batch" / compute-heavy) tasks and running many of those is an uncommon workload (AFAICT). What's far more common is just limiting the CPU time that a certain set of processes can get per second (I am thinking of cgroups and containers).

Regarding the length of time slices: that greatly varies among OSes. Some OSes have fixed time slices (Linux before 2.6.23), say 1/100 s or 1/1000 s. Other OSes (modern Linux, managarm - shameless self promotion) use tickless systems that adjust the length of a time slice such that every thread gets roughly 1/T of a second - regardless of the number of threads running. In particular, for those systems, the length of a time slice depends on the number T of active threads and on the "unfairness" introduced by earlier scheduling decisions.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
eekee
Member
Member
Posts: 891
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: What's the usual way to implement process time slices?

Post by eekee »

I liked priorities. I didn't make much use of them, of course, but I did start whole-system compilation with a lower priority so it wouldn't interfere with me playing a CPU-intensive game. It didn't make a big difference, but it was nice. If I didn't want to play that game the low-priority processes could still get all the CPU time, which wouldn't be the case if there was a fixed limit on the time they could get.

I didn 't see any particular increase or decrease in subjective quality when Linux went tickless. For my usage, (web, game, compiling,) it was fine both ways. I don't think I tested it as carefully as I did faster ticks and preemptable kernel.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: What's the usual way to implement process time slices?

Post by nullplan »

Tickless is mostly a power management thing, so you'd only notice with light use on a laptop in battery mode that it lasts longer. See, with a ticking system, the system is waking up every tick even when there is nothing to do. With tickless, in many circumstances the system is only woken by the next real interrupt. The usefulness of this depends on how many ticks would go by without anything to do. The problem here is that some peripherals require periodic updates, anyway (USB HIDs come to mind), so you've only replaced one timer with another.
Carpe diem!
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: What's the usual way to implement process time slices?

Post by Korona »

Out of the USB HCDs, only the old UHCI needs periodic updates (and possibly OHCI, I don't know much about the latter).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Post Reply