1. Trang chủ
  2. » Giáo án - Bài giảng

he dieu hanh ch02b thread new cuuduongthancong com

24 2 0

Đ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

Nội dung

Chương 2.B Thread  Khái niệm tổng quan  Các mô hình multithread  Pthread (POSIX thread) CuuDuongThanCong.com https://fb.com/tailieudientucntt 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,…) CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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,…) CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Sử dụng thread formatting backup mouse Trình soạn thảo văn với ba thread CuuDuongThanCong.com https://fb.com/tailieudientucntt Các trường tiêu biểu cuûa PCB Tanenbaum CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Chia sẻ CPU thread (1/2) time CPU ba trình single-threaded CuuDuongThanCong.com https://fb.com/tailieudientucntt Chia sẻ CPU thread (2/2) time CPU hai trình multithreaded CuuDuongThanCong.com https://fb.com/tailieudientucntt 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? CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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) CuuDuongThanCong.com https://fb.com/tailieudientucntt 13 User thread (2/4)  Cấu trúc liệu memory layout để thực user thread TCBs D Feitelson CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com kernel space https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 18 Kernel thread (3/3)  Cấu trúc liệu memory layout để thực kernel thread TCBs D Feitelson CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com PCB kernel space TCB TCB https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 24 ... 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... thread’ cũ  Mô hình one-to-one – tương ứng với ‘kernel thread’ cũ  Mô hình many-to-many CuuDuongThanCong.com https://fb.com/tailieudientucntt 20 Mô hình many-to-one  Nhiều user(-level) thread “chia... 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

Ngày đăng: 27/12/2022, 14:12

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN