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?

Ramtin Mousavi
  • 332
  • 1
  • 5
  • 17
  • 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 – old_timer Dec 17 '18 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... – old_timer Dec 17 '18 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 – Ramtin Mousavi Dec 17 '18 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? – Ramtin Mousavi Dec 17 '18 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. – old_timer Dec 18 '18 at 08:03

1 Answers1

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.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • 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? – Ramtin Mousavi Dec 18 '18 at 06:35
  • 1
    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. – user3344003 Dec 18 '18 at 19:47