Lecture #4 쓰레드(Thread).

Slides:



Advertisements
Similar presentations
When Poll is Better than Interrupt
Advertisements

OS 소개 Introduction 설계목표 기본 용어 Resource Management History.
Linux Debugging issues
Mar OSEK/VDK Woo Dong Kyun.
Chapter 7 ARP and RARP.
Linux/UNIX Programming
소프트웨어와 운영체제.
1.1 병렬처리의 한계와 가능성 1.2 병렬처리의 단위 1.3 병렬컴퓨터의 분류 1.4 병렬컴퓨터의 성능 척도
제 2장 컴퓨터 구조.
인공지능실험실 석사 2학기 이희재 TCP/IP Socket Programming… 제 11장 프로세스간 통신 인공지능실험실 석사 2학기 이희재
정보통신실습 및 특강(5)
기본 컴퓨터 프로그래밍 Lecture #6.
강좌 개요 2009년 1학기 컴퓨터의 개념 및 실습.
Operating Systems Overview
7장 : 캐시와 메모리.
Internet Computing KUT Youn-Hee Han
Uniprocessor Scheduling
운영체제 (Operating Systems)
프로세스 관리.
6장 단일 프로세서 스케줄링.
컴퓨터 과학 개론 √ 원리를 알면 IT가 맛있다 컴퓨터 과학도를 위한 첫 전공서 ehanbit.net.
12. 데이터베이스 설계.
임베디드 운영체제 (리눅스 중심) Lecture #2.
14장. 병렬 프로세서 다루는 내용 병렬 프로세서로의 개념 병렬 처리와 병렬 컴퓨터 분류 배열 프로세서와 다중 프로세서의 개념
Chapter 2 OSI 모델과 TCP/IP 프로토콜.
UNIT 07 Memory Map 로봇 SW 교육원 조용수.
운영체제 (OS: Operating System)
2장 운영 체제의 개요 운영체제의 개념 운영체제의 유형 운영체제의 발전 과정 운영체제의 구성 운영체제 서비스 시스템 구조
Lecture #3 프로세스(Process).
Chapter 2. Finite Automata Exercises
5장 스레드 (Threads) 스레드 (Threads) 개요 ~
운영체제 (Operating Systems) (Multi-Thread Programming)
병렬 처리/컴퓨터 기초.
Xen and the Art of Virtualization
제3,4,5장 프로세스, 스레드 관리 CPU 스케줄링.
Sungkyunkwan University OS Project Dongkun Shin
Chapter 10. 파일 시스템 인터페이스(File System Interface)
파일 시스템 인터페이스(File System Interface)
Operating system #2 Process
제2장 프로세스 이나현.
Operating Systems Chapter 03 프로세스 개념.
Operating Systems Chapter 03 프로세스 개념.
제5장 CPU스케줄링(CPU Scheduling)
컴퓨터 시스템 개관 시스템 프로그래밍 - Lecture #1 신라대학교 컴퓨터공학과 시스템 프로그래밍.
Cache Manager Yonghyun Kim Microsoft MVP Dev 5 team leader, ESTsoft
UNIT 07 Memory Map 로봇 SW 교육원 조용수.
제19강 병렬처리시스템 1.
제6장 교착상태 OS 컴퓨터 운영체제 Operating Systems
Linux/UNIX Programming
리눅스 시스템 & 커널 기초 P.46 – P.53 이름: nsh009 학번: 112 1/20.
운영체제(Operating System)
운영체제 (Operating Systems) (Memory Management Strategies)
자바 5.0 프로그래밍.
2. 프로세스 관리 프로세스 중단과 재시작 중단과 재시작을 추가한 프로세스 상태 변화
Chapter 12 Memory Organization
Machine architecture Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Chapter 2.
Linux/UNIX Programming
운영체제 (Operating System) (하드웨어와 응용 프로그램 사이의 인터페이스 역할을 담당하는 시스템 소프트웨어)
Lecture #3 프로세스(Process) 신라대학교 컴퓨터공학과 - 운영체제.
Machine architecture Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Chapter 2.
1. 관계 데이터 모델 (1) 관계 데이터 모델 정의 ① 논리적인 데이터 모델에서 데이터간의 관계를 기본키(primary key) 와 이를 참조하는 외래키(foreign key)로 표현하는 데이터 모델 ② 개체 집합에 대한 속성 관계를 표현하기 위해 개체를 테이블(table)
System Security Operating System.
Machine architecture Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Chapter 2.
1. 데이터베이스 환경.
과제 4: Thread (5월 9일까지) 4장 연습문제 풀이
I/O Management and Disk Scheduling
Lecture #3 프로세스(Process).
Operating Systems Overview
Lecture 7 7-Segment LED controller using u-controller
Chapter 7: Deadlocks.
Presentation transcript:

Lecture #4 쓰레드(Thread)

“프로세스” 란?(1) 실행중인 프로그램 프로세스 상태(process states) 프로세스 이미지(process image) 실행하기 위해 메모리에 적재되어거나 실행 준비중인 프로그램 사용자가 프로그램을 실행시키면 “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) Operating System

“프로세스” 란?(2) Process Switching 프로세스의 상태 변환 Process context switching 병렬 실행(Concurrent processing) on SMP Machine 프로세스간의 통신(Interprocess communication) 프로세스간의 데이터 전송 Signal, Pipe, Shared Memory, Message Queue 등 Kernel에 의해 수행 Operating System

프로세스 특성(1) 자원 할당의 단위(Unit of resource ownership) 프로세스가 실행에 필요한 자원이 프로세스 단위로 할당된다 프로세스 이미지(process image)를 가지는 메모리 공간 다양한 자원(files, I/O devices...)에 대한 제어 프로세서 실행의 단위(Unit of dispatching) 프로세스는 프로그램에서 하나의 실행 패스(path) 이다 프로세서는 여러 개의 프로세스에 의해 공유된다 각각의 프로세스는 실행 정보와 실행 우선순위를 갖는다 Operating System

프로세스 특성(2) 최근 위의 두 가지 특성이 독립적으로 정의되고 있다 자원 할당 단위와 프로그램 실행 단위를 분리 프로세서 실행 단위(unit of dispatching)  a thread or a LWP(lightweight process) 자원 할당 단위 (unit of resource ownership)  a process or a task Operating System

Threads(1) 프로그램 실행 단위 또는 프로그램 실행 패스(path) Thread는 실행 상태를 갖는다(running, ready, blocked etc.) 실행 상태가 변할 때에 thread context switching이 수행된다 Thread 별로 프로그램 실행 스택(execution stack)과 지역 변수를 위한 메모리를 갖는다 Operating System

Threads(2) 프로세스에 할당된 메모리 영역과 자원을 접근한다 하나의 프로세스를 구성하는 모든 쓰레드는 프로세스의 자원을 공유한다 하나의 쓰레드가 메모리 내용을 변경하면 다른 모든 쓰레드는 그 메모리 내용을 접근할 수 있다 하나의 쓰레드가 파일을 열면 다른 쓰레드가 그 파일을 접근할 수 있다 Operating System

Multithreading 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 Operating System

Threads and Processes Operating System

Single Threaded and Multithreaded Process Models Thread Control Block contains a register image, thread priority and thread state information Operating System

Threads States Three key states: running, ready, blocked 동일한 프로세스을 구성하는 쓰레드는 메모리 공간을 공유하므로 중단 상태(suspend state)을 갖지 않는다 하나의 쓰레드를 suspending (i.e: swapping) 하는 것은 동일한 프로세스의 모든 쓰레드를 suspending 하게 된다 프로세스 종료는 프로세스 내의 모든 쓰레드를 종료한다 Operating System

Thread 응용(1) 응용 #1: a file server on a LAN 짧은 기간에 여러 개의 파일 접근 요청을 처리하여야 한다 각각의 파일 접근 요청에 대해 하나의 thread를 생성하는 것이 훨씬 효과적이다 SMP machine의 경우: 여러 개의 쓰레드가 각각 다른 프로세서에서 동시에 실행할 수 있다 Web server 등 Operating System

Thread 응용(2) 응용 #2: User Interface thread on Windows 하나의 쓰레드는 메뉴를 생성하여 사용자 입력을 받고 다른 쓰레드는 사용자 명령을 처리한다 Operating System

Thread 응용(3) 응용 #3: 순서대로 실행할 필요가 없는 여러 개의 독립적인 부분으로 구성되어 있는 응용 각각의 독립적인 부분을 쓰레드로 구현한다 하나의 쓰레드가 입력 동작에 대한 대기 상태가 되면 다른 쓰레드가 실행 상태로 전환된다 (thread switching instead of switching to another process) Operating System

Thread 장점(1) 프로세스 보다 새로운 쓰레드를 생성하는데 시간이 덜 걸린다 프로세스 보다 쓰레드를 종료하는데 시간이 덜 걸린다 동일한 프로세스에 있는 두 개의 쓰레드 사이의 전환이 더 적은 시간을 소요한다 Operating System

Thread 장점(2) 동일한 프로세스에 있는 쓰레드는 메모리 및 파일 등의 자원을 공유하므로 커널(kernel)의 도움없이 상호간에 통신할 수 있다 따라서, 공유되는 메모리나 파일에 대해 일관성 있는 접근을 지원하기 위해 쓰레드 간의 동기화가 필수적으로 요구된다 Operating System

일관성 없는 데이터 접근의 예 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 Operating System

Process와 Thread 비교(1) 하드 디스크로 부터 필요한 데이터를 읽어 오는 producer 모듈과 읽어온 데이터를 처리 하는 consumer 모듈로 구성된 프로그램 Producer 모듈과 consumer 모듈은 병행 수행(concurrent execution)이 가능하다 프로그램 실행 모델 Single process execution Multiple process execution Multithreading execution Operating System

Process와 Thread 비교(2) Single process execution : Process Producer Sequential execution Process Hard Disk Producer Main memory (shared memory) Consumer Operating System

Process와 Thread 비교(3) Multiple process execution : Kernel Concurrent execution with high communication overhead Producer process Consumer process Main memory (Shared Memory) Hard Disk Kernel Operating System

Process와 Thread 비교(4) Multithreading execution : Process Kernel Concurrent execution with low communication overhead Process Producer Thread Consumer Thread Hard Disk Local memory Kernel Operating System

User-Level Threads (ULT) 커널은 쓰레드 존재를 알지 못하며 프로세스을 관리 한다 응용 프로그램이 thread library을 이용하여 모든 쓰레드를 관리한다 Thread switching은 kernel mode privileges을 요구하지 않는다 (no mode switch) 쓰레드 간의 실행 스케줄링은 응용 프로그램에 의해 정의된다 Operating System

Operating System

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

ULT에 대한 커널 동작 커널은 쓰레드 존재 여부를 알지 못하며 프로세스 동작 만을 관리한다 쓰레드가 시스템 콜을 요구하면 프로세스 전체가 대기 상태가 된다 Thread library에 대해서는 프로세스가 대기 상태에 있더라도 현재의 쓰레드를 실행 상태로 인식한다 쓰레드 상태는 프로세스 상태와 독립적이다 Operating System

ULT 장점 장 점: Thread switching이 커널과 상관없이 이루어진다: no mode switching Thread scheduling이 응용 프로그램에 의해 정의 할 수 있다 응용 프로그램에 가장 적합한 스케줄링 알고리즘을 선택할 수 있다 ULT은 어떠한 운영체제에서도 실행할 수 있다 단지 thread library가 필요하다 Operating System

ULT 문제점 문제점: 대부분의 시스템 콜에 대해 커널은 프로세스를 대기 상태로 전환시킨다 프로세스 내의 모든 쓰레드가 대기 상태가 된다 커널은 프로세스를 프로세서(CPU)에 할당한다 프로세스 내의 두 개의 쓰레드는 다중 프로세서가 있더라도 동시에 실행할 수 없다 Operating System

Kernel-Level Threads (KLT) 커널이 모든 쓰레드 관리를 수행한다 thread library가 없으며 커널 쓰레드 기능에 대한 API가 지원된다 커널은 프로세스와 쓰레드에 대한 문맥 정보(context information) 을 유지한다 커널이 쓰레드 간의 전환을 수행한다 커널이 쓰레드 스케줄링을 수행한다 예: Windows NT and OS/2 Operating System

KLT 장점 장 점: 커널은 쓰레드 수준에서 프로세서 스케줄링을 수행한다 쓰레드 수준에서 blocking이 수행된다 커널은 하나의 프로세스 내의 쓰레드가 다중 프로세서에서 동시에 실행될 수 있도록 스케줄링 한다 쓰레드 수준에서 blocking이 수행된다 하나의 쓰레드가 시스템 콜로 대기 상태가 되더라도 프로세스 내의 다른 쓰레드는 실행 상태를 유지한다 커널 루틴 자체도 multithread로 구성할 수 있다 Operating System

KLT 문제점 문제점 프로세스 내의 쓰레드 전환(thread switching)이 커널에 의해 수행된다 쓰레드 전환 오버헤드가 크다 프로그램 실행 속도가 떨어진다 Operating System

Combined ULT/KLT Approaches 쓰레드 생성은 응용 프로그램에 의해 수행 쓰레드 스케줄링과 동기화가 사용자 공간에서 수행 사용자는 KLTs 개수를 조정한다 예: Solaris Operating System

Relationship Between Threads and Processes Operating System

Categories of Computer Systems Single Instruction Single Data (SISD) stream Single processor executes a single instruction stream to operate on data stored in a single memory Single Instruction Multiple Data (SIMD) stream Each instruction is executed on a different set of data by the different processors Operating System

Categories of Computer Systems Multiple Instruction Single Data (MISD) stream A sequence of data is transmitted to a set of processors, each of which executes a different instruction sequence. Never implemented Multiple Instruction Multiple Data (MIMD) A set of processors simultaneously execute different instruction sequences on different data sets Operating System

Operating System

Symmetric Multiprocessing Kernel can execute on any processor Typically each processor does self-scheduling form the pool of available process or threads Operating System

Operating System

Multiprocessor Operating System Design Considerations Simultaneous concurrent processes or threads Scheduling Synchronization Memory management Reliability and fault tolerance Operating System

Microkernels Small operating system core Contains only essential core operating systems functions Many services traditionally included in the operating system are now external subsystems Device drivers File systems Virtual memory manager Windowing system Security services Operating System

Operating System

Benefits of a Microkernel Organization Uniform interface on request made by a process Don’t distinguish between kernel-level and user-level services All services are provided by means of message passing Extensibility Allows the addition of new services Flexibility New features added Existing features can be subtracted Operating System

Benefits of a Microkernel Organization Portability Changes needed to port the system to a new processor is changed in the microkernel - not in the other services Reliability Modular design Small microkernel can be rigorously tested Operating System

Benefits of a Microkernel Organization Distributed system support Message are sent without knowing what the target machine is Object-oriented operating system Components are objects with clearly defined interfaces that can be interconnected to form software Operating System

Microkernel Design Low-level memory management Mapping each virtual page to a physical page frame Operating System

Microkernel Design Interprocess communication I/O and interrupt management Operating System

Windows Processes Implemented as objects An executable process may contain one or more threads Both processes and thread objects have built-in synchronization capabilities Operating System

Operating System

Windows Process Object Operating System

Windows Thread Object Operating System

Windows 2000 - Thread States Ready Standby Running Waiting Transition Terminated Operating System

Operating System

Solaris 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 Operating System

Process 2 is equivalent to a pure ULT approach Process 4 is equivalent to a pure KLT approach We can specify a different degree of parallelism (process 3 and 5) Operating System

Operating System

Solaris: 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 Operating System

Solaris Lightweight Data Structure Identifier Priority Signal mask Saved values of user-level registers Kernel stack Resource usage and profiling data Pointer to the corresponding kernel thread Pointer to the process structure Operating System

Operating System

Linux Task Data Structure State Scheduling information Identifiers Interprocess communication Links Times and timers File system Address space Processor-specific context Operating System

Linux States of a Process Running Interruptable Uninterruptable Stopped Zombie Operating System

Operating System