[운영체제] Context Switching (컨텍스트 스위칭)

강민혁·2023년 3월 6일

Context Switching (컨텍스트 스위칭)에 대해 설명하세요

Keyword

interrupt, context, PCB, multi processing, overhead


Script

Context Switching은 CPU가 interrupt 요청에 의해 다음 우선 순위의 process가 실행되어야 할 때, 기존의 process 상태인 context를 해당 process의 PCB에 저장하고, 새로운 process의 context로 교체하는 작업을 말합니다.

multi processing을 위해 context switching은 필수적이고, 이를 통해 2개 이상의 process를 동시에 실행할 수 있게 됩니다. PCB에 저장된 context를 통해, CPU가 추후 해당 process가 하던 일을 이어서 할 수 있게 됩니다.

추가로, context switching 동안에는 CPU가 작업을 할 수 없고, 이는 overhead로 작용하기 때문에, 잦은 context switching은 성능에 좋지 않습니다. 이 overhead는 process의 레지스터 수, 데이터의 종류 등에 영향을 받기 때문에, 성능 개선이 필요하다면 context의 data 양을 줄여야 합니다.


Additional

interrupt

Context Switching은 interrupt에 의해 발생한다.

  • I/O request (입출력 요청)
  • time slice expired (CPU 사용시간 만료)
  • fork a child (자식 프로세스를 만들때)
  • wait for an interrupt (인터럽트 처리를 기다릴 때)

thread의 context switching

thread는 process와 다르게 thread 간에 공유하는 영역이 많기 때문에, thread의 context switching이 process의 context switching보다 빠르다.

thread는 각 thread간에 공유하는 데이터가 있기 때문에, cache 데이터가 유효하다. 그래서 cache 적중을 통해 더 빠른 context switching이 가능하다.


Reference

Book - 혼자 공부하는 컴퓨터 구조+운영체제

멀티 프로세스(Multi process), Context 와 Context Switching

OS - Context Switch(컨텍스트 스위치)가 무엇인가?

문맥교환(context switching)과 오버헤드(overhead)

profile
with programming

1개의 댓글

comment-user-thumbnail
2023년 3월 14일

'시분할 시스템에서 짧은 간격으로 여러 프로세스를 전환'과 context switching은 같은 의미일까요?

답글 달기