Process Context switching
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Process Context switching




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Process Context switching  
alice


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-07 12:21 PM

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.






[ Post a follow-up to this message ]



    Re: Process Context switching  
wenz.zhang@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-07 12:21 PM

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.






[ Post a follow-up to this message ]



    Re: Process Context switching  
Nils O. Selåsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-07 06: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.)





[ Post a follow-up to this message ]



    Re: Process Context switching  
David T. Ashley


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:13 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register