DEV Community

ivinieon
ivinieon

Posted on

What is a context switching?

What is context switching?

Context Switching

The process of replacing a running process/thread with another process/thread on a CPU/core.
Each process has a thread, which is the basic unit executed on a CPU/core.

What is a context?

The status of a process/thread, CPU, memory, etc.

Why is context switching necessary?

To execute multiple processes/threads simultaneously.

When does context switching occur?

When the given time slice(quantum) has been used up, when IO operations are required, when using other resources, etc.

Context switching in a multitasking system

→ Since the program is executed by dividing it into short periods, the user feels like programs are being executed simultaneously.

Who executes context switching?

OK kurnel: manages/supervises various resources.

How does context switching occur?

  • Process context switching: switching between different processes.

  • Thread context switching: switching between threads in the same process.

Commonalities:

  • Executed in kernel mode
    → control is transferred from the process to the kernel.

  • Registers of the CPU are replaced
    → the register state of the process being executed is saved somewhere, and another process is executed. Later, the saved register state of the first process is used to execute it again.

Registers: a location where data necessary to execute instructions is stored.

Differences:

  • Process context switching
    → additional processing related to virtual memory addresses is performed. MMU TLB

  • Thread context switching
    → memory-related processing is not needed because memory is shared within the same process.

Summary

  • Memory processing must be performed to prevent incorrect access to each other's memory areas.

  • Thread context switching is faster than process context switching.(sharing a memory)

  • Indirect effects of context switching: cache pollution.
    Since the previous thread's information may be stored in the cache, when the thread is switched, it becomes useless.

  • From the user's perspective: pure overhead.

This posting is just a study note which was written after watching youtube videos in Korean.
Youtube Link

Top comments (0)