Pergunta de entrevista da empresa Everpure

Consider a queue between the two processes indicated below. N is the capacity (maximum length) of the queue; e, f, and b are semaphores. "P" refers to the operation of acquiring (decrementing) a semaphore, and "V" refers to the operation of releasing (incrementing) a semaphore. init() { e = N; f = 0; b = 1; queue = EMPTY; } process1() { for(;;) { P(e); P(b); queue.enqueue(...); V(b); V(f); } } process2() { for(;;) { P(f); P(b); ... = queue.dequeue(); V(b); V(e); } } Which of the following statements is (are) true? (Zero or more may be correct.) a. The purpose of semaphore f is to ensure that dequeue is not executed on an empty queue. b. The purpose of semaphore e is to ensure that deadlock does not occur. c. The purpose of semaphore b is to provide mutual exclusion for queue operations. d. None of the above.

Respostas da entrevista

Sigiloso

23 de out. de 2011

a and c according to me. The mutex ensures we lock the queue, the f semaphore ensures that we do not dequeue the queue before it has an element

5

Sigiloso

20 de abr. de 2014

A, C for sure. Not sure for B.

Sigiloso

26 de out. de 2018

a and c.. semaphore b is providing the mutual exclusion such that either Queue or DQueue operation is done.. e is not avoiding deadlock..it is just ensuring that consumer should read the queue if full.

Sigiloso

13 de jan. de 2015

e makes sure we don't enqueue when queue is full. f makes sure that we don't dequeue when queue is empty. semaphore b just makes sure that only one of them access the queue. There is no deadlock. Deadlock requires more than 1 lock. The only way deadlock might occur if one thread acquires b and dies without releasing. So, A is the answer but I don't think C should be an answer.

1

Sigiloso

5 de jan. de 2014

a, b and c. Semaphore 'e' ensures that you lock the resource (a queue item) before you access it, else you might ran out of queue items and there are still processes that are waiting for it