채록채록
[OS] Process, Thread, Address Space, Webserver example, Multicore Programming, Thread management, Thread design space 본문
Developing
[OS] Process, Thread, Address Space, Webserver example, Multicore Programming, Thread management, Thread design space
김책은 2025. 7. 30. 08:47Heavy-weight인 process
- process는 많은 것들을 포함할 수 밖에 없다.
- address space(code, data), OS resources, accounting info, hardware execution state(PC, SP, registers…)
- 새로운 process를 만드는 것은 비용이 많이 든다.
- all of data struction must be allocated and initialized
- memory 많이 잡아먹는다.
- Inter Process Communication도 비싸다.
- OS를 통해서 가기 때문에 kernel 개입이 크다. (Shared memory, memory-queue 둘 다)
- overhead of system calls = cpu utilization이 떨어진다.
그래서 등장한 Thread Concept
- 하나의 process 안에서 path만 나눠서 switch해보자.
- 근간 code section (static 영역) 공유
- IPC 오버헤드 감소
- dependency를 줄이기 위해 따로 register를 가지긴한다.
- 함수별 data section인 stack도 따로 가진다.
- process에서 execution state만 따로 분리해낸 것
- PC, SP, register, etc
- 근간 code section (static 영역) 공유
- 함수를 thread로 등록한다.
- thread 생성 system call 사용
- memory 상에 동시동작하는 것처럼 보인다.
- single-threaded process : func1 다 수행 후 func2
- multithreaded process : 여러개의 thread에서 func1, func2가 병렬처리 되는 것처럼 보인다.
Address Space with Threads
- 공유 : code segment, static data segment, heap
- 그러나 code segment에서 thread별로 PC 가진다
- 개별 : stack
- guard 영역
- 기존 stack 구조를 따르나 thread별 stack 사이에 guard 영역 존재
- os가 알아서 처리
- guard 영역
Webserver example
While(1){ //다수 연결 보장
int socket = accept(); //client 요청이 들어오면 세션 연결
if ((pid = fork()) == 0){
//세션이 만들어져 있는 상태 = socket이 연결되어있는 상태
// child process에서 할 역할들 수행
// handle client request
} else{
// parent process면 close socket
} //그리고 반복문이니까 waiting for 다음 accept() 세션 연결 요청
}
- Multiprocess Model
- using fork() to create new processes to handle requests in parallel
- parent process는 socket close하고 다시 세션연결 요청(accept()) 대기
- 간단한 task처리에도 overkill
- Multithread Model
- 하나의 process
- socket 한 개 유지한채로 요청 handling
- thread_fork(hadle_request, sock)로 each request에 대한 thread 생성
- handle_request(int sock)
- 자식 process에서 해야하는 무언가를 함수로 정의
- thread에서 close socket
- 원래는 parent process에서 fork로 child process 생성하고 연결 끊고 waiting 상태였음.
- handle_request(int sock)
- 하나의 process
Multicore Programming
- single-core system
- Concurrent execution(동시성)
- 같은 종류의 작업이 가능한 많이 동시에
- 병렬처리하는 것 처럼 보이지만 엄청 확대해보면 thread 여러 개가 빠르게 switching
- scheduling만 고려
- time quantum 내 수행되는 것 + i/o interrupt 일어나면 switching될 것
- Concurrent execution(동시성)
- multicore system
- Parallel execution(병렬성)
- 어떤 계산을 병렬적으로 수행하여 빠르게 끝낼 수 있도록
- dependency가 없는 threads들로 조합해서 multicore에 할당
- 어떤 thread를 어떤 core에 넣어야할지 고려
- Data parallelism
- 어떤 문제의 계산량을 분할 & core에 분배 → 합치면 최종결과
- Task parallelism
- 전체 data를 공유하고 동시동작
- 더 많이 사용됨
- Pthreads(posix threads) : task parallelism을 위한 library
- Parallel execution(병렬성)
Thread management
- User Threads
- user-level threads library
- kernel을 뜯어고치지 않고
- library 형태로 user software로 구현
- time quantum 내에 thread 관리
- 장점
- small and fast
- 어쨌든 kernel overhead는 줄였다.
- 단점
- thread 관리 & scheduling 관련 사항을 kernel이 모른다.
- 먼저 scheduling된 thread 때문에 wait 상태가 풀릴 때까지 나머지 thread들이 다 기다려야한다.
- thread A가 I/O요청하면 threadB,C…를 포함하고 있는 process 1이 다 내려간다. 나머지 threadB, C는 process가 부여받은 time quantum을 써도 다 모자를 판인데..
- POSIX Pthreads, Mach C-thread, Solaris threads
- user-level threads library
- Kernel Threads
- thread table in Kernel
- 힘들지만 process 관리하듯이 kernel이 system calls를 받아 thread creation & management
- kernel threads are cheaper than processes
- 힘들지만 process 관리하듯이 kernel이 system calls를 받아 thread creation & management
- windows 몇개, solaris, linux….
- thread table in Kernel
- Models
- many user threads - one kernel threads
- user level threads library가 여기 해당
- one user threads - one kernel threads
- threads 개념 도입 전 multi-process가 여기 해당
- many user threads - many kernel threads
- kernel overhead를 줄일 수 있음 (ex 4 user threads - 3 kernel threads)
- many user threads - one kernel threads
Threading Issues
- fork() : thread까지도 복사? process만 복사?
- 규칙 : fork()할거면 fork() 이후에 thread 만들어서 처리하자.
- Thread cancellation, Signal handling, Thread pools, thread specific data…
Thread design Space
- one thread/one process in one process
- MS/DOS와 같은 batch system
- one thread in multi processes
- older unix
- many thread in one process
- JAVA
- jvm이라는 덩치 큰 process 위에서 실행시키는 process들이 thread형태로 얹어서 실행된다.
- JAVA
- many threads in multi process
- 전형적인 thread 적용 version - 여러개 혼용해서 사용ㅇㅇ
- Mach, Linux, NT, Chorus…