320 views
0 0 votes
1. Consider the following function. The semaphores a, b, c, d are all initialized to one.

sem_t a, b, c, d;

void f (sem_t *s1, sem_t *s2, sem_t *s3, sem_t *s4)

{

          sem_wait(s1); sem_wait(s2); sem_wait(s3); sem_wait(s4);

         Critical section

         sem_post(s1); sem_post(s2); sem_post(s3); sem_post(s4);

 }

Two threads call the function f concurrently. One of these threads invokes f as f(&c, &b, &a, &d). Write down all possible correct invocations of function f by the other thread.

1 Answer

0 0 votes

Let us call the two threads as thread1 and thread2.

Let thread1 has called f(&c, &b, &a, &d).

Now, we see that sem_wait(c); sem_wait(b); sem_wait(a); sem_wait(d); will be called in thread1. So, now if thread2 calls them in another order without first as c - like - sem_wait(b); sem_wait(c); sem_wait(a); sem_wait(d);, then we can see that we might get a deadlock if thread1 executes sem_wait(c) and thread2 executes sem_wait(b);.

So we would need the condition that first semaphore should be c itself.

Thus, now thread2 can call f by 6 different ways - 

  1. f(&c, &b, &a, &d)
  2. f(&c, &b, &d, &a)
  3. f(&c, &d, &a, &b)
  4. f(&c, &d, &b, &a)
  5. f(&c, &a, &b, &d)
  6. f(&c, &a, &d, &b)
Position:
Show:

Related questions

0 0 votes
1 1 answer
261
261 views
RUPALI_SHAHAPURE asked Sep 26, 2024
261 views
Consider a CPU-bound process P with a CPU burst of 100 time units. The process does not execute any system call during its life except the exit() call at the end. The tim...
1 1 vote
1 1 answer
332
332 views
soudipta_dutta asked Sep 19, 2025
332 views
TRUE OR FALSEThere exists a unique feature map $\phi_{\text{lin}}: \mathbb{R}^2 \to \mathbb{R}^D$ for the linear kernel such that \[\langle \phi_{\text{lin}}(\mathbf{x}),...
1 1 vote
1 1 answer
309
309 views
soudipta_dutta asked Sep 17, 2025
309 views
Given three Mercer kernels $ K_1, K_2, K_3 : \mathbb{R} \times \mathbb{R} \to \mathbb{R} $ with their corresponding 2D feature maps:\begin{align*} \phi_1(x) &= (1, x) ...
1 1 vote
0 0 answers
214
214 views
RUPALI_SHAHAPURE asked Nov 15, 2024
214 views
Consider the following multi-threaded program. Discuss with adequate explanation what you expect to see when this program is run.#include <stdio.h>#include <pthread.h>#in...