voidfirst(function<void()> printFirst) { // printFirst() outputs "first". Do not change or remove this line. printFirst(); sem_post(&sem_pthread_2); }
voidsecond(function<void()> printSecond) { sem_wait(&sem_pthread_2); // printSecond() outputs "second". Do not change or remove this line. printSecond(); sem_post(&sem_pthread_3); }
voidthird(function<void()> printThird) { sem_wait(&sem_pthread_3); // printThird() outputs "third". Do not change or remove this line. printThird(); } };
classFoo { Semaphore sem_thread_2,sem_thread_3; publicFoo() { sem_thread_2 = new Semaphore(0); sem_thread_3 = new Semaphore(0); }
publicvoidfirst(Runnable printFirst)throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run(); sem_thread_2.release(); }
publicvoidsecond(Runnable printSecond)throws InterruptedException { sem_thread_2.acquire(); // printSecond.run() outputs "second". Do not change or remove this line. printSecond.run(); sem_thread_3.release(); }
publicvoidthird(Runnable printThird)throws InterruptedException { sem_thread_3.acquire(); // printThird.run() outputs "third". Do not change or remove this line. printThird.run(); } }
deffirst(self, printFirst: 'Callable[[], None]') -> None: # printFirst() outputs "first". Do not change or remove this line. printFirst() self.sem_thread_s2.release()
defsecond(self, printSecond: 'Callable[[], None]') -> None: self.sem_thread_s2.acquire() # printSecond() outputs "second". Do not change or remove this line. printSecond() self.sem_thread_s3.release()
defthird(self, printThird: 'Callable[[], None]') -> None: self.sem_thread_s3.acquire() # printThird() outputs "third". Do not change or remove this line. printThird()
voidfirst(function<void()> printFirst) { // printFirst() outputs "first". Do not change or remove this line. printFirst(); lock_2.unlock(); }
voidsecond(function<void()> printSecond) { lock_2.lock(); // printSecond() outputs "second". Do not change or remove this line. printSecond(); lock_3.unlock(); }
voidthird(function<void()> printThird) { lock_3.lock(); // printThird() outputs "third". Do not change or remove this line. printThird(); } };
deffirst(self, printFirst: 'Callable[[], None]') -> None: # printFirst() outputs "first". Do not change or remove this line. printFirst() self.lock_s2.release()
defsecond(self, printSecond: 'Callable[[], None]') -> None: self.lock_s2.acquire() # printSecond() outputs "second". Do not change or remove this line. printSecond() self.lock_s3.release()
defthird(self, printThird: 'Callable[[], None]') -> None: self.lock_s3.acquire() # printThird() outputs "third". Do not change or remove this line. printThird()
from threading import Semaphore classZeroEvenOdd: def__init__(self, n): self.n = n self.sem_zero = Semaphore(1) self.sem_odd = Semaphore(0) self.sem_even = Semaphore(0) # printNumber(x) outputs "x", where x is an integer. defzero(self, printNumber: 'Callable[[int], None]') -> None: for i in range(0,self.n): self.sem_zero.acquire() printNumber(0) if i%2 == 0: self.sem_odd.release() else: self.sem_even.release() defeven(self, printNumber: 'Callable[[int], None]') -> None: for i in range(2,self.n+1,2): self.sem_even.acquire() printNumber(i) self.sem_zero.release() defodd(self, printNumber: 'Callable[[int], None]') -> None: for i in range(1,self.n+1,2): self.sem_odd.acquire() printNumber(i) self.sem_zero.release()