Issues with time slicing

linuxmultithreadingoperating systems

I was trying to see the effect of time slicing, and how it can consume significant amount of time. Actually, I was trying to divide a certain task into a number of threads and see the effect.

I have a two core processor. So two threads can run in parallel. I was trying to see if I have a task w that is done by 2 threads, and if I have the same task executed by t threads with each thread doing w/t of the task. How much does time slicing play a role in it.

As time slicing is time consuming process, I was expecting that when I do the same task using a two thread process or by a t thread process, the amount of time taken by the t thread process will be more.

Any suggestions?

Best Answer

time slicing is a small cost - each time the thread quantum expires and the scheduler switches to another thread, all the registers have to be saved and the new thread's old state restored. If you do this continually, you will see this context switching take a lot of the overall time, but if you have 2 threads running on 2 CPUs, you're not going to see any switching at all. How long exactly does a context switch take? depends on the CPU - risc processors have more registers so take longer. How long is a thread quanta? depends on the OS.

The other cost is in CPU cache invalidation - if 2 threads are running on the same CPU, and each one is working of different data, then the required data will have to re-fetched which will slow things down tremendously on modern CPUs (you won't see this effect so much on older CPUs)