Context Switching in os

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
A hardware timer is configured to create an interrupt every 1 ms. Time slice is 1 ms.

CPU is executing three Task's in round robin fashion.

  1. Task 1 takes 2 ms to complete
  2. Task 2 takes 4 ms to complete
  3. Task 3 takes 6 ms to complete

What is context switching in the whole process ?

After giving up control of one task the CPU gives control to another task, is this called context or task switching?
 
Last edited:

nsaspook

Joined Aug 27, 2009
16,275
The precise meaning of the phrase “context switch” varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an interrupt, such as when a task needs to access disk storage, freeing up CPU time for other tasks. Some operating systems also require a context switch to move between user mode and kernel mode tasks. The process of context switching can have a negative impact on system performance
https://en.wikipedia.org/wiki/Context_switch
 

BobTPH

Joined Jun 5, 2013
11,488
I think context switch is a more general term than task switch. For example, a single task in a protected system might do a context switch to go from user mode to kernel mode or back.

Bob
 

djsfantasi

Joined Apr 11, 2010
9,237
What is the interrupt doing? Have you considered that?

What is each task doing? More than you think…

First, the interrupt MAY be incrementing a counter… Let’s assume this is all it will do.

Then, let’s assume that Task n does Work n. And for this level of discussion, Work n consists of a single action.

Then, you need to know if you’re doing cooperative or preemptive multitasking.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
" In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed."

What is the meaning of above paragraph?

In multitasking, task can be in any state like running, ready to run, blocked and terminated.

one state can be changed to another state such as If a task is running then it can also be blocked.
 

nsaspook

Joined Aug 27, 2009
16,275
" In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed."

What is the meaning of above paragraph?

In multitasking, task can be in any state like running, ready to run, blocked and terminated.

one state can be changed to another state such as If a task is running then it can also be blocked.
Me telling you is useless. You must have your own level of comprehension. Read and reread the material until it starts to make sense.
 

djsfantasi

Joined Apr 11, 2010
9,237
@Sparsh45

in your many posts, you have demonstrated total confusion about multitasking. Many members have tried to explain it to you, but you always confuse the concepts, mixing them up so badly that there appears hopeless that you’ll ever understand.

You need to do the work yourself. I cannot help you. And others who have tried cannot help you, until you better understand the underlying concepts on your own.

Good luck. I must ignore your posts in the future until you demonstrate that you understand the concepts.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
What is the interrupt doing? Have you considered that?
Interrupt generates 1 ms tick every time.
Then, you need to know if you’re doing cooperative or preemptive multitasking.
My example shows the round robin scheduling Algorithm. Each task share 1ms time. Pre-emptive scheduling can be designed with round robin scheduling algorithm. I can say here that I am doing Pre-emptive scheduling.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Me telling you is useless. You must have your own level of comprehension. Read and reread the material until it starts to make sense.
In the wikipedia page that you gave, I did not really understand that paragraph.

"In a multitasking context, it refers to the process of storing the system state for one task "

It say that task can be in any state like running, ready to run, blocked and terminated. We have to save each state of the task.

"so that task can be paused and another task resumed."

I do not understand the meaning of this specific line in the paragraph
 

Ya’akov

Joined Jan 27, 2019
10,226
The context is the state of the registers of the processor. In order to switch tasks and still be able to return to the task being set aside, the context needs to be saved so it can be restored.

Context switching is a necessary part of multitasking because each task will expect to find the CPU in a certain state when it is returned to for execution. It is not the status of the task but the state of the CPU that is being stored and restored.

You are not being systematic in your investigation into multitasking and because you don't understand, for example, machine level operations, things will not make sense to you. The author of an operating system needs to know a lot more than the user of one. While C is very useful for the higher level functions without insight into the "bare metal programming" you will always be confused.

My suggestion is to step back and learn to write a few simple programs in assembler so you have some idea of what is actually going on and not some abstraction that involves names that might as well be magic incantations.
 
Top