Question

Is the following operation of semaphore correct, please explain. signal (mutex) …. wait (mutex)   Note: wait...

Is the following operation of semaphore correct, please explain.

signal (mutex) …. wait (mutex)  

Note:

wait (S) {

    while (S <= 0)

       ; // busy wait

    S--;

}

signal (S) {

    S++;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Yes, the given operation of the semaphore is correct.

Explanation:

Semaphore:

A semaphore is a simple and special variable and it solves the critical section problem that can be accessed by only two system calls signal() and wait() which are atomic in nature.

The given code is for counting semaphore and it has unrestricted domain.

The translated code using V() is given below:

signal(S)
{
S++;
}

The translated code using P() is given below:

wait(S)
{
while(S<=0);
S--;
}

The wait() operation is the waits operation and it waits until the variable 'S' is less than or equal to 0. The while() loop is terminated by semicolon means it will be executed while the condition is true. If no resource is available the busy-waiting state will occur.

The signal() operation increments the value by one.

Add a comment
Know the answer?
Add Answer to:
Is the following operation of semaphore correct, please explain. signal (mutex) …. wait (mutex)   Note: wait...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Explain whether this code avoids deadlock and starvation. Give detailed reasoning. semaphore fork5-1 semaphore s ##...

    Explain whether this code avoids deadlock and starvation. Give detailed reasoning. semaphore fork5-1 semaphore s ## 1 int i void philosopher(int ai) while(true) f think: wait (s); wait (forki); wait(forkfi +1 mod 5); signal(% eat ): wait(s); signal(fork]); signal(forkli +1] mod 5); signal(s):

  • Let I represent an execution of init(s), W of wait(s), and S of signal(s). Then, for...

    Let I represent an execution of init(s), W of wait(s), and S of signal(s). Then, for example, IWWS represents the sequence of calls init(s), wait(s), wait(s), and signal(s) by some processes in an operating system. For each of the following sequences of calls, state the value of s and the number of processes blocked after the last call in the sequence: (b) IS (c) ISSSW (d) IWWWS (e) ISWWWW Each of the following code fragments contains a bug in the...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • Using the following pseudocode write a c program that solves the dining philosophers problem. #define N...

    Using the following pseudocode write a c program that solves the dining philosophers problem. #define N 5 #define LEFT (i+N-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 1 #define EATING 2 typedef int semaphore; int state[N]; semaphore mutex = 1; semaphore s[N]; void philosopher(int i) { while(TRUE) { think(); take_forks(i); eat(); put_forks(i); } } void take_forks(int i) { down(&mutex); state[i] = HUNGRY; test(i); up(&mutex); down(&s[i]); } void put_forks(i) { down(&mutex); state[i] = THINKING; test(LEFT); test(RIGHT); up(&mutex); } void test(i)...

  • Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive...

    Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive integers from the command line as the amounts to withdraw a. and deposit from an account. b. Creates two threads to perform withdraw and deposit operations repectively. Passes the amount to withdraw/deposit as the parameter to thread's runner function c. Waits for both threads to terminate. Since both threads need to update the account balance, it is necessary to protect sections. You may use...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • What are the three requirements for a solution to the critical-section problem? Consider the following solution...

    What are the three requirements for a solution to the critical-section problem? Consider the following solution to the dining-philosophers' problem. // Global variables. Shared among threads int state [5]; semaphore mutex; I/ Initially set to 1 semaphore s [5] I Initially s[i] is set to 0 for all i Initially statei]-THINKING for all i void philosopher (int i) f int left(int i) f // Philosopher to the left of i // % is the mod operator. while(TRUE) thinkO take.forks ()...

  • pleasw answer as soon as possible . this is for my exam practice . please read...

    pleasw answer as soon as possible . this is for my exam practice . please read the code and answer the question In a system, there are multiple reader processes which read from a shared file and multiple writer processes which update a shared file. The following variables are shared among all processes: int readCounter; semaphore mutex, writeLock; Reader and writer processes are given in the following C++-like pseudo programs: Reader Process // Non-Critical Section P(mutex); Writer Process // Non-Critical...

  • Question We are given the following signal (t), which is also known as "infinite train" of...

    Question We are given the following signal (t), which is also known as "infinite train" of Dirac delta functions. w(t) TITI t(ms) -20 -10 0 10 20 30 Write a mathematical expression for w(t) in the time domain. Find the Fourier coefficients of the signal using the definition and its Fourier transform W(S) [Note that results without correct derivation will get minimal points]. Sketch the signal magnitude spectrum | W (f). Is this signal an energy or a power signal?...

  • need answer for this question please explain in detail. Appreciate it! thank you so much! Consider...

    need answer for this question please explain in detail. Appreciate it! thank you so much! Consider the following three threads and four semaphores: /Initialize x / x = 1; /Initialize semaphores/ s1 = s2 = s3 = s4 = void threadl () void thread2) void thread3 () while (x != 5400) while (x != 5400) while (x != 5400) x=x*2; x=x*3; x=x*5; exit (0)i exit (0) exit (0) Provide initial values for the four semaphores and add PO, VO semaphore...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT