1. Trang chủ
  2. » Tất cả

Chuong 8 da tien trinh

89 0 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

Chương ĐA TIẾN TRÌNH NỘI DUNG CHÍNH Giới thiệu tiến trình Đa tiến trình NET Quản lý tiến trình Đồng hóa NỘI DUNG CHÍNH Giới thiệu tiến trình Đa tiến trình NET Quản lý tiến trình Đồng hóa Tiến trình • Tiến trình (thread) thường tạo muốn làm đồng thời việc thời điểm Giới thiệu đa tiến trình • Một xử lý làm việc vào thời điểm • Nếu có hệ thống đa xử lý, theo lý thuyết có nhiều lệnh thi hành đồng bộ, lệnh xử lý • Tuy nhiên ta làm việc xử lý • Do cơng việc khơng thể xảy lúc • Thực hệ điều hành window làm điều thủ tục gọi pre emptive multitasking Giới thiệu đa tiến trình • Window lấy luồng vào vài tiến trình cho phép luồng chạy khoảng thời gian ngắn (gọi time slice) Khi thời gian kết thúc, Window lấy quyền điều khiển lại lấy luồng khác lại cấp khoảng thời gian time slice Vì khoảng thời gian ngắn nên ta có cảm tưởng thứ xảy lúc • Khi có nhiều cửa sổ hình, cửa sổ đại diện cho tiến trình khác Người dùng tương tác với cửa sổ đáp ứng Nhưng thực việc đáp ứng xảy vào sau khoảng thời gian time slice luồng đương thời Ứng dụng Windows • Đa tiến trình có nhiều lợi ích ứng dụng windows như: – Mỗi cửa sổ ứng dụng MDI gán cho tiểu trình khác – Nếu phần đồ họa chương trình nhiều thời gian để thực thi, GUI khóa hồn tất việc vẽ lại Tuy nhiên, định tiểu trình riêng cho hàm OnDraw, làm cho ứng dụng phản hồi xảy tình trạng vẽ q lâu – Nhiều tiểu trình thực thi đồng thời có nhiều CPU hệ thống tăng tốc độ thực chương trình – Sự mơ phức tạp thực hiệu việc gán tiểu trình riêng cho thực thể mô – Các kiện quan trọng điều khiển hiệu thơng qua việc phân cho tiểu trình có độ ưu tiên cao Các trạng thái tiến trình: Chu trình tiến trình • Trạng thái tiến trình: – Chưa bắt đầu (Unstarted): • Khi tiến trình khởi tạo • Tiếp tục phương thức Start tiến trình gọi – Bắt đầu (Started): • Duy trì tới lúc xử lý bắt đầu thực – Đang thực thi (Running): • Tiến trình bắt đầu có độ ưu tiên cao vào trạng thái thực thi • Bắt đầu thực thi xử lý gán cho tiến trình • ThreadStart Tiến trình bắt đầu ủy nhiệm hành động cụ thể cho tiến trình – Ngừng (Stopped): • Khi ủy nhiệm kết thúc • Nếu chương trình gọi phương thức Abort tiến trình Các trạng thái tiến trình: Chu trình tiến trình • Trạng thái tiến trình: – Blocked: • Blocked yêu cầu I/O • Unblocked hệ điều hành hồn thành I/O – WaitSleepJoin: • Xảy khi: – Tiến trình gọi Monitor phương thức Wait gặp mã mà khơng thực » Leaves tiến trình khác gọi Pulse – Gọi phương thức Sleep để sleep khoảng thời gian – Hai tiến trình dược kết hợp tiến trình khơng thể thực tiến trình hồn thành • Các tiến trình đợi (Waiting) ngủ (Sleeping) khỏi trạng thái phương thức Interrupt tiến trình gọi Các trạng thái tiến trình: Chu trình tiến trình • Trạng thái tiến trình: – Tạm ngưng (Suspended): • Khi phương thức Suspend gọi • Trở trạng thái bắt đầu (Started) phương thức Resume gọi 10 Quan hệ sản xuất/ tiêu thụ: đệm vịng • Hai tiến trình đồng hóa chia sẻ tài ngun gây chậm trễ • Bộ đệm vòng: – Các đệm thêm vào để ghi đọc – Có thể thực với mảng • Sản xuất tiêu thụ bắt đầu(start) lúc ban đầu • Khi đến cuối mảng, tiến trình trở lại điểm bắt đầu • Khi tiến trình thời nhanh tiến trình khác, sử dụng thêm đệm để tiếp tục thực thi 75 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Fig 14.6: CircularBuffer.cs // Implementing the producer/consumer relationship with a // circular buffer using using using using using using using CircularBuffer.cs System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Threading; // implement the shared integer with synchronization public class HoldIntegerSynchronized { // each array element is a buffer private int[] buffers = { -1, -1, -1 }; Circular buffer // occupiedBufferCount maintains count of occupied buffers private int occupiedBufferCount = 0; // variable that maintain read and write buffer locations private int readLocation = 0, writeLocation = 0; // GUI component to display output private TextBox outputTextBox; // constructor public HoldIntegerSynchronized( TextBox output ) { outputTextBox = output; } How many buffers are occupied Next read location Next write location Create textbox 76 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 // property Buffer public int Buffer Method to read { from buffer get { // lock this object while getting value // from buffers array Get lock lock ( this ) { // if there is no data to read, place invoking // thread in WaitSleepJoin state if ( occupiedBufferCount == ) { outputTextBox.Text += "\r\nAll buffers empty " + Thread.CurrentThread.Name + " waits."; outputTextBox.ScrollToCaret(); CircularBuffer.cs Test if any buffers occupied If no buffers occupied, consumer must wait Monitor.Wait( this ); } // obtain value at current readLocation, then // add string indicating consumed value to output int readValue = buffers[ readLocation ]; outputTextBox.Text += "\r\n" + Thread.CurrentThread.Name + " reads " + buffers[ readLocation ] + " "; Read value from correct buffer Output value read // just consumed a value, so decrement number of // occupied buffers occupiedBufferCount; Decrement number of buffers occupied 77 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 // update readLocation for future read operation, // then add current state to output readLocation = ( readLocation + ) % buffers.Length; outputTextBox.Text += CreateStateOutput(); outputTextBox.ScrollToCaret(); // return waiting thread (if there is one) // to Started state Monitor.Pulse( this ); return readValue; } // end lock CircularBuffer.cs Update readLocation Call CreateStateOutput Get producer from wait state } // end accessor get Method to write to buffer set { // lock this object while setting value // in buffers array lock ( this ) Get lock { // if there are no empty locations, place invoking // thread in WaitSleepJoin state if ( occupiedBufferCount == buffers.Length ) { outputTextBox.Text += "\r\nAll buffers full " + Thread.CurrentThread.Name + " waits."; outputTextBox.ScrollToCaret(); Monitor.Wait( this ); } Test if all buffers are occupied If all buffers occupied, producer must wait 78 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 // place value in writeLocation of buffers, then // add string indicating produced value to output buffers[ writeLocation ] = value; outputTextBox.Text += "\r\n" + Thread.CurrentThread.Name + " writes " + buffers[ writeLocation ] + " "; CircularBuffer.cs Put new value in next location of buffer Output value written to buffer // just produced a value, so increment number of // occupied buffers ++occupiedBufferCount; Increment number of buffers occupied operation, // update writeLocation for future write // then add current state to output writeLocation = ( writeLocation + ) % buffers.Length; outputTextBox.Text += CreateStateOutput(); outputTextBox.ScrollToCaret(); // return waiting thread (if there is one) // to Started state Monitor.Pulse( this ); } // end lock Update write location Call CreateStateOutput Get consumer from wait state } // end accessor set } // end property Buffer // create state output public string CreateStateOutput() { // display first line of state information string output = "(buffers occupied: " + occupiedBufferCount + ")\r\nbuffers: "; Output number of buffers occupied 79 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 for ( int i = 0; i < buffers.Length; i++ ) output += " " + buffers[ i ] + " "; CircularBuffer.cs output += "\r\n"; // display second line of state information output += " "; Output contents of buffers for ( int i = 0; i < buffers.Length; i++ ) output += " "; output += "\r\n"; // display third line of state information output += " "; // display readLocation (R) and writeLocation (W) // indicators below appropriate buffer locations for ( int i = 0; i < buffers.Length; i++ ) if ( i == writeLocation && writeLocation == readLocation ) output += " WR "; else if ( i == writeLocation ) output += " W "; else if ( i == readLocation ) output += " R "; else output += " "; Output readLocation and writeLocation 80 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 output += "\r\n"; CircularBuffer.cs return output; } } // end class HoldIntegerSynchronized // produce the integers from 11 to 20 and place them in buffer public class Producer Producer class { private HoldIntegerSynchronized sharedLocation; private TextBox outputTextBox; private Random randomSleepTime; // constructor public Producer( HoldIntegerSynchronized shared, Random random, TextBox output ) { sharedLocation = shared; Set shared location outputTextBox = output; Set output randomSleepTime = random; } to buffer Set sleep time // produce values from 11-20 and place them in // sharedLocation's buffer public void Produce() { // sleep for random interval up to 3000 milliseconds // then set sharedLocation's Buffer property for ( int count = 11; count

Ngày đăng: 25/02/2023, 18:04

Xem thêm:

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

TÀI LIỆU LIÊN QUAN

w