운영체제 8주차
부산가톨릭 대학교, 컴퓨터공학과
변상선
HW #2
•
Named pipe 를 이용하여 두 프로세스 간에 다음의 통신을 하도록 하라
•
첫번째 프로세스에서는 양의 정수 두 개를 키보드를 통해 입력 받아 pipe를 통해 전달받은 두 숫자를 두번째 프로세스에게 전달한다
•
두번째 프로세스는 첫번째 프로세스로부터 넘겨받은 두 개의 숫자사이에 있는 모 든 값을 더해서 그 결과를 첫번째 프로세스에게 pipe를 통해 전달한다 (예를 들 어, 1과 10이 넘겨졌으면 1부터 10까지 모두 더해서 그 결과를 전달한다)
•
첫번째 프로세스는 두번째 프로세스로부터 전해받은 결과를 화면에 출력한다
•
Named pipe는 mkfifo() 함수를 통해서 생성한다
•
참고 자료
•
http://blog.divelia.com/223
•
http://molrayo.tistory.com/entry/Network-mkfifo-
%EC%98%88%EC%A0%9C
Motivation
•
Concurrency within A PROCESS•
Multithreaded process•
Increase efficiency•
Event driven task•
Display update•
Spell checking•
Answer a network request•
Process => heavy-weight, thread => light-weightMultithreaded server architecture
client
(1) request (2) create new
thread to service the request
(3) resume listening for additional client requests
server thread
Benefits
• Responsiveness
• 프로세스의 일부 (어떤 쓰레드)가 block 되어도 다른 쓰레드를 진행 가능,
• 프로세스가 특정 연산을 수행 중이어도 사용자 인터페이스는 활성화
• Resource Sharing
• 프로세스의 자원 (변수 및 메모리)를 공유, 별도의 IPC 메커니즘이 불필요
• Economy
• 쓰레드 생성 << 프로세스 생성
• 쓰레드 스위칭 << 프로세스 문맥교환
• Scalability
• Multiprocessor (multicore) 환경을 효과적으로 이용
Multicore programming
• 멀티코어 환경에서 프로세스가 멀티코어 아키텍쳐를 효과적으로 사용하게 하기 위해서는 응용프로그 램이 그에 걸맞게 만들어져야 함
• Multi-thread로 제작
• 가급적 하나 이상의 쓰레드는 언제나 실행이 되도록
• 쓰레드간의 의존성 최소화
• 쓰레드간의 공유 변수 및 자원 최소화
• Parallelism vs. concurrency
• Type of parallelism
• Data parallelism
• 데이터를 쪼개서 각 코어에서 (같은 연산을) 나누어서 수행
• 배열의 덧셈
• Task (instruction) parallelism
• 각 코어에서 다른 연산을 수행
Concurrency vs.
parallelism
T1 T2 T3 T4 T1 T2 T3 T4 T1 single core
time
…
T1 T3 T1 T3 T1
core 1
T2 T4 T2 T4 T2
core 2
time
…
…
Single and multithreaded processes
registers
code data files
stack registers registers registers
code data files
stack stack
stack
thread thread
single-threaded process multithreaded process
User thread and kernel thread
•
User thread•
Managed and scheduled by user-level thread library•
Windows Fiber•
Java Thread•
GNU Portable Threads•
Solaris Green Threads•
Kernel thread•
Managed and scheduled by kernel•
Windows => Windows Thread•
Linux, Solaris, Mac OS X => POSIX PthreadMultithreading models
•
Many-to-one•
One-to-one•
Many-to-manyMany-to-one
•
Many user-level threads => one kernel thread•
One thread blocking cause all to block•
Multiple threads may not run inparallel on multicore system because only one may be in kernel at a time
•
Example•
Solaris Green threads•
GNU portable threadsuser thread
kernel thread k
One-to-one
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of thread per process sometimes restricted due to overhead
• 대부분의 운영체제
• Windows NT/XP/2000
• Linux
• Solaris 9 and later
user thread
kernel thread k k
k k
Many-to-many model
• 여러개의 user-level thread -> 여러개의 kernel thread
• # of user-level threads >= # of kernel threads
• 필요한 만큼의 user-level thread 생성이 가능
• Windows NT/2000
• Fiber
user thread
kernel thread k
k k
Many-to-one vs. one-to- one vs. many-to-many
t
• Process 1 => thread A, thread B, thread C
• Process 2 => thread D, thread E, thread F
Process 1 thread A-B-
C
Process 2 thread D-E-
F
Process 1 thread A-B-
C
Process 2 thread D-E-
F
t
Process 1 tA tB tC
Process 2 tD tE tF
Process 1 tA tB tC
Process 2 tD tE tF
t
Process 1 tA-B tC
Process 2 tD-E tF
Process 1 tA-B tC
Process 2 tD-E tF
3-to-2
Two-level model
•
Similar to M:M, except that it allows a user thread to be bound to kernel thread•
Examples•
IRX•
HP-UX•
Solaris 8 and earlieruser thread
kernel thread k
k
k k
Thread libraries
•
Thread library provides with API for creating and managing threads•
구현 방법•
Library entirely in user space•
Kernel-level library supported by OSPthreads
•
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization•
쓰레드 구현에 대한 명세•
실제 구현은 커널 또는 사용자 레벨에서 구 현됨•
Solaris, Linux, Mac OS X•
Kernel-level 에서 구현Pthreads example
Pthread example
Pthread code for joining
10 threads
Implicit threading
• 자동화된 multi-threaded programming
• 프로그래머는 병렬로 처리되는 부분만 명시, 실 제 쓰레드 생성, 개시, 종료 동작은 컴파일러에 의해 자동적으로 명시
• Thread pools (Windows), OpenMP (Unix), Grand central dispatch (Mac OS X and iOS), Intel threading building blocks (TBB),
java.util.concurrent package
OpenMP
Semantics of fork() in thread
•
Thread에서 fork() 시스템 콜 수행•
모든 thread가 다 복제되는가? 아니면, 해당 fork()를 호출한 thread만 복제 되는가?•
Pthread는 fork()를 호출한 thread만 자 식 프로세스로 복제Signal handling
• Signal
• UNIX 운영체제에서 프로세스에게 특정 사건이 발생했음을 알리기 위해 사용
• 특정 사건
• 사용자가 강제 종료
• 타이머 만료
• 0으로 나누기 발생
• 허가되지 않은 메모리 접근
• 특정 사건에 해당되는 시그널 발생 => 해당 프로세스에게 전달 => 프로세스가 시그널을 처리 (handling)
• 시그널 핸들러
• Default handler => 커널에 정의되어 있고, 커널이 실행
• User-defined handler => 사용자 프로그램에 정의
• 사용자 정의 핸들러가 정의되면 default handler를 대치