2

Does context switch between process take equal time for all the Process ( Constant Time ) or the context switch time is dependent on various local factors which varies from process to process ( like process size,stack size etc..) ?

EDIT : Assume the OS and Hardware are fixed, means will the time be same in a given OS and hwd. environment ?

Amol Sharma
  • 1,521
  • 7
  • 20
  • 40
  • This is two different questions - you cannot combine them with 'or' :) – Martin James Apr 29 '12 at 12:23
  • Also 'quantum' is a word that should not enter into any discussion of OS scheduling. It strongly suggests that threads can only run for some set of discrete intervals. Most OS have not worked like that since the late 60's. – Martin James Apr 29 '12 at 12:27
  • If you want guarantees, you will have to use a real-time OS that provides them :( – Martin James Apr 29 '12 at 12:35
  • 1
    Also remember about caches. With all other things being equal, if one process' data structures are in the cache, while the other's aren't, you'll spend extra cycles bringing them into the cache from the main memory. – Alexey Frunze Apr 29 '12 at 12:45
  • @Alex - that is true if the data is actually in main memory. If it's been paged out.. – Martin James Apr 29 '12 at 12:49

1 Answers1

1

It varies with hardware as well as OS/process :( To run a thread from a different process, memory-management context, floating-point context etc. must be swapped. This is easier/quicker on some hardware than others.

Drivers vary widely in the time they take to handle their hardware and signal the OS that a thread should be made running - so that's another complication.

In some cases, such a swap may need the preemption of a thread running on another core than the one that received the hardware/software interrupt that initiated the swap. This takes a lot longer than swapping context on the same processor.

It's difficult to come up with any sort of average figure on this. Where would you time it from - the driver interrupt that initiated the inter-process thread swap or from the entry to the scheduler from the driver?

So, overall, we can probably agree that it takes some time and it can vary.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • On a Given hardware and a given OS...will it be constant ??.........i mean if hwd. and OS is fixed then time in switching should be constant ? – Amol Sharma Apr 29 '12 at 12:20
  • No, not on a desktop OS. If the thread that is going to be preempted is running on a different core than the core that is processing the interrupt that requested the swap via the scheduler, some expensive inter-thread comms must be used to stop the core running the other thread so that core can be used to run the newly-ready thread. – Martin James Apr 29 '12 at 12:34
  • and if there is single core then time will be same ? as there will not be any expensive comm. – Amol Sharma Apr 29 '12 at 12:36
  • It will still vary a lot, in general. Just one thing - code and/or data required by the thread in the other process may have been paged out. – Martin James Apr 29 '12 at 12:46