I don't understand difference between semaphore and lock - multithreading

I do not understand some things.
For example binary Semaphore and lock are the same?
When using lock and when semaphore,or both?

The difference between a lock and a binary semaphore is only a apparent when there are multiple processes trying to access the same resource. A "process" is defined here as an instance of a program or application that may contain one or more threads.
Both allow only one thread to access a resource at a given time. However, locks can only limit access within a single process while binary semaphores can limit access across multiple processes.
Therefore, within a single process, the behavior of a lock and a binary semaphore are the same. Both allow only one thread to access a resource a given time.
Across multiple processes, the behavior is different. A binary semaphore will allow only one process to access a given resource at a time, but a lock will give multiple processes to access to a resource a time (but only a single thread in each process will have access at a given time).

Related

Unblocking all processes waiting on a semaphore

I have a program that requires several processes access a shared resource. This shared resource does not exist when they all start, so one of them needs to create it. Once the shared resource is created, important infrastructure is installed for use later. There is, however, a possibility that if the "creator" process is scheduled out before it can install the infrastructure in the shared resource, that other processes will try to use the uninitialized data (leading to undefined behavior).
In order to control this, I've created a named semaphore (sem_t *sem_init). Any process that is not the creator "downs" or "waits" on this zero-initialized semaphore. When the creator process has finished setup, it "up's" or "posts" the semaphore, releasing the processes. However, there remains one problem. I do not know exactly how many processes are waiting on it.
In order to solve this problem, I have the following options:
I create a counting semaphore. Each process "up's" or "posts" on this semaphore before blocking on the initialization semaphore. This way, I can know how many processes to release.
I just "post" on the initialization semaphore until it is the maximum allowed value.
I don't like these "solutions" though. For one, I am limited by the maximum size of a semaphore when it comes to the number of processes that I can count. It also seems like "posting" so many times would incur a nasty overhead. My question then, is whether there is any way in which I can instruct a semaphore to release all blocked processes, without me having to do any explicit bookkeeping on my end. I'd also not like to be constrained by the maximum value of a semaphore.
Something like: sem_releaseAll (sem_t *sem_p); would be ideal.
Note: I would greatly prefer a Linux-native solution.

semaphore and mutex locking concept

I read one of the differences between semaphore and mutex is in case of mutex the process/thread (which ever is having the lock) can only release the lock. But in the case of the semaphore any other process can release the semaphore. My doubt arises when a process that does not have the semaphore with it can release the semaphore. What is the use of having a semaphore?
Let's say I have two processes A and B. Assume process A is having a semaphore with it and executing some critical task. Now let us say process B sends a signal to release the semaphore. In this scenario, will process A release the semaphore even if it is executing some critical task?
You are making half-sense. It is not about ownership. Partner-release in semaphores (and mutexes) is usable, for instance, in my favorite interview question of thread ping-pong. As a matter of fact, I have specifically tried to partner-release a mutex on 3 implementations available to me at a time (Linux/Solaris/AIX) and partner-release did work for mutexes as expected - i.e. mutex was successsfully released and threads blocking on it resumed execution. However, this is, of course, prohibited by Posix.
I think you might be confused on the whole set of differences between a semaphore and a mutex. A mutex provides mutual exclusion. A semaphore counts until it reaches a level where it starts excluding. A semaphore that counted to one would give similar semantics to a mutex though.
A good example would be a television set. Only so many people can watch the same television set, so protecting it with a semaphore would make sense. Anyone can stop watching the television. The remote control for the television can only be operated by one person at a time though, so you could protect it with a mutex.
Some reading...
https://en.wikipedia.org/wiki/Mutual_exclusion
https://en.wikipedia.org/wiki/Semaphore_%28programming%29
"Let's say I have two processes A and B. Assume process A is having a semaphore with it and executing some critical task. Now let us say process B sends a signal to release the semaphore. In this scenario, will process A release the semaphore even if it is executing some critical task?"
One key point to note here is the role of OS kernel. Process B can't send a signal to Process A 'to release the semaphore'. What it can do is request the kernel to give it access to the resource. Process A had requested the kernel and the kernel granted it access to the resource.
Now process A, after it finishes its job, will let the kernel know that it is done with the resource and then kernel grants access to B.
"My doubt arises when a process that does not have the semaphore with it can release the semaphore. What is the use of having a semaphore?"
The key difference between a mutex and a semaphore is, a semaphore serializes access to multiple instances of a resource. Mutex does the same when there is one instance of the resource.
A count is maintained by kernel in case of semaphore and mutex is a special case where the count is 1.
Consider the processes as customers waiting in line at a bank.
The use of semaphore is analogous to the case where there are multiple tellers serving the customers. Usage of mutex is analogous to the case where there is just one teller.
Say there are processes A, B and C that need concurrent access to a resource (lock, file or a data structure in memory, etc.). Further suppose there are 2 instances of the resource. So at most two processes can be granted access at a time.
Process A requests access to an instance of the resource following the required semantics. This request to the kernel involves data structures to identify the resource and maximum number of instances as 2. kernel creates the semaphore with a count of 2, grants A access to the resource and decrements the count to 1, because now only one other process can get access.
Now process B requests access to the resource by following the same semantics. Kernel grants it access and decrements the count to 0.
Now process C requests access, but kernel keeps it in waiting state, because count is 0 and no more than 2 processes can get concurrent access.
Process A is done with the resource and lets kernel know. Kernel notices this and grants access to process C that has been waiting.
In case of mutex, kernel grants access to the resource only one process at a time.
A normal binary semaphore is basically used for synchronization. However, the mutex is for exclusive access to a resource. A mutex is a special variant of semaphore that allows only one locker at a time and with more stringency on ownership than a normal semaphore such as the mutex should be released only by the thread that acquired it. Also, please note that in case of pthreads, fast mutex may not check for this error related to ownership, whereas the error checking mutex shall return error.
For the query related to 2 process A and B, the Process A shall intimate via kernel that it is done with its critical work so that the resource can be made available for waiting processes like B.
You could find some related information in this link too :
When should we use mutex and when should we use semaphore
There is no such thing as "having" a semaphore. Semaphores don't have ownership like mutexes do. The code you describe would simply be buggy. Mutexes won't work if your code is buggy either.
Consider the most classic example of a semaphore -- allowing one train at a time on a section of track. You could implement this with a mutex if the train is a thread. The train would lock the track mutex before going on the track and unlock it after leaving the track.
But what if the train itself is multi-threaded? Which thread should own the track?
And what if the signalling devices are the threads, not the train? Here, the signalling device that detects the train entering the track has to lock the track while the signalling device that detects the train leaving the track has to unlock it.
Mutexes are suitable for cases where there is something that is owned by a particular thread for a short period of time. That thread can "own" the mutex. Semaphores are useful for cases where there is no thread to own anything or nothing for the thread to own.

Threads and Processes

I am trying to revise my Operating System concepts, but I had some confusions. I know that a process is a thread with its own address space.
1) Are deadlocks only caused by threads or processes? (Threads share the process's stack, where as different processes have different stacks).
2) Can a single process cause a deadlock? or does it take more than one process for a deadlock to occur?
I am not sure if this is the right place to ask this. If not, please let me know and I will delete the question.
Both threads AND processes can get into deadlocks depending on what they are trying to lock. If the resource that they want to lock is a resource that's shared within a process (e.g. critical section), threads can get into deadlock. On the other hand if it's a resource that's shared globally (e.g. named mutex), processes can get into a deadlock. For 2), there must be more than one process involved since more than one process must try to lock (globally) shared resource in order for a deadlock to occur.
The answer lies in your Question itself. Each Process has a stack and all the threads created by the process share the stack. whenever two threads of the same process request for a resource(data,comm,...) that other threads has a lock to and in-turn waits for a release of other resource then deadlocks occur.
answer:
for 1):
threads cause deadlocks within process and process cause deadlocks within parent process (in most situations OS)
for 2):
yes a single process can cause deadlocks.

Is a lock (threading) atomic?

This may sound like a stupid question, but if one locks a resource in a multi-threaded app, then the operation that happens on the resource, is that done atomically?
I.E.: can the processor be interrupted or can a context switch occur while that resource has a lock on it? If it does, then nothing else can access this resource until it's scheduled back in to finish off it's process. Sounds like an expensive operation.
The processor can very definitely still switch to another thread, yes. Indeed, in most modern computers there can be multiple threads running simultaneously anyway. The locking just makes sure that no other thread can acquire the same lock, so you can make sure that an operation on that resource is atomic in terms of that resource. Code using other resources can operate completely independently.
You should usually lock for short operations wherever possible. You can also choose the granularity of locks... for example, if you have two independent variables in a shared object, you could use two separate locks to protect access to those variables. That will potentially provide better concurrency - but at the same time, more locks means more complexity and more potential for deadlock. There's always a balancing act when it comes to concurrency.
You're exactly right. That's one reason why it's so important to lock for short period of time. However, this isn't as bad as it sounds because no other thread that's waiting on the lock will get scheduled until the thread holding the lock releases it.
Yes, a context switch can definitely occur.
This is exactly why when accessing a shared resource it is important to lock it from another thread as well. When thread A has the lock, thread B cannot access the code locked.
For example if two threads run the following code:
1. lock(l);
2. -- change shared resource S here --
3. unlock(l);
A context switch can occur after step 1, but the other thread cannot hold the lock at that time, and therefore, cannot change the shared resource. If access to the shared resource on one of the threads is done without a lock - bad things can happen!
Regarding the wastefulness, yes, it is a wasteful method. This is why there are methods that try to avoid locks altogether. These methods are called lock-free, and some of them are based on strong locking services such as CAS (Compare-And-Swap) or others.
No, it's not really expensive. There are typically only two possibilities:
1) The system has other things it can do: In this case, the system is still doing useful work with all available cores.
2) The system doesn't have anything else to do: In this case, the thread that holds the lock will be scheduled. A sane system won't leave a core unused while there's a ready-to-run thread that's not scheduled.
So, how can it be expensive? If there's nothing else for the system to do that doesn't require acquiring that lock (or not enough other things to occupy all cores) and the thread holding the lock is not ready-to-run. So that's the case you have to avoid, and the context switch or pre-empt issue doesn't matter (since the thread would be ready-to-run).

Threading/Synchronization

What is the real difference Conceptually and Implementation point of view between
Semophore, Mutex, Monitor?
We say Semophores are resource allocation counter... OK I understand this..
For Mutex we say mutex are Binary Semophore... Gosh what's that???
Monitor guards the entry point for an objects... i.e. only one thread can acquire it and can run one of it's guarded entry points??? Then what are mutex for??
Semaphores are a locking mechanism, iirc they can conceptually be configured to allow multiple aceess to an object; e.g. Access three at a time, four at a time, etc.
Mutex are a special case of a semaphore for ensuring mutual exclusion, I.e. only one can access the protected resource at any given time.
It is important to note that neither the semaphore nor mutex ensure strict ordering when waiting for access to the shared resource. When the resource becomes accessible some waiting thread will gain access but no garuantees are made about which thread that will be. Statistically, eventually all threads will (must) gain access.
A monitor enforces a precedence on the waiting threads/processes by queueing them in a particular order, not necessarily how they arrive. An operating system is an example of a monitor - ensuring a single process has the CPU at any given time.

Resources