How does task sleeping is working in practise

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ZigZogZang
Posts: 14
Joined: Tue Nov 26, 2013 7:41 am

How does task sleeping is working in practise

Post by ZigZogZang »

Hey! My question is about task sleeping, how is it working in "asm code" ?

I haven't find any information about how may it be implemented. By process sleeping everyone know what does it means but how does it work "inside" ?

My idea is that, and hope you will tell me am I correct or am I wrong about it:

Let's have round robin scheduler (1-way queue). Let's have 4 tasks with attribute byte that tells if process is sleeping or isn't it (1 attribute byte per task):
1. task1 attribute=01h (1st bit set = task not sleeping) CS=50h EIP=0
2. task2 attribute=01h (1st bit set = task not sleeping) CS=58h EIP=0
3. task3 attribute=01h (1st bit set = task not sleeping) CS=62h EIP=0
4. task4 attribute=01h (1st bit set = task not sleeping) CS=72h EIP=0

After 1 cycle that allows task to be executed for some time (like 10 ms each) we gain such EIP status (imagined sitation / values of eip register):
task1; EIP=0x45
task2; EIP=0xf3
task3; EIP=0x9d
task4; EIP=0x3b

Now i want to sleep task3. And i am clearing 1st bit of attribute so
task3 attribute=00h
now task is sleeping, and EIP register won't change (increase or decrease)

And after next cycle of schulder:
task1; EIP=0xb5 ;changed
task2; EIP=0xf9 ; changed
task3; EIP=0x9d; not changed. scheduler is not switching into this task (not executing it) because checked attribute == 0x00 and executing next task (task4) right now
task4; EIP=0xc6 ; changed

Is my conception of process sleeping correct or totally wrong ? Or correct but it may be implemented in better way ?

Greeting,
Danny
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How does task sleeping is working in practise

Post by gerryg400 »

ZigZogZang wrote:Hey! My question is about task sleeping, how is it working in "asm code" ?

I haven't find any information about how may it be implemented. By process sleeping everyone know what does it means but how does it work "inside" ?

My idea is that, and hope you will tell me am I correct or am I wrong about it:

Let's have round robin scheduler (1-way queue). Let's have 4 tasks with attribute byte that tells if process is sleeping or isn't it (1 attribute byte per task):
1. task1 attribute=01h (1st bit set = task not sleeping) CS=50h EIP=0
2. task2 attribute=01h (1st bit set = task not sleeping) CS=58h EIP=0
3. task3 attribute=01h (1st bit set = task not sleeping) CS=62h EIP=0
4. task4 attribute=01h (1st bit set = task not sleeping) CS=72h EIP=0

After 1 cycle that allows task to be executed for some time (like 10 ms each) we gain such EIP status (imagined sitation / values of eip register):
task1; EIP=0x45
task2; EIP=0xf3
task3; EIP=0x9d
task4; EIP=0x3b

Now i want to sleep task3. And i am clearing 1st bit of attribute so
task3 attribute=00h
now task is sleeping, and EIP register won't change (increase or decrease)

And after next cycle of schulder:
task1; EIP=0xb5 ;changed
task2; EIP=0xf9 ; changed
task3; EIP=0x9d; not changed. scheduler is not switching into this task (not executing it) because checked attribute == 0x00 and executing next task (task4) right now
task4; EIP=0xc6 ; changed

Is my conception of process sleeping correct or totally wrong ? Or correct but it may be implemented in better way ?

Greeting,
Danny
It's kind of correct but it doesn't scale well. If there a millions of sleeping tasks it may take a long time to find one that is runnable. It's more normal to keep lists of runnable tasks and if a task sleeps simply remove it from its 'runnable' list so that the scheduler never considers it.
If a trainstation is where trains stop, what is a workstation ?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How does task sleeping is working in practise

Post by Brendan »

Hi,
ZigZogZang wrote:Hey! My question is about task sleeping, how is it working in "asm code" ?
I normally have a list of tasks that are "ready to run". When the scheduler gives one of the tasks CPU time it removes it from the "ready to run" list, and if the task is preempted it puts it back on the "ready to run" list. If a task calls "sleep()" then the scheduler doesn't put it back on the "ready to run" list - instead the scheduler puts it on some sort of "sleeping tasks" list; where a timer is responsible for (eventually, when the task has slept long enough) removing the task from the "sleeping tasks" list and putting the task back on the "ready to run" list.

Also note that for round robin the "ready to run list" can be a FIFO queue implemented as a linked list (with "O(1) everything" if there's only one CPU and you don't need to worry about locks).

Of course I've simplified a lot (I'm far more likely to use many different "ready to run" lists/queues) but hopefully you get the general idea...



Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply