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

67 41 0
hệ điều hànhsynchronization 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

Đồng Bộ Quá Trình SinhVienZone.com https://fb.com/sinhvienzonevn Nội dung    Khái niệm Vùng tranh chấp (critical section) Các giải pháp dùng lệnh máy thông thường ● Giải thuật Peterson, giải thuật bakery     Các giải pháp dùng lệnh cấm ngắt lệnh máy đặc biệt Semaphore Semaphore toán đồng Monitor SinhVienZone.com https://fb.com/sinhvienzonevn Bài toán đồng (1/2) • Khảo sát process/thread thực thi đồng thời chia sẻ liệu (như ghi shared memory) hệ thống ● uniprocessor, ● shared memory multicore/multiprocessor   Nếu kiểm soát truy cập liệu chia sẻ chúng trỡ nên không quán Để trì quán liệu, hệ thống cần có chế bảo đảm thực thi có trật tự process đồng thời SinhVienZone.com https://fb.com/sinhvienzonevn Bài toán đồng (2/2)  Hai lớp toán đồng bộ: ● Hợp tác  Bài toán producer-consumer: bounded buffer ● Cấp phát tài nguyên  Bài toán loại trừ tương hỗ: đồâng nhiều trình sử dụng tài nguyên không chia sẻ đồâng thời (= sử dụng trình)  Bài toán Dining Philosophers SinhVienZone.com https://fb.com/sinhvienzonevn “Đồng thời” bao gồm “song song”   Trên uniprocessor hay shared memory multiprocessor, trình chạy đồng thời Trên shared memory multiprocessor, trình chạy song song trình trình Shared memory Biến chia sẻ Quá trinh vaø code vaø private data SinhVienZone.com https://fb.com/sinhvienzonevn Baøi toán Producer-Consumer (1/3)  Ví dụ: Bounded buffer, thêm biến ñeám count #define BUFFER_SIZE /* buffers */ typedef struct { } item; item buffer[BUFFER_SIZE]; int in = 0, out = 0, count = 0; SinhVienZone.com https://fb.com/sinhvienzonevn Bài toán Producer-Consumer (2/3)  Quá trình Producer item nextProduced; while(1) { while (count == BUFFER_SIZE); buffer[in] = nextProduced; count++; in = (in + 1) % BUFFER_SIZE; }  trỏ Quá trình Consumer item nextConsumed; while(1) { while (count == 0); nextConsumed = buffer[out]; count ; out = (out + 1) % BUFFER_SIZE;} SinhVienZone.com trỏ biến count chia sẻ producer consumer https://fb.com/sinhvienzonevn Bài toán Producer-Consumer (3/3)  • Các lệnh tăng/giảm biến count tương đương ngôn ngữ máy là: Producer count++: • register1 = count • register1 = register1 + • count = register1 • Consumer count : • register2 = count • register2 = register2 - • count = register2 Trong đó, registeri ghi CPU SinhVienZone.com https://fb.com/sinhvienzonevn Đồng lệnh đơn nguyên • Mã máy lệnh tăng giảm biến count thực thi xen kẽ  Giả sử count Chuỗi thực thi sau xảy ra: 1: producer register1 := count {register1 = 5} producer register1 := register1 + {register1 = 6} consumer register2 := count {register2 = 5} consumer register2 := register2 - {register2 = 4} 3: producer count := register1 {count = 6} 4: consumer count := register2 {count = 4} 2: Cả hai process thao tác đồng thời lên biến chung count Trò biến chung không quán thao tác hai process Giải pháp: lệnh count++, count phải đơn nguyên (atomic), nghóa thực lệnh đơn, không thực thi đan xen SinhVienZone.com https://fb.com/sinhvienzonevn Race condition   Race condition: tình nhiều process truy xuất thao tác đồng thời lên liệu chia sẻ (như biến count); kết cuối việc truy xuất đồng thời phụ thuộc thứ tự thực thi lệnh (máy) thao tác lên liệu Để liệu chia sẻ trình Producer Consumer quán, cần bảo đảm cho process thao tác lên liệu chia sẻ Do đó, cần có chế đồng hoạt động process SinhVienZone.com https://fb.com/sinhvienzonevn 10 Các vấn đề với semaphore    Các tác vụ wait(S) signal(S) nằm rải rác process  Người lập trình khó nắm bắt hiệu ứng chúng Nếu không sử dụng  xảy deadlock starvation Một process “die” khiến process khác sử dụng bieán semaphore “die” theo signal(mutex) … critical section … wait(mutex) SinhVienZone.com wait(mutex) … critical section … wait(mutex) signal(mutex) … critical section … signal(mutex) https://fb.com/sinhvienzonevn 53 Monitor (1/2)  Các vấn đề sử dụng semaphore ● Quá nhiều “bậc tự do”  Monitor construct ngôn ngữ lập trình cấp cao ● Xuất nhiều ngôn ngữ lập trình đồng thời  Concurrent Pascal, Modula-3, Java,…  Có thể thực semaphore SinhVienZone.com https://fb.com/sinhvienzonevn 54 Monitor (2/2)  Kiểu module phần mềm, bao gồm ● Một nhiều thủ tục (procedure) ● Một đoạn code khởi tạo (initialization code) ● Các biến liệu cục (local data variable) SinhVienZone.com  Ngữ nghóa monitor ● Shared variable truy xuất thủ tục monitor ● Process “vào monitor” cách gọi thủ tục monitor ● Các thủ tục monitor loại trừ tương hỗ https://fb.com/sinhvienzonevn 55 Mô hình monitor shared data waiting queue of threads trying to enter the monitor at most one thread in monitor at a time SinhVienZone.com operations (methods) https://fb.com/sinhvienzonevn 56 Syntax cuûa monitor monitor monitor-name { shared variable declarations procedure P1 (…) { } procedure P2 (…) { } procedure Pn (…) { } initialization code (…){ } } SinhVienZone.com https://fb.com/sinhvienzonevn 57 Condition variable  Để thực concept “process đợi monitor”, monitor phải khai báo biến điều kiện (condition variable) condition a, b;   Các biến điều kiện cục truy cập bên monitor Chỉ thao tác lên biến điều kiện hai thủ tục: ● a.wait: process gọi tác vụ block “trên biến điều kiện” a  process tiếp tục thực thi có process khác thực tác vụ a.signal ● a.signal: phục hồi process block biến điều kiện a  Nếu có nhiều process: chọn  Nếu process: tác dụng ● Nhận xét: condition variable semaphore! SinhVienZone.com https://fb.com/sinhvienzonevn 58 Monitor coù condition variable    SinhVienZone.com Process đợi entry queue condition queue (a, b,…) Khi gọi a.wait, process block chuyển vào condition queue a Lệnh a.signal chuyển process từ condition queue a sang trạng thái ready https://fb.com/sinhvienzonevn 59 Producer-Consumer with Monitors (Hoare) Monitor bounded_buffer { buffer resources[N]; condition not_full, not_empty; produce(resource x) { if (array “resources” is full, determined maybe by a count) wait(not_full); insert “x” in array “resources” không hiểu signal(not_empty); } consume(resource *x) { if (array “resources” is empty, determined maybe by a count) wait(not_empty); *x = get resource from array “resources” signal(not_full); } SinhVienZone.com https://fb.com/sinhvienzonevn 60 Producer-Consumer with Monitors (Mesa) Monitor bounded_buffer { buffer resources[N]; condition not_full, not_empty; produce(resource x) { while (array “resources” is full, determined maybe by a count) wait(not_full); insert “x” in array “resources” signal(not_empty); } consume(resource *x) { while (array “resources” is empty, determined maybe by a count) wait(not_empty); *x = get resource from array “resources” signal(not_full); } SinhVienZone.com https://fb.com/sinhvienzonevn 61 Producer-Consumer with Monitors static count = 0; static Cond full, empty; static Mutex lock; Enter(Item item) { Acquire(lock); while (count==N) Wait(lock, full); insert item into buffer count++; if (count==1) Signal(empty); Release(lock); Remove(Item item) { Acquire(lock); while (count==0) Wait(lock, empty); remove item from buffer count ; if (count==N-1) Signal(full); Release(lock); } } Wait( mutex, condition ): unlock the mutex and enqueued on the condition variable SinhVienZone.com https://fb.com/sinhvienzonevn 62 Monitor vaø dining philosophers (1/5) monitor dp { enum {THINKING, HUNGRY, EATING} state[5]; condition self[5]; SinhVienZone.com https://fb.com/sinhvienzonevn 63 Monitor vaø dining philosophers (2/5) void pickup(int i) { state[ i ] = HUNGRY; test( i ); if (state[ i ] != EATING) self[ i ].wait(); } void putdown(int i) { state[ i ] = THINKING; // test left and right neighbors test((i + 4) % 5); // left neighbor test((i + 1) % 5); // right … } SinhVienZone.com https://fb.com/sinhvienzonevn 64 Monitor vaø dining philosophers (3/5) void test(int i) { if ( (state[(i + 4) % 5] != EATING) && (state[ i ] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[ i ] = EATING; self[ i ].signal(); } } void init() { for (int i = 0; i < 5; i++) state[ i ] = THINKING; } } SinhVienZone.com https://fb.com/sinhvienzonevn 65 Monitor dining philosophers (4/5)  Trước ăn, triết gia phải gọi hàm pickup(), ăn xong phải gọi hàm putdown() đói dp.pickup(i); ăn dp.putdown(i); suy nghó  Câu hỏi: Nếu triết gia phải đợi phục hồi gọi signal lên condition variable từ đâu? SinhVienZone.com https://fb.com/sinhvienzonevn 66 Monitor dining philosophers (5/5)  Monitor dp ● không gây deadlock gây starvation ● điều khiển tập trung  Không tồn giải thuật phân bố tất đònh cho toaùn dining philosophers ● M Rabin, ‘On the advantages of free choice: a symmetric and fully distributed solution to the dining philosophers problem,’ 1981 SinhVienZone.com https://fb.com/sinhvienzonevn 67 ... ví dụ 1, 2, 3, 3, 3, 3, 4, 5,…  Kí hiệu ● (a,b) < (c,d) a < c a = c b < d ● max(a0,…,ak ): số lớn {a0,…,ak} SinhVienZone. com https://fb .com/ sinhvienzonevn 23 Giải thuật bakery (2 /3) Process Pi,... lớn SinhVienZone. com https://fb .com/ sinhvienzonevn 24 Giải thuật bakery (3/ 3)  Giải thuật bakery bảo đảm ● Mutual exclusion ● Progress ● Starvation freedom (Không chứng minh) SinhVienZone. com. .. count ; out = (out + 1) % BUFFER_SIZE;} SinhVienZone. com trỏ biến count chia sẻ producer consumer https://fb .com/ sinhvienzonevn Bài toán Producer-Consumer (3/ 3)  • Các lệnh tăng/giảm biến count

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