THE JR PROGRAMMING LANGUAGE phần 3

(a) So sánh thời gian thực thi của chương trình nhân ma trận liên tiếp và song song cho các ma trận kích thước khác nhau. Đó là hiệu quả hơn? (b) Sửa đổi các chương trình song song để nó sử dụng chỉ xử lý N, mỗi trong số đó tính toán một hàng kết quả ma trận C. | 54 Semaphores Each semaphore definition specifies a single semaphore and optionally gives the initial value of the semaphore. A semaphore definition has the following general form sent-id or semjd expr As shown the initialization clause is optional. Its value must be non-negative since semaphores are non-negative. The default initial value of each semaphore is zero. JR uses traditional notation for the two standard semaphore operations P and V. They have the general forms p sem-reference V sem_reference For simple semaphores a sem_reference is just the name of the semaphore . a sem_id. To illustrate one use of semaphores consider the following instance of the standard critical section problem seen in Chapter 5. Suppose N processes share a class variable . a counter. Access to the counter is to be restricted to one process at a time to ensure that it is updated atomically. An outline of a JR solution follows public class cs private static final int N 20 number of processes private static int X 0 shared variable private static sem mutex 1 mutual exlusion for X private static process p int i 0 i N i non-critical section critical section p mutex enter critical section X X 1 v mutex leave critical section non-critical section public static void main String args The mutex semaphore is initialized to 1 so that only a single process at a time can modify x. Processes wait on semaphores in first-come first-served order based on the order in which they execute P operations. Thus waiting processes are treated fairly A process waiting on a semaphore will eventually be able to proceed after it executes a P assuming a sufficient number of Vs are executed on that semaphore. Semaphore Declarations and Operations 55 As mentioned JR supports arrays of semaphores. Because a semaphore in JR is an object the declaration of an array of semaphores follows the style of declarations of arrays of other objects in Java. Here a reference to a semaphore is an operation capability and

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
2    80    2    14-05-2024
207    559    4    14-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.