|
Home > Archive > Unix Programming > January 2007 > Process Context switching
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Process Context switching
|
|
|
| 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?
Thanks
PS: I apologize in case this is not the right group to post this
question.
| |
| wenz.zhang@gmail.com 2007-01-22, 7:21 am |
| Processes share a set of registers but have private address space.
Here a,b,f are in the stack within the process.They will not be changed
by other processes.
"alice =D0=B4=B5=C0=A3=BA
"
> Hi,
>
> Suppose a program is something like this:
>
> int main(void)
> {
> int a ;
> int b;
> float f;
>
> for(;;)
> {
> a +=3D1;
> b+=3D2;
> f +=3D2.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?
>
> Thanks
>
> PS: I apologize in case this is not the right group to post this
> question.
| |
| Nils O. Selåsdal 2007-01-22, 1:16 pm |
| alice wrote:
> 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?
>
> Thanks
>
> PS: I apologize in case this is not the right group to post this
> question.
Each process will have it's own view of the memory private
to that process(address space).
That state is saved and restored, so when the OS switches
back to your process, it's state is restored.
(There are cases where you can create processes that share
parts with other processes btw.)
| |
| David T. Ashley 2007-01-23, 1: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)
|
|
|
|
|