TID : actually the identifier of the schedulable object in the kernel
PID : the identifier of the group of schedulable objects that share memory
Multithreading Models
어떻게 user-level threads management가 가능한가?
동일한 process에 있는 thread는 동일한 address space를 가진다. 즉, address space를 library입장에서 건드릴 필요가 없다.
thread는 결과적으로 hardware context만 다르다. 이는 user-level process에서 충분히 가능하다.
The user-level process (Many-to-one Model)
라이브러리 형태로 제공.
→ 이렇게 되면 kernel을 손댈 필요가 없음.
Process 하나에 thread가 여러개 있음. 원하는 것은 thread 단위로 돌리고 싶은 것.
Run-time system이 무엇인가
A runtime system is an engine that translates a given programming language or languages into machine code. Often used as a vague term, runtime systems can be used to refer to the collected managed hardware resources and software needed for program execution
and operation.
→ 시스템 개발자가 제공하는 run-time system을 활용. 거기 안에는 thread table이 존재. 이 상황에서 커널은 전혀 모름.
문제가 무엇인가?
만약 특정 thread가 IO call을 하면 run-time system에 속한 모든 thread가 전부 다 block됨
kernel 입장에서는 각 thread를 cpu를 individual하게 사용할 수 없음.
→ many to one이라고 부르기도 함
(Many user-level threads mapped to a single kernel thread)
즉, schedule의 대상은 1개의 kernel thread
Kernel level threading
thread table을 kernel이 가지고 있음.
kernel이 process에 대한 table도 가지고 있고, thread table도 가지고 있음.
→ 이를 통해 kernel이 thread단위로 scheduling이 된다.
One-to-one Model
Linux와 window는 one-to-one model이다.
장점
many to one보다 concurrency가 높음 (특정 thread가 block되어도 나머지는 수행가능)
parallel하게 돌릴 수 있음
단점
user thread만큼 kernel thread를 만드는데 이에 대한 overhead
Many-to-Many model
one-to-one과 many-to-one방식을 혼용
→ many-to-one에서는 유저가 원하는 대로 thread를 만들 수 있었지만 parallelism이 안되고, 반면 one-to-one에서는 concurrency는 올라갔지만, 너무 많은 thread를 생성하지 않게끔 유저가 신경써야하는 문제가 발생.
→ 이를 적당히 절충 (유저는 원하는 대로 만들고, 커널은 커널 입장에서 parallel하게 돌림)
현재 대부분의 시스템에서 processing core가 증가되는 추세이므로, kernel thread를 제한시키는 것은 그다지 중요한 문제가 아니게 됨. 따라서 현재 대부분의 OS는 one-to-one mode을 차용하고 있다.
Thread Libraries
Entirely in user space with no kernel support
해당 라이브러리에 대한 코드와 자료구조는 user space 상에 존재
kernel-level library supported directly by the operating system
해당 라이브러리에 대한 코드와 자료구조는 kernel space 상에 존재
대표적으로 3가지의 thread library가 존재
POSIX Pthread
Windows
Java
Threads Interface
standard에 대한 정의가 필요해짐
그래서 등장한 것이 Pthreads
→ 어떤 함수를 제공할 것인가?
인터페이스를 통일하자는 것. 코드를 안바꾸고 컴파일만 다시 할 수 있게끔.
Solaris / Windows
Process에 대한 자료구조도 있고 thread에 대한 자료구조도 있음
→ 2개를 같이 운영
Problem
Process와 thread를 어떻게 관리할 것인가?
→ pthread로!
Linux
process가 됐든 thread가 됐든 추가적인 자료구조를 만들지 말고 1개로 통일하자.
→ Linux uses the term task rather than process or thread.
추가적으로 Clone이라는 system call을 디자인함.
철학적으로 process와 thread의 차이가 무엇인가?
Degree of sharing
fork와 비슷하게 만들어서, 얼마만큼 공유할 것인지 명시하면 된다는 개념을 제기. 그것이 clone system call
→ 인자에다가 무엇을 공유할 것인지를 명시하게 요구!
이 때문에 사실 thread 자료구조를 따로 정의할 필요가 없음.
lightweight processes 라고 부름.
→ pcb의 이름이 task_struct라고 부른 이유 (task가 process와 thread를 아우름)