DEV Community

ivinieon
ivinieon

Posted on

1

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

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay