0

I have a problem set question where I was given the solution, but don't quite understand the details.

The question is: How do we context switch?

The answer is:

  1. Step 1: Save context of process A
  2. Step 2: Restore context of process B
  3. Step 3: Resume executing process B

I'm wondering what saving and restoring the context of process A and B means?

I was going to say save memory mapping of A, restore memory mapping of B before looking at the answer, but is there something more than the memory mapping to saving and restoring?

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
mlstudent
  • 948
  • 2
  • 15
  • 30

3 Answers3

2

Briefly, process switch can be viewed in two parts, firstly switch current running PCB context registers(Saved and General purpose) followed by finding the highest priority process from the queue and resume it copying the registers from where it switched earlier and returns to its return address. In between if the queue has no other processes to run, the core's are move idle or running power saving cycles. Also care should be taken if it is a Multi-Processor, meaning the context switch of current process happened on one core and it is switched to run on other cores.

A very good information on Context Switching at SOlink

Community
  • 1
  • 1
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

AFAIK the data that is saved is the so called PCB (Process Control Block). What exactly is stored and how it is stored depends on the OS. But usually beside the "memory mapping" a PCB also contains the register values (especially the instruction pointer), the pointer to the parent process, I/O information, etc.

Al3x3
  • 463
  • 3
  • 9
0

Context switch is taken care by the host operating system only and it is transparent to the process or the code writer unless you're writing some device driver etc.

Suppose 100 processes are running, the OS automatically chooses the appropriate processes as per it's priority algorithm and schedule a process or more than one process in case of multi-cpu's available now a days.

When you write a program you write as if yours will run without any hindrance.

Just brush up operating system concepts(http://en.wikipedia.org/wiki/Operating_system) you'll understand.

AgA
  • 2,078
  • 7
  • 33
  • 62