Tags: 123a, 123int, 1b2f, bfloat, context, ends, ffor, int, linux, process, program, programming, suppose, switching, thisint, unix, void

Process Context switching

On Programmer » Unix & Linux

5,006 words with 3 Comments; publish: Tue, 29 Apr 2008 13:17:00 GMT; (20062.50, « »)

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.

All Comments

Leave a comment...

  • 3 Comments
    • 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.

      #1; Tue, 29 Apr 2008 13:18:00 GMT
    • 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.)

      #2; Tue, 29 Apr 2008 13:19:00 GMT
    • "alice" <alice_vas2001.unix-linux.todaysummary.com.yahoo.com> wrote in message

      news:1169463038.625431.75370.unix-linux.todaysummary.com.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.unix-linux.todaysummary.com.e3ft.com)

      http://www.e3ft.com (Consulting Home Page)

      http://www.dtashley.com (Personal Home Page)

      http://gpl.e3ft.com (GPL Publications and Projects)

      #3; Tue, 29 Apr 2008 13:20:00 GMT