Chapter 4: Threads

2021. 12. 10. 18:45Operating System

본 게시글은 Operating System Concepts - 9th Edition(공룡책)을 바탕으로 쓰여졌습니다


목표

  • thread의 개념 소개
  • Pthread, Windows, Java thread 라이브러리 
  • implicit threading
  • multithreaded programming 관련 문제
  • Windows 와 Linux의 threads

 


Thread

  • 실행 흐름
  • CPU의 기본 단위
  • 멀티스레드 컴퓨터 시스템의 기본
  • 대부분 applications은 멀티스레드 
  • 스레드는 application 안에서 작동
  • 스레드 생성은 프로세스 생성에 비해 가벼움 
  • 코드를 간결화하고, 효율성을 높임
  • kernels은 일반적으로 멀티스레드

 

Multithreaded Server Architecure

Multithreaded Server Architecture

 

Benefits

  • 책임감(responsiveness) - (특히 UI에서) 프로세스의 부분이  고장나도 실행을 계속
  • 자원 공유(resource sharing) - 프로세스의 자원을 더 쉽게 공유 (shared memory, message passing보다 쉬워)
  • 경제적(Economy) - 프로세스 생성보다 쌈, thread switching은 context switching보다 oberhead가 낮음
  • 확장성(Scalability) - 프로세스는 멀티 프로세서의 구조를 사용할 수 있음

 

 

Single and Multithreaded Processes

Single and Multithreaded Processes


Multicore Programming

Multicore 나 Multiprocessor는 프로그래머에게 부담을 줌

 

고려 사항

  • Dividing Activities: task가 각 코어에 독립적으로 실행되도록 나눔
  • Balance: 병렬로 실행될 task를 전체 작업에 균등한 기여도를 가질 수 있도록 분배
  • Data Splitting: task에서 사용하는 데이터는 개별 코어에서 사용가능하도록 나눔
  • Data Dependency: task가 접근하는 데이터는 2개 이상 task 사이에 종속성이 있는가
  • Test and Debugging

 

Parallelism(병렬성)

시스템이 동시에 한가지 이상의 업무를 수행

  • data parallelism: 같은 데이터의 부분 집합을 multiple core에 분배, 각각 같은 일을 함
  • task parallelism: 쓰레드를 core전역에 분배, 각 쓰레드는 고유의 일을 함

Parallelism on a multi-core system

 

Concurrency(병행성)

동시에 실행되는 것처럼 보이는 것


User Threads and Kernel Threads

User Threads

  • 사용자 수준의 thread 라이브러리에서 관리
  • 예) POSIX Pthread, Windows threads, Java threads

 

Kernel Threads

  • 커널에 의해 지원
  • 예) Windows, Solaris, Linux, Tru64 Unix, Mac OS X

Multithreading Models

user thread와 kernel thread간의 관계

 

Many-to-One

  • user thread : kernel thread = n : 1
  • 쓰레드 1개의 blocking이 모든 block을 만듬

Many-to-One Model

One-to-One

  • user thread : kernel thread = 1 : 1
  • user thread만들면 kernel thread 만들어짐
  • 더 동시성을 가짐
  • 프로세스 당 쓰레드의 수는 overhead때문에 제한됨

One-to-One Model

 

 

Many-to-Many Model

  • user thread : kernel thread = n : m
  • OS가 많은 kernel thread생성하게 함

Many-to-Many

 

Two-level Model

  • user thread > kernel thread
  • Many-to-Many와 유사, 1 : 1관계도 가능

 

 


Implicit Threading

  • thread 수 증가 -> 프로그램 correctness 문제 explicit thread로는 어려워짐
  • thread의 생성 및 관리가 compiler와 run-time 라이브러리들이 함

Threading Issues

 

Signal Handling

Signals

특정 이벤트가 발생했다고 process에게 알리기 위해 UNIX에서 사용됨

  • 특정 이벤트에 의해 생성됨
  • 프로세스에게 전달됨
  • signal handler에 의해 관리됨 - defalut or user-defined
  • 모든 signal은 kernel이 운영하는 default handler가짐
  • user-defined signal handler는 default를 override할 수 있음
  • 단일 스레드 프로그램에서 signal은 프로세스로 바로(straightforward)전달
  • signal은 어디 multi-threaded로 배달되어야 하는가?
  • signal이 사용되는 스레드
  • 프로세스 내의 모든 스레드
  • 프로세스 내의 특정 스레드 
  • 모든 signal을 받도록 특정 스레드 지정

 


Thread Cancellation

thread가 끝나기(finish)전에 그것을 강제 종료(terminate)시키는 작업

  • target thread: 취소될 thread
  • 2가지 approaches
  • Asynchronous cancellation(비동기식): 취소 요청을 받으면 즉시 취소
  • Deffered cancellation: 취소요청을 받고, target thread가 자신이 강제 종료 되야 할지를 점검
  • thread가 cancellation point에 도달할 때만 cancellation발생
  • thread가 cancel이 불가하면 cancellation을 가능할 때까지 pending함
  • Linux에서는 signal이 요청 전달

 


Thread-Local Storage(TLS)

  • 각 쓰레드가 자기 자신의 데이터 복사본을 가짐
  • 자기만 접근할 수 있는 데이터 
  • 쓰레드 생성 프로세스에 통제권이 없을 때 유용
  • static data와 유사 
  • local variable은 하나의 함수내에서만 가능, TLS는 전역적으로 사용 가능

 


 

Scheduler Activations

  • Many-to-Many 모델과 Two-level모델은 적정 수의 kernel thread를 유지하기 위해 communication 필수 
  • lightweight process(LWP)라는 user thread와 kernel thread 사이의 데이터 구조 사용
  • LWP: 프로세스가 실행할 user thread를 스케쥴링 할 수 있는, lernel thread에 붙어있는 것
  • scheduler activations은 upcalls을 제공
  • upcalls: kernel부터 쓰레드 라이브러리 안에 upcall handler까지 communication 메커니즘

 


출처

https://luv-n-interest.tistory.com/447

https://nesoy.github.io/articles/2018-09/OS-Concurrency-Parallelism

https://lazymankook.tistory.com/32

'Operating System' 카테고리의 다른 글

IPC(Interprocess Communication)  (0) 2021.12.02
System Program  (0) 2021.12.02
System Calls  (0) 2021.12.02
Operating System Service  (0) 2021.12.02
프로세스(Process) & 스레드(Thread)  (0) 2021.11.22