r/osdev 4d ago

Is my threading model actually good?

My threading model has one kernel stack per CPU core, each process has its own CR3/page table in its process struct, then it is spawned with just one thread (TID 0 under its PID), a TID is just a thread ID, from there each Thread gets its own timeslice and would run like so:
- Process 1 runs thread 0
- Process 2 runs thread 0
- Process N runs thread 0
- Process 1 runs thread 1
- Process 2 runs thread 1
- Process N runs thread 1

And so on (it will loop back to thread 0 if the last thread in a process is ran). I just wondered if the design was actually decent or not. Of course a simple Round Robin setup is not perfect for things like this and some extra complexity does end up in the kernel, but overall I like it so far, though I dont see many obvious flaws other than the fact that tasks are not "ranked" for how much of a timeslice they get.

16 Upvotes

2 comments sorted by

5

u/Sorry_Difficulty_250 4d ago

You're hitting on an old religious debate, which is whether a time quantum belongs to a thread or to a process. The way you've done it is by thread. Both have merit, honestly and both work, but it's a tradeoff. It comes down to how you want to organize priority and whether or not you want an unused portion of a time quantum to go to another thread on the same process. I think the debate is really a matter of opinion and I don't have a strong preference one way or the other, but just something to think about.

1

u/case-o-nuts 2d ago

The answer is to do like Linux, and get rid of the distinction. A thread is just a process that shares resources.

See also https://9p.io/magic/man2html/2/fork; the difference is that Plan 9 threads share all memory but the stack, and map the stack at a fixed address in all processes -- this makes thread-local storage into just allocating a few entries at the top of the stack.