tailieunhanh - Schaum’s Outline Series OF Principles of Computer Science phần 6

Thừa kế OO lập trình làm cho nó dễ dàng thêm chức năng để phần mềm mà không cần viết lại mã người ta đã viết và thử nghiệm. Giả sử chúng ta muốn thêm sự phân biệt giữa các loại Ô tô. Ferrari có thể đi nhanh hơn nhiều so với một Kia, do đó tăng tốc () phương pháp khác nhau, | CHAP. 6 OPERATING SYSTEMS 105 If lock is not true lock then make lock true and return the value true to the calling program. Otherwise return the value false to the calling program. Some boolean variable called lock is used to lock or unlock the resource. If the resource is not locked at the time of the call the instruction will lock it and return true meaning It s OK to proceed. If the resource is locked at the time of the call the instruction will return false meaning You must wait. If such an instruction is part of the machine instruction set code like this can take advantage of it boolean thisLock while testAndSet thisLock Do nothing Now do whatever needs to be done in isolation Now free the lock thisLock false Carry on This code says While testAndSet returns false testAndSet do nothing. When testAndSet returns true do whatever needs to be done in the critical section. Note that the locking variable is now set true and competing processes using the same testAndSet routine will have to wait. When the critical section is complete indicate that by changing thisLock the locking variable to false. Because the test-and-set instruction requires a busy wait it often is not the best choice of synchronization mechanism. However this approach is quite useful for coordinating threads in an operating system on a multiprocessor machine. The effect of the busy wait is much less onerous when the waiting thread has its own processor and the wait is not likely to be long. There are other variations on the test-and-set instruction idea. A swap or exchange instruction that atomically without interruption exchanges the values of two arguments can be used the same way. Here is pseudocode for a swap instruction void swap boolean a boolean b boolean temp a a b b temp This code says Given two boolean values a and b can each be either true or false exchange the two values using the variable temp for temporary storage. The void means that this routine does not return any value. It simply

TỪ KHÓA LIÊN QUAN