본문 바로가기

이것 저것 정리

커널 레벨 스레드 vs 유저 레벨 스레드

이것 저것 내용들을 정리하려다가 아래의 글을 발견했는데

단계적으로 용어를 정의하고 커널 레벨 스레드란 결국 무엇이고, 유저 레벨 스레드가 뭔지

쉽게 접근하여 설명해준 글이 있어 정리 해보았다.

 

https://www.quora.com/What-is-the-difference-between-user-level-and-kernel-level-threads/answer/Mohammed-Abualrob

 

What is the difference between user level and kernel level threads?

Mohammed Abualrob's answer: This is a copy & paste from an article that I wrote recently about the topic. Quora moderation used to delete my answers although I received user feedback saying the answer was what they were looking for. To avoid any moderation

www.quora.com

 

우선 커널 레벨 스레드와 유저 레벨 스레드를 정리하기 전에

아래의 '용어' 부터 짚고 가자.

 

[용어 정리]

  • 프로세스(Process) 

컴퓨터에 연속적으로 실행되고 있는 휘발성 컴퓨터 프로그램을 뜻한다.

굳이 '연속적', '휘발성'이란 단어를 쓴 이유는 한 컴퓨터에 동일한 여러 프로그램이 실행될 수 있기 때문이다.

그렇다면 프로세스는 실행되기 위해 메모리를 어떻게 사용하고 있을까?

총 4가지 부분으로 나누어지게 된다.

  • Text(Code) Section: 컴파일 된 코드
  • Data Section: 전역(global)/정적(static) 변수들 저장
  • Heap Section: 동적(dynamic) 변수들 저장 ex) C/C++ 로 예를 들어 보면 malloc, new 같은 것들
  • Stack Section: 지역(local) 변수, 함수 리턴시 돌아갈 주소값 등을 저장

운영체제(OS)는 프로레스의 상태 정보를 저장하기 위해 PCB(Process Control Block) 를 사용한다.

PCB 에는 여러 정보들을 저장하는데, 이것들을 여기서 다 다루기에는 생략하고 2가지만 기억 하자.

  • PC(Program Counter): 다음에 실행될 명령어의 주소를 저장
  • CPU Registers: 각종 레지스터들... (이하 생략)

결국

  • 프로세스 = [Code + Data + Heap + Stack + PC + PCB + CPU Registers]

 

  • 스레드(Thread)

하나의 프로세스 내에서 스레드가 1개만 실행될 수 있고, 여러 개가 실행될 수 있다.

프로세스와 비슷하게 code, data, heap section 이 따로 있는데, stack 은 따로 할당 받는다.

  • 스레드 = [Code + Data + Heap + Stack + PC + CPU Registers]

더 자세한 내용은 아래의 홈페이지 참고

https://gmlwjd9405.github.io/2018/09/14/process-vs-thread.html

 

[OS] 프로세스와 스레드의 차이 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

  • Context Switching

하나의 프로세스/스레드가 다른 프로세스/스레드로 스위칭되는 것을 뜻 한다.

  • 프로세스 Context Switching: 프로세스 state 정보 및 PC(Program Counter), CPU 레지스터, PCB(Process Control Block) 을 저장 및 복구 작업 필요. 때문에 오버헤드가 큰 편
  • 스레드 Context Switching: 스레드 CPU 레지스터, PC, stack 포인터을 저장 및 복구 작업 필요. 프로세스와 스레드간의 차이를 보면서 이미 확인했다 싶이, 스레드간에 code, data, heap 영역은 공유하고 있기 때문에 스레드간 데이터를 주고 받고 프로세스 Context Switching 보다 오버헤드가 적은 편이다.

 

  • Thread Management

위 내용들을 정리하면 결론은 다음과 같다.

  • 스레드는 프로세스 내에서 실행되는 여러 흐름의 단위 이다.
  • CPU 는 하나의 프로세스만 처리 가능.
  • Context Switching 이 실행되려면 실행 상태(Execution State) 저장이 필요하다.
  • 실행 상태는 간단하게 말하자면 PC 와 CPU 레지스터를 뜻한다.
  • PC(Program Counter) 는 다음 실행 명령어를 알려준다.
  • CPU 레지스터는 더하기 피연산자와 같은 실행 인수를 보유한다.
  • 스레드 간의 switching 에는 관리가 필요하다.
  • 관리자는 state 저장 및 복구, 다음 thread 선택 및 결정을 한다.

user level threads 는 user level library 에 의해,

kernel level threads 는 os kernel code 에 의해 관리된다.

 

  • User Level Threads

위에서 언급 했듯이, user level threads 는 user level library 에 의해 관리된다고 했지만, 작동하려면 결국 kernel 시스템이 필요하다.

다만 kernel 시스템이 모든 user level 의 thread management 를 아는 것은 아니다.

user level thread management 중, '실행되는 부분'만 관리를 한다.

  • Kernel Level Threads

kernel level threads 는 os 에 의해 관리된다. 예를 들어 Scheduling 같은 것들이 kernel code 에 의해 관리된다.

 

더 자세한 내용은 다음의 글을 참고 한다.

https://www.geeksforgeeks.org/difference-between-user-level-thread-and-kernel-level-thread/

 

Difference between User Level thread and Kernel Level thread - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

  • Thread Models

다시 환기하자면 프로세스(Process) 는 멀티 스레드로 동작할 수 있다.

이 스레드들은 user level library 혹은 os 커널 여부에 따라 user level 인지 kernel level 인지 구분될 수 있다.

더 나아가 user level threads 와 kernel level threads 간 형태에 따라 thread model 이 나뉘게 된다.

 

  • Many-To-One Model
  • One-To-One Model
  • Many-To-Many Model

 

 

[참고]

 

https://velog.io/@fredkeemhaus/%EC%9A%B4%EC%98%81%EC%B2%B4%EC%A0%9C-User-Mode-Kernel-Mode

 

[운영체제] User Mode & Kernel Mode

유저모드 (User Mode)와 커널모드 (Kernel mode)커널에서 중요한 자원을 관리하기 때문에, 사용자가 그 중요한 자원에 접근하지 못하도록 모드를 2가지로 나눈 것이다.유저(사용자)가 접근할 수 있는

velog.io

https://towardsdatascience.com/multithreading-and-multiprocessing-in-10-minutes-20d9b3c6a867

 

Multithreading and Multiprocessing in 10 Minutes

Multitasking made easy with Python examples

towardsdatascience.com

  • 멀티 스레드 VS 멀티 프로세스
Note that using multithreading for CPU-bound processes might slow down performance due to competing resources that ensure only one thread can execute at a time, and overhead is incurred in dealing with multiple threads.
On the other hand, multiprocessing can be used for IO-bound processes. However, overhead for managing multiple processes is higher than managing multiple threads as illustrated above. You may notice that multiprocessing might lead to higher CPU utilization due to multiple CPU cores being used by the program, which is expected.