Question

In: Computer Science

In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race...

In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race conditions. It might make some sense to just chuck spinlocks out the window and use mutexes. Explain why mutexes would tend to relieve you of some of the problems inherent in spinlocks. Explain a situation in which those benefits might not be worth your time and you’d go with the spinlock anyway.

Solutions

Expert Solution

Mutex forces Mutual Exclusion at a time it allows only one process to hold the lock and enter the critical section. When a task attempts to acquire a mutex that is unavailable, the mutex places the task onto a wait queue and puts it to sleep. As the mutex allows the process to sleep, thus used in the user-space application.

The processor is then free to execute other code. When the mutex becomes available, one of the tasks from the wait queue is awakened so that it can acquire the mutex lock.A spinlock is a mutual exclusion device that can have only two values: “locked” and “unlocked”.

If the lock is available, the “locked” bit is set and the code continues into the critical section.

If instead, the locks been taken by somebody else, the code goes into a tight loop where it repeatedly checks the lock until it becomes available. This looping process is the “spin” part of a spinlock.Spinlock continuously polling for spin waste the CPU time and if the lock is held for more amount of time this will waste a lot more CPU time.Spinlock is not preferable as when the lock is not available and thread put to sleep, it would spin forever no other thread would ever be able to obtain the CPU to release the lock whereas by mutex if the thread was put to sleep, another thread could have run at once, possibly unlocking the lock and then allowing the first thread to continue processing once its wakeup.In cases where the sleep time is large or you potentially need to sleep while holding the lock, the mutex is a solution.Lastly mutex can be held for a long duration of time. where as spinlock can't.
When should we use Spinlock:-
When a thread tries to lock a mutex and it does not succeed, because the mutex is already locked, it will go to sleep, immediately allowing another thread to run. It will continue to sleep until being woken up, which will be the case once the mutex is being unlocked by whatever thread was holding the lock before. When a thread tries to lock a spinlock and it does not succeed, it will continuously re-try locking it, until it finally succeeds; thus it will not allow another thread to take its place .In certain places in kernel (interrupt context, other atomic contexts where you cannot sleep) use spinlocks.


Related Solutions

You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does the standard cp do if you try to copy a file onto itself? $ cp file1 file1 cp: 'file1' and 'file1' are the same file Modify cp1.c to handle the situation and include comments. /** * @file cp1.c * @brief Uses read and write with tunable buffer size * usage: cp1 src dest */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In...
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In this task, a character buffer should be created (e.g., char buf[500]) in the server which is shared among threads that are created to handle communication with clients. Specifically, when the server receives a message (a string) from a client, the server should (1) store the message in the buffer, (2) broadcast the message to all connected clients, and (3) clear the buffer. Make sure...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Using C programming make one for loop Description For this problem you will be figuring out...
Using C programming make one for loop Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given We...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session. The server should support multiple clients (use telnet as the client in this task). Compile and run the server program. Try and connect to it from multiple other hosts using telnet as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT