0

I have a question about which datas will be saved in context switching. I have searched but all links just talked about registers.

My question is does os also save the memory values of a process ?

Assume a process has defined an array starting from adress 0x80000 of Ram. When context switch occurs, what will happen to this array? Maybe the new process override 0x80000 adress in memory and when the old process continue it's working it losts the array!

Anyone can explain?

5
  • 1
    this is very broad as it is system dependent. but for example if you are on a desktop windows/linux/macos the memory map for a process is virtual, when you change contexts in some way you change the map, how depends on the mmu and the rest of the chip/system design. it could be as simple as the process gets a different processid and the process id is used by the mmu to define what physical memory 0x80000 points at and certainly in that case during a context swap you save/retain the process id along with a few registers and restore them Commented Dec 17, 2018 at 21:11
  • 1
    but certainly you dont make a second copy of memory that that process is using, we would perceive that when even typing text into this web page, it would be painfully slow... Commented Dec 17, 2018 at 21:11
  • I understand your answer but there is another non-solved question for me. What happens if the new process write on the physical adress which the old process array is saved on it ? The memory is shared and maybe on a bad luck the new process overwrites the array of old process in the same memory location Commented Dec 17, 2018 at 21:15
  • You mean each process has a seperate part of physical memory which the other processes dont have permission to get inside that? Commented Dec 17, 2018 at 21:16
  • depends on the operating system your question is incredibly vague as there are so many different implementations for so many different systems and so many answers. But when you are talking things like windows or linux, etc on an x86, then yes everyone is protected from everyone else, otherwise there would be no protection. Commented Dec 18, 2018 at 8:03

1 Answer 1

2

In general, an operating system does not save memory in a context switch. It just changes register values. The old process's memory just stays there until the system needs it. If that happens, the memory will be paged out.

In the olde days of swapping, yes, the memory was frequently saved when a new process came in.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer. You mean the memory of a process wont be modified by another process and each process has it's own memory section?
Each process has its own user address space that cannot be modified by another process UNLESS pages of the user space are mapped to multiple process through a shared memory mechanism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.