01-23-07 06:26 AM
"alice" <alice_vas2001@yahoo.com> wrote in message
news:1169463038.625431.75370@m58g2000cwm.googlegroups.com...
> Hi,
>
> Suppose a program is something like this:
>
> int main(void)
> {
> int a ;
> int b;
> float f;
>
> for(;;)
> {
> a +=1;
> b+=2;
> f +=2.2;
> }
>
> ...
> } .. main ends here.
>
> Now what I've read is whenever a context switch takes places, the OS
> stores all the status of all the registers and the other pointers
> needed to restore the state of the process but it does not saves the
> values of the variables.
> So how after the context switch, the values of the variables(here
> a,b,f) will remain equal to their values prior to the context swiching?
The statement you read about the registers and so on makes the implicit
assumption that each process has its own memory space (which it does). So,
when a context switch occurs, it is (in a simplified way of thinking) only
necessary to save the CPU registers and restore them to another saved set.
Restoring the CPU registers (such as program counter, stack pointer, various
segment registers, condition code register, general purpose registers, etc.)
causes the CPU to "point" back to the right process' memory.
All variables are, at any point in time, either in the CPU or in memory, so
they are automatically preserved.
However, in practice it isn't quite that simple. There is memory management
hardware to deal with, and also probably some integer counters updated for
scheduling and performance statistics, etc.
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
[ Post a follow-up to this message ]
|