views:

128

answers:

1

In Russinovich book it says that thread (NOTE: this is about OS thread) will need dispatching (scheduling) if it a) became ready b) ended its timeslice, yields or blocks.

I have a managed thread in my C# real-time app for which is very important to achieve as fewer context switches as possible.

This thread has Highest priority and the process has RealTime priority which makes my thread OS priority 26 out of 31.

What is going to happen to my thread when it ends its timeslice and there are no waiting threads with priority >= 26 ?

Will be there a context switch to reschedule my thread to run again or context switch will be avoided and the thread will be running uninterrupted?

If there will be context switch - can anyone tell how many CPU cycles it takes on average?

I'd appreciate simple and definitive answers!

Thank you !

+3  A: 

How could you make a real-time application on non realtime OS? Here's the information about realtime OS

If your thread has max priority you can end up with unresponsive system (http://blogs.msdn.com/b/oldnewthing/archive/2010/06/10/10022675.aspx)

What is going to happen to my thread when it ends its timeslice and there are no waiting threads with priority >= 26 ?

If there are no other threads with the same or higher priority and your thread does not block (sleep, wait etc) - then system will reschedule it with a new time slice.

From MSDN article: "The system treats all threads with the same priority as equal. The system assigns time slices in a round-robin fashion to all threads with the highest priority. If none of these threads are ready to run, the system assigns time slices in a round-robin fashion to all threads with the next highest priority. If a higher-priority thread becomes available to run, the system ceases to execute the lower-priority thread (without allowing it to finish using its time slice), and assigns a full time slice to the higher-priority thread"

Will be there a context switch to reschedule my thread to run again or context switch will be avoided and the thread will be running uninterrupted?

Context switch will occur when thread is transitioned to kernel mode (blocks, sleeps) You can prevent thread from context switching using Thread.SpinWait.

If there will be context switch - can anyone tell how many CPU cycles it takes on average?

Assuming the context switch is initiated by an interrupt, the overhead of switching from user-level to kernel-level on a (2.8 GHz) P4 is 1348 cycles, on a (200 MHz) P2 227 cycles. Why the big cycle difference? It seems like the P4 flushes its micro-op cache as part of handling an interrupt (go to arstechnica.com for some details on the micro-op cache). Counting actual time, the P4 takes 481 ns and the P2 1335 ns.

The return from kernel-level to user-level will cost you 923 cycles (330 ns) on a P4, 180 cycles (900 ns) on a P2.

The overhead of storing / restoring registers (not counting any TLB overhead / excluding cost of FPU register store / restore) is 188 cycles on the P4 (67 ns), 79 cycles on the P2 (395 ns). (taken from here)

Vadmyst
although I appreciate your input on context switch overhead you completely missed the first question. please read it again. it describes 4 situations when thread must be scheduled/dispatched. you describe only one of it - blocking. the question is specifically about - what happens to a thread which has finished its timeslice/quantum (this is one of 4 situations and completely independent from blocking). note that in .NET one of the states of a thread is "wait, sleep or join" which makes sleep = block. thanks
Bobb
Thanks for the input. I've added an answer for the first question
Vadmyst
@bobb I would think about that: "How could you make a real-time application on non realtime OS?" Since .NET threads are not mapped 1:1 to OS threads, you cant be sure when there will be context switches and when there wont(afaik).
atamanroman
Ok moving on. Vadmyst we are almost there... The essence of the first question actually (you say - "system will reschedule it with a new time slice.") - what exactly involves rescheduling? I thought there will be context switch. But its just a wild guess.. Could you please tell what takes to reschedule? Thank you!
Bobb
I decided to ask this question separately: http://stackoverflow.com/questions/3100448/what-involves-to-schedule-a-thread-in-windows
Bobb
@Bobb. I doubt that there will be context switch when system reschedules the same thread (I may be wrong though) Rescheduling in this context means assigning the thread to run on the specific CPU.
Vadmyst