Using semaphores in a parallel programming or multi user environment

In summary: Since A acquired 2 permits before B attempted to acquire 2, A is the winner and B is the loser. Deadlock.
  • #1
Eus
94
0
Hi Ho!

I read on http://en.wikipedia.org/wiki/Semaphore_(programming) that

P(Semaphore s, integer howmany)
{
wait until s >= 0;
s := s - howmany; /* must be atomic operation */
wait until s >= 0;
}

Must the whole function of P be atomic or just the "s := s - howmany;" part?
A pointer to a more detailed resource would help also.

Thank you very much.


Eus
 
Technology news on Phys.org
  • #2
As I interpret what they've written there, they just mean that the decrement must be atomic. For one thing, atomic increment/decrement is a widely available synchronization primitive. For another thing, I don't see how it would make sense to say that a "wait" should be "atomic" with respect to some other operation in the first place.
 
  • #3
Okay, I got it. Thanks.

Besides that, it is also written there that "the need of checking and waiting twice for the s >= 0 condition in P is needed to guarantee that as multiple processes are added to the semaphore's waiting list they do not disturb each other's request: a process does not change the semaphore's count until it is next in the queue."

I still don't get why a process will disturb the other if it changes the semaphore's count in its legitimate turn. Could you please give additional information? Or, maybe a pointer to a more detailed explanation?

Thank you very much.


Eus
 
  • #4
It is a process context-switching issue. There is no guarantee about when a process or a thread will get/lose it's turn with the CPU. Atomic means the process will complete a subtraction, guaranteed, once it starts a subtraction. It cannot lose its turn with the CPU in the middle of making the subtraction.

Semaphores and mutexes are traffic cops, making threads or processes take turns one at a time when accessing an object or a critical section. So any change to a mutex has to complete, it cannot be left in an indeterminate state - its value has to be absolutely the same for any process that tests it, not 0 for me and 1 for you.
 
  • #5
It is a process context-switching issue...

Semaphores and mutexes are traffic cops, ...

Yes, to avoid race condition and to enforce a certain order of execution (to perform synchronization).

But, it still does not explain why:
the need of checking and waiting twice for the s >= 0 condition in P is needed to guarantee that as multiple processes are added to the sempahore's waiting list they do not disturb each other's request: a process does not change the sempahore's count until it is next in the queue.

With other words, why doesn't P defined as:
Code:
P(Semaphore s, integer howmany)
{
    s := s - howmany; /* must be atomic operation */
    wait until x >= 0;
}
instead?

It is written there that:
In real implementations it is done without actually activating up the waiting process for the intermediate step.
Do you agree that it says about the new definition of P that I write above?

If you agree, then why even bother putting two waiting codes in the old definition of P and start making a long explanation, which I paste below, about it?
It should be noted that the semaphore count is not necessary equal to the buffers available in the pool. The need of checking and waiting twice for the s >= 0 condition in P is needed to guarantee that as multiple processes are added to the semaphore's waiting list they do not disturb each other's request: a process does not change the semaphore's count until it is next in the queue. In real implementations it is done without actually activating up the waiting process for the intermediate step.

The main thing is in the following sentence that is still greek to me:
... they do not disturb each other's request: a process does not change the semaphore's count until it is next in the queue.
Why "until it is next in the queue"? The scheduler picks a process at random, doesn't it?
In what way a process can change the semaphore's count before it is next in the queue if function P is the only way to change the semaphore's count?
In what way a process disturbs each other's request if the process does change the semaphore's count before it is next in the queue?

Could someone please help?

Thank you.


Eus
 
  • #6
Eus said:
With other words, why doesn't P defined as:
Code:
P(Semaphore s, integer howmany)
{
    s := s - howmany; /* must be atomic operation */
    wait until x >= 0;
}
instead?
This is definitely bad -- it leads to deadlock. Consider the following: (the letters denote threads of execution)

The semaphorse is created with 3 permits.

A acquires 2 permits. The counter is now 1
B attempts to acquire 2 permits. The counter is now -1, and B sleeps.
C attempts to acquire 2 permits. The counter is now -3, and C sleeps.
A returns 2 permuts. The counter is -1.

Note that B and C can now never proceed; the counter is firmly locked at a negative value.


The example you originally gave in this post can demonstrate the same bad behavior -- e.g. if B and C both wake up from the first wait at the same time, and both do the decrement before the second check.


I think the Wikipedia article is implicitly making use of some additional synchronization method in their exposition. I'm going to complain on the discussion page.
 
  • #7
This is definitely bad -- it leads to deadlock.

If suppose the whole function of P is atomic, can a deadlock occur?
Atomic means that once a thread is actively executing P, no other thread can execute P.
But, once a thread is sleeping waiting for its turn or it has reached the end of P, another thread can execute P.
Any objection to this atomic definition of P?

Anyway, even if P is atomic, a deadlock can still occur as follows :wink::

Consider the original code below:
Code:
P(Semaphore s, integer howmany) /*ATOMIC*/
{
    wait until s >= 0; /*first wait*/
    s := s - howmany;
    wait until s >= 0;/*second wait*/
}

Using your example, there will be a deadlock as follows:
The semaphore is created with 3 permits.

A acquires 2 permits. The counter is now 1
B attempts to acquire 2 permits. The counter is now -1, and B sleeps at 2nd wait.
C attempts to acquire 2 permits. C sleeps at 1st wait because the counter is -1.
A returns 2 permits. The counter is 1.
C wakes up to acquire 2 permits. The counter is now -1, and C sleeps at 2nd wait.
DEADLOCK!

I think the Wikipedia article is implicitly making use of some additional synchronization method in their exposition.

I think the article even does not say anything about that.
I cannot find a sentence that makes an allusion to it.

I'm going to complain on the discussion page.

Thank you for your help.


Eus
 

Related to Using semaphores in a parallel programming or multi user environment

1. What are semaphores and how do they work?

Semaphores are a synchronization mechanism used in parallel programming or multi user environments to control access to a shared resource. They work by allowing only one process or thread to access the resource at a time, preventing multiple processes from trying to access it simultaneously.

2. How do semaphores differ from mutexes?

Semaphores and mutexes are both used for synchronization, but they have different properties. While a mutex only allows one thread to access a resource at a time, a semaphore can allow a certain number of threads to access it concurrently. Additionally, mutexes are typically used for locking critical sections of code, while semaphores are often used for controlling access to shared resources.

3. What is the purpose of using semaphores in a parallel programming or multi user environment?

The main purpose of using semaphores is to prevent race conditions, where multiple processes or threads try to access a shared resource at the same time. By using semaphores, we can ensure that only one process is accessing the resource at a given time, avoiding data corruption or inconsistency.

4. Can semaphores be used for inter-process communication?

Yes, semaphores can be used for inter-process communication as they are a form of shared synchronization object that can be accessed by multiple processes. They can be used to coordinate the actions of different processes and ensure that they do not interfere with each other's operations.

5. What are some common issues that can arise when using semaphores?

One common issue is deadlock, where two or more processes are waiting for each other to release a semaphore, resulting in a deadlock. Another issue is starvation, where a process may never acquire the semaphore and therefore never be able to access the shared resource. It is important to carefully design and implement semaphore usage to avoid these issues.

Similar threads

  • Programming and Computer Science
Replies
29
Views
3K
  • Special and General Relativity
3
Replies
75
Views
3K
  • Math Proof Training and Practice
Replies
16
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Beyond the Standard Models
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
10K
  • Math Proof Training and Practice
2
Replies
57
Views
8K
Replies
4
Views
5K
  • Programming and Computer Science
Replies
7
Views
9K
Back
Top