CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    how to make wait some processes inside a process using semaphores?

    Hello, i need to write such a code that, three processes will run concurrently, namely all 3 of them should be at the same region at the same time.

    The task looks like something like that:
    Task
    {

    /*********/
    Processes should be here at the same time.
    /*********/

    }

    How can i achieve this using semaphores? Can you provide me some solution or pseudo code, or any guide? I think about that problem long enough, i think, when one process enters the region, it should wait until the other two process enters this region. But i cannot do this operation without causing a deadlock, unfortunately.


    In other words, i'm quoting from : https://www.cs.mtu.edu/~shene/NSF-3/...echniques.html

    Let us look at another example. Suppose a showroom can hold no more than 30 visitors. Each visitor is represented with a thread. Visitors stop by the showroom, stay for a while, exit, come back some time later, and visit the showroom again. How do we make sure that the showroom will always has no more than 30 visitors? This is simple. We just install a counter. When a visitor enters, the counter is increased by one and when a visitor exits, the counter is decreased by one. Once the counter value becomes 30, we close the door until a visitor leaves the showroom. The "door" can be simulated with a semaphore S with initial value 30. Before each visitor can enter the showroom, he must wait on semaphore S, and before each visitor leaves the showroom, he signals semaphore S. In this way, when the semaphore counter reaches zero, the showroom has 30 visitors, and the next visitor will be blocked by semaphore S until a visitor exits and signals S.

    Because of this capability, we refer to this way of using semaphores as countdown and lock.

    What if you do not want all 30 visitors coming and going? Instead, you insist that all visitors enter the room at the same time, and wait until all of them leave before the next batch can come in? This is similar to a movie theater that visitors can only come in before a movie starts? Semaphores cannot solve this problem directly. However, with the help of semaphores, we can implement a synchronization primitive, called barrier, to achieve this goal.


    But how to do that? I need an explanation.

    Thanks in advance.
    Last edited by loves_oi; June 8th, 2015 at 08:23 PM.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: how to make wait some processes inside a process using semaphores?

    create the semaphore with a count of 30
    every one entering tries to lock the semaphore, this will be allowed until the count has been reached if so, the lock request will fail/block.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: how to make wait some processes inside a process using semaphores?

    as to the 2Nd, you need a mutex as well.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured