Notice
Recent Posts
Recent Comments
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

채록채록

[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:47

Heavy-weight인 process

  1. process는 많은 것들을 포함할 수 밖에 없다.
    1. address space(code, data), OS resources, accounting info, hardware execution state(PC, SP, registers…)
  2. 새로운 process를 만드는 것은 비용이 많이 든다.
    1. all of data struction must be allocated and initialized
    2. memory 많이 잡아먹는다.
  3. Inter Process Communication도 비싸다.
    1. OS를 통해서 가기 때문에 kernel 개입이 크다. (Shared memory, memory-queue 둘 다)
    2. overhead of system calls = cpu utilization이 떨어진다.

그래서 등장한 Thread Concept

  1. 하나의 process 안에서 path만 나눠서 switch해보자.
    1. 근간 code section (static 영역) 공유
      1. IPC 오버헤드 감소
    2. dependency를 줄이기 위해 따로 register를 가지긴한다.
    3. 함수별 data section인 stack도 따로 가진다.
    4. process에서 execution state만 따로 분리해낸 것
      1. PC, SP, register, etc
  2. 함수를 thread로 등록한다.
    1. thread 생성 system call 사용
  3. memory 상에 동시동작하는 것처럼 보인다.
    1. single-threaded process : func1 다 수행 후 func2
    2. multithreaded process : 여러개의 thread에서 func1, func2가 병렬처리 되는 것처럼 보인다.

Address Space with Threads

  1. 공유 : code segment, static data segment, heap
    1. 그러나 code segment에서 thread별로 PC 가진다
  2. 개별 : stack
    1. guard 영역
      1. 기존 stack 구조를 따르나 thread별 stack 사이에 guard 영역 존재
      2. os가 알아서 처리

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() 세션 연결 요청
}
  1. Multiprocess Model
    1. using fork() to create new processes to handle requests in parallel
    2. parent process는 socket close하고 다시 세션연결 요청(accept()) 대기
      1. 간단한 task처리에도 overkill
  2. Multithread Model
    1. 하나의 process
      1. socket 한 개 유지한채로 요청 handling
    2. thread_fork(hadle_request, sock)로 each request에 대한 thread 생성
      1. handle_request(int sock)
        1. 자식 process에서 해야하는 무언가를 함수로 정의
      2. thread에서 close socket
        1. 원래는 parent process에서 fork로 child process 생성하고 연결 끊고 waiting 상태였음.

Multicore Programming

  1. single-core system
    1. Concurrent execution(동시성)
      1. 같은 종류의 작업이 가능한 많이 동시에
    2. 병렬처리하는 것 처럼 보이지만 엄청 확대해보면 thread 여러 개가 빠르게 switching
    3. scheduling만 고려
    4. time quantum 내 수행되는 것 + i/o interrupt 일어나면 switching될 것
  2. multicore system
    1. Parallel execution(병렬성)
      1. 어떤 계산을 병렬적으로 수행하여 빠르게 끝낼 수 있도록
    2. dependency가 없는 threads들로 조합해서 multicore에 할당
      1. 어떤 thread를 어떤 core에 넣어야할지 고려
    3. Data parallelism
      1. 어떤 문제의 계산량을 분할 & core에 분배 → 합치면 최종결과
    4. Task parallelism
      1. 전체 data를 공유하고 동시동작
      2. 더 많이 사용됨
      3. Pthreads(posix threads) : task parallelism을 위한 library

Thread management

  1. User Threads
    1. user-level threads library
      1. kernel을 뜯어고치지 않고
      2. library 형태로 user software로 구현
      3. time quantum 내에 thread 관리
    2. 장점
      1. small and fast
      2. 어쨌든 kernel overhead는 줄였다.
    3. 단점
      1. thread 관리 & scheduling 관련 사항을 kernel이 모른다.
      2. 먼저 scheduling된 thread 때문에 wait 상태가 풀릴 때까지 나머지 thread들이 다 기다려야한다.
        1. thread A가 I/O요청하면 threadB,C…를 포함하고 있는 process 1이 다 내려간다. 나머지 threadB, C는 process가 부여받은 time quantum을 써도 다 모자를 판인데..
    4. POSIX Pthreads, Mach C-thread, Solaris threads
  2. Kernel Threads
    1. thread table in Kernel
      1. 힘들지만 process 관리하듯이 kernel이 system calls를 받아 thread creation & management
        1. kernel threads are cheaper than processes
    2. windows 몇개, solaris, linux….
  3. Models
    1. many user threads - one kernel threads
      1. user level threads library가 여기 해당
    2. one user threads - one kernel threads
      1. threads 개념 도입 전 multi-process가 여기 해당
    3. many user threads - many kernel threads
      1. kernel overhead를 줄일 수 있음 (ex 4 user threads - 3 kernel threads)

Threading Issues

  1. fork() : thread까지도 복사? process만 복사?
    1. 규칙 : fork()할거면 fork() 이후에 thread 만들어서 처리하자.
  2. Thread cancellation, Signal handling, Thread pools, thread specific data…

Thread design Space

  1. one thread/one process in one process
    1. MS/DOS와 같은 batch system
  2. one thread in multi processes
    1. older unix
  3. many thread in one process
    1. JAVA
      1. jvm이라는 덩치 큰 process 위에서 실행시키는 process들이 thread형태로 얹어서 실행된다.
  4. many threads in multi process
    1. 전형적인 thread 적용 version - 여러개 혼용해서 사용ㅇㅇ
    2. Mach, Linux, NT, Chorus…