[OS] Thread

HyunDong Lee·2021년 10월 19일
0

OS

목록 보기
5/7
post-thumbnail

Thread model

  • process model

    • resource grouping
      • Address space, open files, child processes, accounting information
    • Thread
      • program counter, registers, stack
  • Thread model

    • Allows multiple thread of executions to take place in ther same process environment(multithreading)
    • Entity scheduled for execution on the CPU
    • Lightweight process (관련된 정보 적다)

(a) Three processes each with one thread

  • The three proccesses are unrelated

(b) One process with three threads (sharing the same address space)

  • The three threads are part of the same job and are closely cooperating with each other
  • No protection between threads

    • Multiple threads in a process cooperaate.
  • Per process items

    • shared by all threads in a process => table entry로 저장
  • Per thread items

    • private to each thread => thread마다 수행 위치 다르면 각각 저장한다.
Per process itemsPer thread items
Address spaceProgram counter
Global variableRegister
Open filesStack
Child ProcessesState
pending alarms
Signals and signal handlers
Accounting information
  • Multiple threads of execution share a set of resources so they can work together closely to perform some task.
  • Like a traditional process, a thread can be in any one of seeveral states :running, blocked, ready
  • Each thread has its own stack
  • Thread-related library procedures
    • thread_create (name of a procedure for the new thread to run)
    • thread_exit
    • thread_wait
      • blocks the calling thread until a specific thread has exited, 특정 thread exit될 때까지 block
    • thread_yield
      • Allows a thread to voluntarily give up the CPU to let another thread run, CPU에서 자발적 양보, 할일 없을 때, mutex값에서 사용됨, busy waiting x

why use thread?

  • In many applications, nultiple activities are going on at once
  • Threads are easier to create and destroy than processes.
  • Performance gain
  • Real parallelism is possible with multiple CPUs.

Thread usage

  • A word processor with three threads
    • Interactive thread
    • Reformatting thread
    • Disk-backup thread
  • A multithreaded Web server

Implementin Threads in user space

Adv of User-level ThreadDisadv of User-level Thread
Can be implemented on an OS that does not supprot threadHow blocking system calls are implemented
Thread switching is faster than the kernel-level threadGiving up the CPU
Allows each process to have its own cutomized scheduling algorithm
Scales better

Adv

kernel-level thread은 각 테이블 구성되는데 처음에 kernel에 얼마나 thread table 잡을지? 가 곤란하다. process당 thread table을 가지고 있고 개수도 정해져서 나온다.

Disadv

user level에서는 process 존재만 알아서 같은 프로세스에 속한 thread도 모두 block될 수 있다. read lib sys call x -> thread yield => system lib call 수정해야한다. user level thread의 문제점 중 하나. -> 다른 프로세스가 수행될 기회가 없을 수 있다.

Implementing threads in the kernel

  • A threads package managed by kernel : Does not require any new, nonblocking system calls 커널이 thread 존재를 알아서 직접 block시킬 수 있다. 단점은 thread switching is slow...

0개의 댓글