answersLogoWhite

0

What is a Mutex?

Updated: 8/16/2019
User Avatar

Wiki User

17y ago

Best Answer

Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of un-shareable resources by pieces of computer code called critical sections. Examples of such resources are fine-grained flags, counters or queues, used to communicate between code that runs concurrently, such as an application and its interrupt handlers. The problem is acute because a thread can be stopped or started at any time. To illustrate: suppose a section of code is mutating a piece of data over several program steps, when another thread, perhaps triggered by some unpredictable event, starts executing. If this second thread reads from the same piece of data, the data, in the process of being overwritten, is in an inconsistent and unpredictable state. If the second thread tries overwriting that data, the ensuing state will probably be unrecoverable. These critical sections of code accessing shared data must therefore be protected, so that other processes which read from or write to the chunk of data are excluded from running. On a uniprocessor system the common way to achieve mutual exclusion is to disable interrupts for the smallest possible number of instructions that will prevent corruption of the shared data structure, the so-called "critical region". This prevents interrupt code from running in the critical region. Beside this hardware supported solution, some software solutions exist that use "busy-wait" to achieve the goal. Examples of these algorithms include: * Dekker's algorithm * Peterson's algorithm * Lamport's bakery algorithm In a computer in which several processors share memory, an indivisible test-and-set of a flag is used in a tight loop to wait until the other processor clears the flag. The test-and-set performs both operations without releasing the memory bus to another processor. When the code leaves the critical region, it clears the flag. This is called a "spinlock" or "busy-wait." Some computers have similar indivisible multiple-operation instructions for manipulating the linked lists used for event queues and other data structures commonly used in operating systems. Most classical mutual exclusion methods attempt to reduce latency and busy-waits by using queuing and context switches. Some claim that benchmarks indicate that these special algorithms waste more time than they save. Many forms of mutual exclusion have side-effects. For example, classic semaphores permit deadlocks, in which one process gets a semaphore, another process gets a second semaphore, and then both wait forever for the other semaphore to be released. Other common side-effects include starvation, in which a process never gets sufficient resources to run to completion, priority inversion in which a higher priority thread waits for a lower-priority thread, and "high latency" in which response to interrupts is not prompt. Much research is aimed at eliminating the above effects, such as by guaranteeing non-blocking progress. No perfect scheme is known.

User Avatar

Wiki User

17y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a Mutex?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the role of mutex in IPC?

contct to damit for this.


What is priority inversion?

Priority inversion is a situation where in lower priority tasks will run blocking higher priority tasks waiting for resource (mutex). For ex: consider 3 tasks. A, B and C, A being highest priority task and C is lowest. Look at sequence of context swaps A goes for I/O . unlocks mutex. C was ready to run. So C starts running. locks mutex B is ready to run. Swaps out C and takes mutex. A is ready to run. but A is blocked as mutex is locked by B. but B will never relinqishes the mutex as its higher priority than C. The solution to priority inversion is Priority inheritance.


Can you explain for what is mutex used?

The uses of a mutex can indeed be described, however some of the more notable uses are that of cleaning and disinfecting area so that germs can not be spread from area to area.


What is TVE?

TVE stands for Traffic verification entitlement. this is a sub domain of mutex exchange protocol of SPPVTGs assertion suite


How to write c programme of Sleeping-barber problem in opertating system?

#define CHAIRS 5 /* # chairs for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0; /* # of customers waiting for service */ semaphore barbers = 0; /* # of barbers waiting for customers */ semaphore mutex = 1; /* for mutual exclusion */ int waiting = 0; /* customer are waiting (not being cut) */ void barber(void) { while (TRUE) { down(&customers); /* go to sleep if # of customers is 0 */ down(&mutex); /* acquire access to "waiting' */ waiting = waiting - 1; /* decrement count of waiting customers */ up(&barbers); /* one barber is now ready to cut hair */ up(&mutex); /* release 'waiting' */ cut_hair(); /* cut hair (outside critical region */ } } void customer(void) { down(&mutex); /* enter critical region */ if (waiting < CHAIRS) { /* if there are no free chairs, leave */ waiting = waiting + 1; /* increment count of waiting customers */ up(&customers); /* wake up barber if necessary */ up(&mutex); /* release access to 'waiting' */ down(&barbers); /* go to sleep if # of free barbers is 0 */ get_haircut(); /* be seated and be served */ } else { up(&mutex); /* shop is full; do not wait */ } }



What about multithreading in c sharp?

Multi-threading in c sharp is a system with different tutorial. Like: interaction between threads, producer, using thread pool and using mutex objects.


Protecting a dedicated function in Visual C plus plus from interruption?

You can only protect a function from interruption by another thread in the same process -- you cannot prevent the operating system from interrupting your program, nor threads in a separate process. To prevent interruption by another thread, use a mutex. The thread that holds the mutex blocks all other threads from holding that same mutex -- they must wait for it to be released by the holder. However, mutexes should only be used when two or more threads need to share information that must be written to by one or more of the threads. They cannot all write simultaneously and you cannot read information that is currently being written. So each thread must obtain the mutex immediately before performing a read or write, and must release it when the read or write is complete. In this way, only one thread has access to the information at any given moment, and all other threads are blocked. This does not prevent interruption, however. You can reduce the chances of interruption simply by raising your thread's priority. Level 0 is the highest priority but is reserved for the operating system, but there are 31 levels available (1 to 31). Use this sparingly and only when absolutely required.


How do you write Multithreaded applications using C plus plus?

Use std::packaged_task (preferably) or std::thread to start a new thread. Use std::future (preferred), std::mutex or std::atomic to share information between threads.


What is a semaphore in RTOS?

A semiphore is a means of protecting a resource/data shared between threads. It is a token based mechanism for controlling when a thread can have access to the resource/data.Usually a semiphore handle will be able to be received from the system by name/id.See Also Mutex. which is a simplification of the semiphore concept.


What is the use of 'lock' statement?

The lock prefix on the 8086/8088 prevents any other bus master from accessing the bus during this instruction, even if this instruction is a multiple access instruction. It assures atomicity and data consistency in a multi-computer environment, but only for a single instruction. It is generally used to manipulate a mutex or semaphore.


Discuss the class involved in implementation of thread in visual c plus plus?

The thread class (std::thread) was introduced with the C++11 standard. Indeed, C++11 introduced several classes to make it easier to implement concurrency.Consider the following example:#include#include#includevoid foo (char ch, size_t count, std::mutex* cout_mutex){if (!cout_mutex) return;for (size_t i=0; i!=count; ++i){cout_mutex->lock();std::cout unlock();}}void do_it(){std::mutex my_mutex;std::thread t1 (foo, 'A', 30, &my_mutex); // print 30 A'sstd::thread t2 (foo, 'B', 25, &my_mutex); // print 25 B'sstd::thread t3 (foo, 'C', 20, &my_mutex); // print 20 C'sstd::thread t4 (foo, 'D', 15, &my_mutex); // print 15 D's// synchronize threads:t1.join();t2.join();t3.join();t4.join();}int main(){for (size_t i=0; i!=10; ++i){do_it();std::cout


Why is c plus plus standard library needed?

It isn't needed, but it exists to provide common functionality. Without it you would have to re-invent the wheel to provide that common functionality yourself. The standard library includes the standard C library (which includes C-style memory management functions, general utilities and common types such as strings), common container classes (including lists, queues, stacks and vectors), input/output streams (including iostream, fstream, streambuf, etc), thread support (including thread and mutex) and language support (typeinfo, exception, new and limits).