OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:09 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: What's the usual way to implement process time slices?
PostPosted: Fri Dec 06, 2019 6:20 am 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
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?


Top
 Profile  
 
 Post subject: Re: What's the usual way to implement process time slices?
PostPosted: Fri Dec 06, 2019 7:48 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: What's the usual way to implement process time slices?
PostPosted: Fri Dec 06, 2019 10:00 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
"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].


Top
 Profile  
 
 Post subject: Re: What's the usual way to implement process time slices?
PostPosted: Tue Dec 24, 2019 2:41 pm 
Offline
Member
Member
User avatar

Joined: Mon May 22, 2017 5:56 am
Posts: 812
Location: Hyperspace
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


Top
 Profile  
 
 Post subject: Re: What's the usual way to implement process time slices?
PostPosted: Tue Dec 24, 2019 3:24 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
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!


Top
 Profile  
 
 Post subject: Re: What's the usual way to implement process time slices?
PostPosted: Tue Dec 24, 2019 5:06 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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].


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group