answersLogoWhite

0


Best Answer

semaphore is a variable providing mutual exclusion in following manner-

-It consist of two function called wait and signal,

wait()

{

while(semav==0);

semav--;

}

signal()

{

semav++;

}

where semav is semaphore.

we apply wait() and signal() in following manner-

while(true)

{

<entry section>;

wait()

<critical section>

signal()

<Exit section>

}

note that wait() always comes before signal(), a process is not allowed to execute critical section if the semaphore has a value 0 i.e. at most one process can execute critical section at a time.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can semaphore be used to enforce mitual exclusion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between MUTEX and Semaphore?

From wikipedia:"A mutex is a binary semaphore, usually including extra features like ownership or priority inversion protection. The differences between mutexes and semaphores are operating system dependent. Mutexes are meant to be used for mutual exclusion only and binary semaphores are meant to be used for event notification and mutual exclusion."They also have a good example as to the use of a semaphore:"A thread named A needs information from two databases before it can proceed. Access to these databases is controlled by two separate threads B, C. These two threads have a message-processing loop; anybody needing to use one of the databases posts a message into the corresponding thread's message queue. Thread A initializes a semaphore S with init(S,-1). A then posts a data request, including a pointer to the semaphore S, to both B and C. Then A calls P(S), which blocks. The other two threads meanwhile take their time obtaining the information; when each thread finishes obtaining the information, it calls V(S) on the passed semaphore. Only after both threads have completed will the semaphore's value be positive and A be able to continue. A semaphore used in this way is called a 'counting semaphore.'"Basically think of a semaphore as a lock that allows multiple threads to wait in line for the resource to be free. Usually they will block and the semaphore will wake them up when it is their turn.


What is spinlock?

Semaphores are a useful tool for mutual exclusion, but they are not the only such tool provided by the kernel. Instead, most locking is implemented with a mechanism called a spinlock. Unlike semaphores, spinlocks may be used in code that cannot sleep, such as interrupt handlers. When properly used, spinlocks offer higher performance than semaphores in general. They do, however, bring a different set of constraints on their use.


How busy waiting occur?

I copied the answer from other place:while any process is in its critical section , any other process that tries to enter its critical section must loop continously in the entry code.this continue looping is clearly a problem in a real multiprogramming system. where a single CPU is shared among many processes. Busy waiting wastes CPU cycles that could have been used by some process in more productive way. This type of semaphore is also called a spinlock.


What were the earliest windmills used for?

they were used for capsureing windthey were used for capsureing wind


What was irrigation used for?

irrigation was used for watering crops and was occasionally used for drinking.

Related questions

What is Semaphore and state its implementation?

Semaphores are used to provide mutual exclusion and condition synchronization. Locks provide mutual exclusion and have special properties that make them useful in object-oriented programs Implementation: public interface semaphore { //creating the semaphore interface public void semWait(); //method signature public void semSignal(); //method signature } Semaphore lock = new semaphoreImplementation(1); //implementing the interface //non-critical instructions lock.semWait(); //method calling to enter critical region //access data // accessing shared data lock.semSignal(); // method calling to exit critical region //non-critical instructions


What is the difference between MUTEX and Semaphore?

From wikipedia:"A mutex is a binary semaphore, usually including extra features like ownership or priority inversion protection. The differences between mutexes and semaphores are operating system dependent. Mutexes are meant to be used for mutual exclusion only and binary semaphores are meant to be used for event notification and mutual exclusion."They also have a good example as to the use of a semaphore:"A thread named A needs information from two databases before it can proceed. Access to these databases is controlled by two separate threads B, C. These two threads have a message-processing loop; anybody needing to use one of the databases posts a message into the corresponding thread's message queue. Thread A initializes a semaphore S with init(S,-1). A then posts a data request, including a pointer to the semaphore S, to both B and C. Then A calls P(S), which blocks. The other two threads meanwhile take their time obtaining the information; when each thread finishes obtaining the information, it calls V(S) on the passed semaphore. Only after both threads have completed will the semaphore's value be positive and A be able to continue. A semaphore used in this way is called a 'counting semaphore.'"Basically think of a semaphore as a lock that allows multiple threads to wait in line for the resource to be free. Usually they will block and the semaphore will wake them up when it is their turn.


Explain binary semaphore and its uses?

A binary semaphore is a semaphore with an integer value that can range only between 0 and 1. A binary semaphore can be simpler to implement than a counting semaphore, depending on the underlying hardware architecture.To implement it in terms of binary semaphores we need the following data structures: binary-semaphore S1, S2; i n t C; Initially S1 = 1, S2 = 0, and the value of integer C is set to the initial value of the counting semaphore S.The wait operation on the counting semaphore S can be implemented as follows: wait (S1) ; c--; i f (C &lt; 0) { signal(S1) ; wait (S2) ; } signal(S1) The signal operation on the counting semaphore S can be implemented as follows: w a i t (S1) ; C++ ; i f (C &lt;= 0) signal (S2) ; e l s e signal (S1) ;


What items are used to send messages in Semaphore code?

Semaphore is transmitted via flags. One is held in each hand. The positions of the arms determine the letter. Semaphore can also be transmitted via paddles or blades on towers.


Semaphore in operating system?

Semaphore data type used to maneuver access to multiple sources. The sources usually pertain to parallel programming, or multi user networks.


When was semaphore used?

Semaphore was invented in 1648 by Robert Hooke. The first use was by Claude Chappe in 1792 in France. The system conveyed messages from city to city by relay stations or towers.


How do you use semaphore in an easy sentence?

The meaning of semaphore is to give information by using visual signals so when using it in a sentence, it must be used to replace the explanation of the signals. An example of a sentence is the airport grounds crew uses semaphores to guide the pilot of the plane.


What is Semaphore used for?

you use your hand to go up and down on the rod which can get slimmy


When was Semaphore first used?

Semaphore communication was first used in the late 18th century by Claude Chappe in France, around the 1790s. Chappe developed a system of visual signals using large mechanical arms to communicate messages over long distances.


What is mutually exclusive?

It is one of thecharacteristics of deadlock. When semaphores are used or mutual exclusion, the semaphore has an initial value of 1, and P() is called before the critical section, and V() is called after the critical section as shown below : semaphore-&gt; P(); critical section semaphore-&gt; V(); remainder section let us suppose that one process A is already executing its critical section then it implies that semaphore value at that time is zero. If process B now tries to enter this critical section , it cannot enter the critical section because it will have to wait before semaphore becomes greater than zero. This is possible only when process A executes its signal operation; after executing its critical section.


What do you call the flag used in Morse code in the Philippines?

Morse code is an audio code (or blinking light code), it has nothing to do with flags.When flags are used to send messages, this is called semaphore, so they would be "semaphore flags".See the related links below.


Describe how the swap instruction can be used to provide mutual exclusion that satisfies the bounded-waiting requirement?

Describe how the Swap() instruction can be used to provide mutual exclusion that satisfies the bounded-waiting requirement.