r/AskComputerScience • • Oct 08 '19

How does a process know where it currently is in the code?

If I create a process and after a second suspend it. Where is the information stored about what current instruction it is at?

39 Upvotes

8 comments sorted by

16

u/NeoMarxismIsEvil Oct 08 '19

Not the best Wikipedia article but https://en.wikipedia.org/wiki/Context_switch#Steps

(What you’re looking for is the details of context switching.)

9

u/drohhr Oct 08 '19 edited Oct 09 '19

Computerphile just released a video on context switching y'all might want to check out.

Edit: typo

12

u/trevorsears Oct 08 '19

At a very very low level, it is common for a processor to have a PC (program counter) or IP (instruction pointer) register in which the address of the current instruction is stored.

https://en.wikipedia.org/wiki/Program_counter

Obviously if the process has been suspended the current instruction is no longer one belonging to said suspended process, but this seemed relevant :)

3

u/roman_fyseek Oct 08 '19

Stack. Push and pop.

1

u/[deleted] Oct 08 '19

Ahh alright thanks :)

9

u/zacker150 Oct 08 '19

In an operating system, processes are represented by a data structure called a process control block (PCB). This data structure contains, among other things, called the program counter or instruction pointer which points to the current instruction. When the operating system switches from one process to another, it updates the data in the PCB of the outgoing process and loads the information from the PCB into the CPU registers.

0

u/[deleted] Oct 08 '19

[deleted]

1

u/yazalama Oct 09 '19

Not OP, but this is great, thank you!