hệ điều hànhthread new chương 3 sinhvienzone com

24 37 0
hệ điều hànhthread new chương 3 sinhvienzone com

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chương 3.B Thread  Khái niệm tổng quan  Các mô hình multithread  Pthread (POSIX thread) SinhVienZone.com https://fb.com/sinhvienzonevn Xem xét lại khái niệm trình  Nhìn lại phân tích khái niệm trình truyền thống: trình gồm ● Không gian nhớ / đòa  chứa text (code), data, heap ● Một luồng thực thi (single thread of execution)  program counter  register  stack ● Các tài nguyên khác (các open file, trình con,…) SinhVienZone.com https://fb.com/sinhvienzonevn Mở rộng khái niệm trình  Nhìn lại ‘cooperating processes’ ● Web server tạo process cho client để phục vụ yêu cầu trang web, hình ảnh, âm thanh…  Tạo process tốn thời gian tài nguyên  Mở rộng khái niệm trình truyền thống cách thực nhiều luồng thực thi môi trường trình SinhVienZone.com https://fb.com/sinhvienzonevn Mở rộng khái niệm trình  Quá trình gồm ● Không gian đòa ● 2’ Một hay nhiều luồng thực thi, luồng thực thi (thread) có riêng  program counter  register  stack ● Các tài nguyên khác (các open file, trình con,…) SinhVienZone.com https://fb.com/sinhvienzonevn Quá trình multithreaded  Khi trình khởi đầu có main (hay initial) thread thực thi ● Main thread tạo thread khác   Các thread process chia sẻ code, data tài nguyên khác (các file mở, ) process Quá trình đa luồng (multithreaded process) trình có nhiều luồng SinhVienZone.com https://fb.com/sinhvienzonevn Sử dụng thread formatting backup mouse Trình soạn thảo văn với ba thread SinhVienZone.com https://fb.com/sinhvienzonevn Các trường tiêu biểu cuûa PCB Tanenbaum SinhVienZone.com https://fb.com/sinhvienzonevn Process & thread information Per process items Address space Open files Child processes Signals & handlers Accounting info Global variables Per thread items Per thread items Per thread items Program counter Registers Stack & stack pointer State Program counter Registers Stack & stack pointer State Program counter Registers Stack & stack pointer State Quaù trình có ba thread SinhVienZone.com https://fb.com/sinhvienzonevn Chia sẻ CPU thread (1/2) time CPU ba trình single-threaded SinhVienZone.com https://fb.com/sinhvienzonevn Chia sẻ CPU thread (2/2) time CPU hai trình multithreaded SinhVienZone.com https://fb.com/sinhvienzonevn 10 Ví dụ chương trình sử dụng Pthread #include void* thread1(){ int i; for (i = 0; i < 10; i++){ printf(“Thread 1\n”); sleep(1); } SP1 } void* thread2(){ SP2 int i; for (i = 0; i < 10; i++){ printf(“Thread 2\n”); sleep(1); } int main(){ pthread_t th1, th2; pthread_create(&th1, NULL, thread1, NULL); pthread_create(&th2, NULL, thread2, NULL); sleep(20); return 0; } Stack Heap thread1 stack thread2 stack Data PC1 Text PC2 Sơ đồ nhớ Chương trình chạy có thread? SinhVienZone.com https://fb.com/sinhvienzonevn 11 Ưu điểm thread  Tính đáp ứng cao cho ứng dụng tương tác  Chia sẻ tài nguyên thread: vd memory  Tiết kiệm chi phí hệ thống ● Chi phí tạo/quản lý thread nhỏ so với trình ● Chi phí chuyển ngữ cảnh thread nhỏ so với trình  Tận dụng đa xử lý (multiprocessor) ● Mỗi thread chạy processor riêng, tăng mức độ song song chương trình SinhVienZone.com https://fb.com/sinhvienzonevn 12 User thread (1/4)  Một thư viện thread (thread library, run-time system) thực user space để hổ trợ tác vụ lên thread ● Thư viện thread cung cấp hàm khởi tạo, đònh thời quản lý thread  thread_create  thread_exit  thread_wait  thread_yield ● Thư viện thread dùng Thread Control Block (TCB) để lưu thông tin user thread (program counter, caùc register, stack) SinhVienZone.com https://fb.com/sinhvienzonevn 13 User thread (2/4)  Cấu trúc liệu memory layout để thực user thread TCBs D Feitelson SinhVienZone.com https://fb.com/sinhvienzonevn 14 User thread (3/4)  Kernel có mặt user thread ● Kernel biết PCB trình  Ví dụ thư viện user thread ● POSIX Pthread SinhVienZone.com https://fb.com/sinhvienzonevn 15 User thread (4/4)  Vaán đề: hệ điều hành cấp phát PCB cho moãi process ( main/initial thread) ● Blocking problem: Khi thread trở nên blocked thread khác process không tiến triển user space thread library thread library thread library PCB PCB PCB SinhVienZone.com kernel space https://fb.com/sinhvienzonevn 16 Kernel thread (1/3)  Khi kỹ thuật multithreading hệ điều hành trực tiếp hỗ trợ ● Kernel quản lý process thread – kernel thread ● Kernel thực việc đònh thời CPU cho thread trình SinhVienZone.com https://fb.com/sinhvienzonevn 17 Kernel thread (2/3)  Khi multithreading hỗ trợ kernel ● Khởi tạo quản lý thread chậm so với user thread system call overhead chuyeån user mode  kernel mode ● Tận dụng lợi kiến trúc multiprocessor ● Dù thread bò blocked, thread khác trình tiến triển  Một số hệ thống multithreading ● Windows 9x/NT/200x ● Solaris ● Linux SinhVienZone.com https://fb.com/sinhvienzonevn 18 Kernel thread (3/3)  Cấu trúc liệu memory layout để thực kernel thread TCBs D Feitelson SinhVienZone.com https://fb.com/sinhvienzonevn 19 Các mô hình thực thread  Để thống ý niệm ‘user thread’ ‘kernel thread’ trình bày, ta dùng mô hình ‘X-to-X’ sau: ● Khái niệm ‘user(-level) multithreading’ dùng để multithreading user space ● User(-level) multithreading thực theo mô hình sau  Mô hình many-to-one – tương ứng với ‘user thread’ cũ  Mô hình one-to-one – tương ứng với ‘kernel thread’ cũ  Mô hình many-to-many SinhVienZone.com https://fb.com/sinhvienzonevn 20 Mô hình many-to-one  Nhiều user(-level) thread “chia sẻ” PCB để thực thi ● Việc quản lý thread thực thông qua hàm thread library gọi user level ● Blocking problem: Khi thread trở nên blocked thread khác process không tiến triển  user space thread library TCB TCB TCB PCB kernel space Có thể thực hầu hết hệ điều hành SinhVienZone.com https://fb.com/sinhvienzonevn 21 Mô hình one-to-one  Mỗi user thread hệ điều hành quản lý thông qua kernel TCB riêng user space ● Hệ điều hành phải cấp phát kernel TCB để tạo user thread   Hệ điều hành phải cung cấp nhiều kernel TCB cho trình Ví dụ: Windows NT/2000 SinhVienZone.com PCB kernel space TCB TCB https://fb.com/sinhvienzonevn TCB 22 Mô hình many-to-many   Gọi ‘many-to-few’ Nhiều user-level thread phân chia thực thi (multiplexed) thông qua số kernel TCB trình ● Kết hợp ưu điểm mô hình many-to-one one-to-one  Ví dụ user space thread library kernel space PCB TCB TCB ● Solaris ● Windows NT/2000 với package ThreadFiber SinhVienZone.com https://fb.com/sinhvienzonevn 23 Pthread      Chuaån POSIX (IEEE 1003.1c) đặc tả API cho thủ tục tạo thread đồng thread Phổ biến hệ thống UNIX/Linux Là thư viện hỗ trợ user-level thread Tham khảo thêm ví dụ lập trình thư viện Pthread với ngôn ngữ C hệ thống Unix-like, trang 140, “Operating System Concepts”, Silberschatz et al, 6th Ed, 2003 Biên dòch thực thi chương trình multithreaded C Linux $ gcc source_file.c -lpthread –o output_file $ /output_file SinhVienZone.com https://fb.com/sinhvienzonevn 24 ... thực hầu hết hệ điều hành SinhVienZone. com https://fb .com/ sinhvienzonevn 21 Mô hình one-to-one  Mỗi user thread hệ điều hành quản lý thông qua kernel TCB riêng user space ● Hệ điều hành phải... register, stack) SinhVienZone. com https://fb .com/ sinhvienzonevn 13 User thread (2/4)  Cấu trúc liệu memory layout để thực user thread TCBs D Feitelson SinhVienZone. com https://fb .com/ sinhvienzonevn... trình có nhiều luồng SinhVienZone. com https://fb .com/ sinhvienzonevn Sử dụng thread formatting backup mouse Trình soạn thảo văn với ba thread SinhVienZone. com https://fb .com/ sinhvienzonevn Các trường

Ngày đăng: 28/01/2020, 22:11

Tài liệu cùng người dùng

Tài liệu liên quan