lecture #4

35
신신신신신 신신신신신신 - 신신신신 1 Lecture #4 Lecture #4 신신신 신신신 ( ( Thread) Thread)

Upload: tom

Post on 15-Jan-2016

60 views

Category:

Documents


0 download

DESCRIPTION

Lecture #4. 쓰레드( Thread). “프로세스” 란?(1). 실행중인 프로그램 실행하기 위해 메모리에 적재되어거나 준비중인 프로그램 사용자가 프로그램을 실행시키면 “ process” 가 생성 프로세스 상태( process states) 5-state process model: ready, running, blocked, new, stop 7-state process model: 5-state process model + suspended state - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제1

Lecture #4Lecture #4

쓰레드쓰레드 ((Thread)Thread)

Page 2: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제2

““ 프로세스” 란프로세스” 란 ?(1)?(1)

실행중인 프로그램 실행하기 위해 메모리에 적재되어거나 준비중인 프로그램 사용자가 프로그램을 실행시키면 “ process” 가 생성

프로세스 상태 (process states) 5-state process model:

ready, running, blocked, new, stop 7-state process model:

5-state process model + suspended state 프로세스 이미지 (process image)

program, data, stack PCB(process control block)

Page 3: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제3

““ 프로세스” 란프로세스” 란 ?(2)?(2)

Process Switching 프로세스의 상태 변환 Process context switching

병렬 실행 (Concurrent processing) on SMP Machine

프로세스간의 통신 (Interprocess communication) 프로세스간의 데이터 전송

Signal, Pipe, Shared Memory, Message Queue 등 Kernel 에 의해 수행

Page 4: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제4

프로세스 특성프로세스 특성 (1)(1)

자원 할당의 단위 (Unit of resource ownership) 프로세스가 실행에 필요한 자원이 프로세스 단위로 할당된다 프로세스 이미지 (process image) 를 가지는 메모리 공간 다양한 자원 (files, I/O devices...) 에 대한 제어

프로세서 실행의 단위 (Unit of dispatching) 프로세스는 프로그램에서 하나의 실행 패스 (path) 이다 프로세서는 여러 개의 프로세스에 의해 공유된다 각각의 프로세스는 실행 정보와 실행 우선순위를 갖는다

Page 5: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제5

프로세스 특성프로세스 특성 (2)(2)

최근 위의 두 가지 특성이 독립적으로 정의되고 있다 자원 할당 단위와 프로그램 실행 단위를 분리

프로세서 실행 단위 (unit of dispatching)

a thread or a LWP(lightweight process)

자원 할당 단위 (unit of resource ownership)

a process or a task

Page 6: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제6

Multithreading vs. Single threadingMultithreading vs. Single threading

Multithreading: when the OS supports multiple threads of execution within a single process

Single threading: when the OS does not recognize the concept of thread

MS-DOS support a single user process and a single thread

UNIX supports multiple user processes but only supports one thread per process

Solaris supports multiple threads

Page 7: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제7

Threads and ProcessesThreads and Processes

Page 8: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제8

Threads(1)Threads(1)

프로그램 실행 단위 또는 프로그램 실행 패스 (path) 하나의 프로세스는 여러 개의 thread 로 구성 Thread 는 실행 상태를 갖는다 (running, ready,

blocked etc.) 실행 상태가 변할 때에 thread context switching 이

수행된다 Thread 별로 프로그램 실행 스택 (execution stack)

과 지역 변수를 위한 메모리를 갖는다

Page 9: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제9

Threads(2)Threads(2)

프로세스에 할당된 메모리 영역과 자원을 접근한다 하나의 프로세스를 구성하는 모든 쓰레드는 프로세스의

자원을 공유한다 하나의 쓰레드가 메모리 내용을 변경하면 다른 모든

쓰레드는 그 메모리 내용을 접근할 수 있다 하나의 쓰레드가 파일을 열면 다른 쓰레드가 그 파일을

접근할 수 있다

Page 10: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제10

Single Threaded and Single Threaded and Multithreaded Process ModelsMultithreaded Process Models

Thread Control Block contains a register image, thread priority and thread state information

Page 11: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제11

Threads StatesThreads States

Three key states: running, ready, blocked

동일한 프로세스을 구성하는 쓰레드는 메모리 공간을 공유하므로 중단 상태 (suspend state) 을 갖지 않는다 하나의 쓰레드를 suspending (i.e: swapping) 하는 것은

동일한 프로세스의 모든 쓰레드를 suspending 하게 된다

프로세스 종료는 프로세스 내의 모든 쓰레드를 종료한다

Page 12: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제12

Thread Thread 응용응용 (1)(1)

응용 #1: a file server on a LAN 짧은 기간에 여러 개의 파일 접근 요청을 처리하여야

한다 각각의 파일 접근 요청에 대해 하나의 thread 를

생성하는 것이 훨씬 효과적이다 SMP machine 의 경우 : 여러 개의 쓰레드가 각각

다른 프로세서에서 동시에 실행할 수 있다 Web server 등

Page 13: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제13

Thread Thread 응용응용 (2)(2)

응용 #2: User Interface thread on Windows 하나의 쓰레드는 메뉴를 생성하여 사용자 입력을 받고

다른 쓰레드는 사용자 명령을 처리한다

Page 14: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제14

Thread Thread 응용응용 (3)(3)

응용 #3: 순서대로 실행할 필요가 없는 여러 개의 독립적인 부분으로 구성되어 있는 응용

각각의 독립적인 부분을 쓰레드로 구현한다 하나의 쓰레드가 입력 동작에 대한 대기 상태가 되면

다른 쓰레드가 실행 상태로 전환된다 (thread switching instead of switching to another process)

Page 15: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제15

Thread Thread 장점장점 (1)(1)

프로세스 보다 새로운 쓰레드를 생성하는데 시간이 덜 걸린다

프로세스 보다 쓰레드를 종료하는데 시간이 덜 걸린다

동일한 프로세스에 있는 두 개의 쓰레드 사이의 전환이 더 적은 시간을 소요한다

Page 16: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제16

Thread Thread 장점장점 (2)(2)

동일한 프로세스에 있는 쓰레드는 메모리 및 파일 등의 자원을 공유하므로 커널 (kernel) 의 도움없이 상호간에 통신할 수 있다

따라서 , 공유되는 메모리나 파일에 대해 일관성 있는 접근을 지원하기 위해 쓰레드 간의 동기화가 필수적으로 요구된다

Page 17: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제17

일관성 없는 데이터 접근의 예일관성 없는 데이터 접근의 예

3 variables: A, B, C which are shared by thread T1 and thread T2

T1 computes C = A+B T2 transfers amount X from A to B

T2 must do: A = A -X and B = B+X (so that A+B is unchanged)

But if T1 computes A+B after T2 has done A = A-X but before B = B+X then T1 will not obtain the correct result for C = A + B

The resource sharing requires the synchronization between threads

Page 18: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제18

ProcessProcess 와 와 Thread Thread 비교비교 (1)(1)

하드 디스크로 부터 필요한 데이터를 읽어 오는 producer 모듈과 읽어온 데이터를 처리 하는 consumer 모듈로 구성된 프로그램

Producer 모듈과 consumer 모듈은 병행 수행(concurrent execution) 이 가능하다

프로그램 실행 모델 Single process execution Multiple process execution Multithreading execution

Page 19: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제19

ProcessProcess 와 와 Thread Thread 비교비교 (2)(2)

Single process execution : Sequential execution

Producer

Consumer

Hard Disk

Main memory

(shared memory)

Process

Page 20: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제20

ProcessProcess 와 와 Thread Thread 비교비교 (3)(3)

Multiple process execution : Concurrent execution with high communication

overhead

Hard Disk

Main memory

(Shared Memory)

Producer process

Consumer process

Kernel

Page 21: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제21

ProcessProcess 와 와 Thread Thread 비교비교 (4)(4)

Multithreading execution : Concurrent execution with low communication overhead

Hard Disk Local

memory

Kernel

Producer Thread

Consumer Thread

Process

Page 22: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제22

User-Level Threads (ULT)User-Level Threads (ULT)

커널은 쓰레드 존재를 알지 못하며 프로세스을 관리 한다

응용 프로그램이 thread library을 이용하여 모든 쓰레드를 관리한다

Thread switching 은 kernel mode privileges 을 요구하지 않는다 (no mode switch)

쓰레드 간의 실행 스케줄링은 응용 프로그램에 의해 정의된다

Page 23: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제23

Threads libraryThreads library

다음 작업을 수행하는 코드를 지원한다 : 쓰레드 생성과 종료 쓰레드 간의 메시지와 데이터 전달 쓰레드 실행 스케줄링 쓰레드 문맥 전환 (thread context switching)

Page 24: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제24

ULTULT 에 대한 커널 동작에 대한 커널 동작

커널은 쓰레드 존재 여부를 알지 못하며 프로세스 동작 만을 관리한다

쓰레드가 시스템 콜을 요구하면 프로세스 전체가 대기 상태가 된다

Thread library 에 대해서는 프로세스가 대기 상태에 있더라도 현재의 쓰레드를 실행 상태로 인식한다

쓰레드 상태는 프로세스 상태와 독립적이다

Page 25: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제25

ULT ULT 장점장점

장 점 : Thread switching 이 커널과 상관없이 이루어진다 : no

mode switching Thread scheduling 이 응용 프로그램에 의해 정의 할 수

있다 응용 프로그램에 가장 적합한 스케줄링 알고리즘을 선택할

수 있다 ULT 은 어떠한 운영체제에서도 실행할 수 있다

단지 thread library 가 필요하다

Page 26: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제26

ULT ULT 문제점문제점

문제점 : 대부분의 시스템 콜에 대해 커널은 프로세스를 대기

상태로 전환시킨다 프로세스 내의 모든 쓰레드가 대기 상태가 된다

커널은 프로세스를 프로세서 (CPU) 에 할당한다 프로세스 내의 두 개의 쓰레드는 다중 프로세서가 있더라도

동시에 실행할 수 없다

Page 27: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제27

Kernel-Level Threads (KLT)Kernel-Level Threads (KLT)

커널이 모든 쓰레드 관리를 수행한다

thread library 가 없으며 커널 쓰레드 기능에 대한 API 가 지원된다

커널은 프로세스와 쓰레드에 대한 문맥 정보 (context information) 을 유지한다

커널이 쓰레드 간의 전환을 수행한다

커널이 쓰레드 스케줄링을 수행한다

예 : Windows NT and OS/2

Page 28: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제28

KLT KLT 장점장점

장 점 : 커널은 쓰레드 수준에서 프로세서 스케줄링을 수행한다

커널은 하나의 프로세스 내의 쓰레드가 다중 프로세서에서 동시에 실행될 수 있도록 스케줄링 한다

쓰레드 수준에서 blocking 이 수행된다 하나의 쓰레드가 시스템 콜로 대기 상태가 되더라도 프로세스

내의 다른 쓰레드는 실행 상태를 유지한다 커널 루틴 자체도 multithread 로 구성할 수 있다

Page 29: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제29

KLT KLT 문제점문제점

문제점 프로세스 내의 쓰레드 전환 (thread switching) 이 커널에

의해 수행된다 쓰레드 전환 오버헤드가 크다

프로그램 실행 속도가 떨어진다

Page 30: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제30

Combined ULT/KLT ApproachesCombined ULT/KLT Approaches

쓰레드 생성은 응용 프로그램에 의해 수행

쓰레드 스케줄링과 동기화가 사용자 공간에서 수행

사용자는 KLTs 개수를 조정한다

예 : Solaris

Page 31: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제31

SolarisSolaris

Process includes the user’s address space, stack, and process control block

User-level threads (threads library) invisible to the OS are the interface for application parallelism

Kernel threads the unit that can be dispatched on a processor it’s structures are maintained by the kernel

Lightweight processes (LWP) each LWP supports one or more ULTs and maps to

exactly one KLT each LWP is visible to the application

Page 32: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제32

Process 2 is equivalent to a pure ULT approachProcess 4 is equivalent to a pure KLT approachWe can specify a different degree of parallelism (process 3 and 5)

Page 33: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제33

Solaris: versatilitySolaris: versatility

We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching) Ex: Multiple windows, but only one is active at any one time

If threads may block then we can specify two or more LWPs to avoid blocking the whole application

Page 34: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제34

Solaris: user-level thread statesSolaris: user-level thread states

(attached to a LWP)

Page 35: Lecture #4

신라대학교 컴퓨터공학과 - 운영체제35

Solaris: Lightweight Process StatesSolaris: Lightweight Process States

LWP states are independent of ULT states(except for bound ULTs)