289 views
2 2 votes

Consider the Producer-Consumer problem with a shared buffer of size $N=5$. The system uses three semaphores$: \verb|empty|$ $($initialized to $5 )$, $\verb|full|$ $($initialized to $0 )$, and $\verb|mutex|$ $($initialized to $1)$.

The Producer process executes the following code:

while(true) {
    // Produce item
    Wait(empty);
    Wait(mutex);
    // Add to buffer
    Signal(mutex);
    Signal(full);
}

The Consumer process executes the following code:

while(true) {
    Wait(full);
    Wait(mutex);
    // Remove from buffer
    Signal(mutex);
    Signal(empty);
    // Consume item
}

Suppose the Producer has already added $\mathbf{3}$ items to the buffer. If $\mathbf{2}$ Consumer instances and $\mathbf{1}$ Producer instance now attempt to access the buffer simultaneously, what is the maximum possible value the semaphore $\verb|empty|$ can reach during their execution?

1 Answer

2 2 votes

Buffer size $N=5$.

$3$ items are already in the buffer.

$\verb|full|$ $=3$ (counts items present).

$\verb|empty| = 2$ (counts available slots: $5-3=2$).

$\verb|mutex| =1$.
 

Execution for Maximum $\verb|empty|$ Value: To maximize $\verb|empty|$, we need the Consumers to finish their work before the Producer starts theirs.

Consumer increments empty so it will become $4$ after $2$ consumers.

Producer decrements empty so it will become $3$ after $1$ producer.

Max possible empty during execution $= 4$

Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
249
249 views
GO Classes asked Jan 30
249 views
Three processes $(P_1, P_2, P_3)$ share a common counting semaphore $S$ , which is initialized to $2$. Each process executes the following sequence of operations exactly ...
1 1 vote
1 1 answer
303
303 views
GO Classes asked Jan 30
303 views
Consider a system with three processes $P_1, P_2, P_3$ and three resource types $R_1, R_2, R_3$. Each resource type has one instance. The current allocation state is as f...
1 1 vote
1 1 answer
285
285 views
GO Classes asked Jan 30
285 views
Consider a system with two priority queues for CPU scheduling: Queue $\mathbf{1}$ (High Priority) uses Round Robin (RR) with a time quantum of $\mathbf{2 ~ms}$, and Queue...
2 2 votes
1 1 answer
303
303 views
GO Classes asked Jan 30
303 views
A system uses a $\mathbf{2}$-level paging scheme with a $32$-bit logical address space and a page size of $\mathbf{4}$ KB. Each page table entry (PTE) at both levels is $...