Layered design in software engineering

From Processes to Threads to Goroutines Part2

When the operating system moves from one process to another, when it starts a process running and then it starts another process, that act of switching is called a context switch

When OS performs the context switch, the OS takes that context, all that state information, and saves it to memory

When I said the state, that's called the Context. It includes all those things that are unique to a process. So, it's virtual memory system like which parts of memory it can access which It can't, register values, the program counter value, stack pointer, all that stuff, it's code. All that stuff is unique to a process.‎

If the  OS runs it again, it would find its state and memory and bring it all back in, bring in its virtual address system and bring in its register values, and all that so, the process can continue from where it left off.‎

one downside of processes is that the context switching time can be long because the context switch requires taking data, writing it to memory, and then reading stuff from memory back into registers. So, there's a lot of memory access and memory access can be slow, and you want the operating system to be fast, don't want to waste time doing that. 

People said look, let's make threads. Now, they were originally called lightweight processes but what a thread is, it's like a process but it has less context. It shares some of the contexts with other threads in the process.‎

So, one process can have multiple threads inside it, These threads share a decent amount of context.‎

Threads have a unique context but context is much less than the unique context between two different processes.‎

So, when you do a context switch between two processes, that might take you a long time because there's a lot of context, this unique. But with two threads, when you go from one thread to the next in the same process, it's much faster, because there's less contexts, less data that you have to write to memory and read back from memory when you do a context switch. ‎

What happens in operating systems is that they instead of scheduling processes, they scheduled thread by thread.

That's from the operating system point of view. You can have many threads inside a process, maybe the process only has one thread which is called the main thread, but it might have multiple threads.‎





تعليقات