Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:36:28+00:00 2026-05-26T18:36:28+00:00

Barrier is a synchronization construct where a set of processes synchronizes globally i.e. each

  • 0

Barrier is a synchronization construct where a set of processes synchronizes globally i.e. each process in the set arrives at the barrier and waits for all others to arrive and then all processes leave the barrier. Let the number of processes in the set be three and S be a binary semaphore with the usual P and V functions. Consider the following C implementation of a barrier with line numbers shown on left.

void barrier (void) {    
    1: P(S);
    2: process_arrived++;
    3: V(S);
    4: while (process_arrived !=3);
    5: P(S);
    6: process_left++;
    7: if (process_left==3) 
       {
         8: process_arrived = 0;
         9: process_left = 0;
    10: }
    11: V(S);
 }

The variables process_arrived and process_left are shared among all processes and are initialized to zero. In a concurrent program all the three processes call the barrier function when they need to synchronize globally.

Will the above implementation work? I think it may lead to a deadlock if two barrier invocations are used in immediate succession as first process to enter the barrier doesn’t waits until process_arrived becomes zero before proceeding to execute P(S).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:36:29+00:00Added an answer on May 26, 2026 at 6:36 pm

    Hmm… restricted to three threads and only binary semaphores, I would be tempted to try this using three semaphores, A, B, C. A controls access to the process_arrived count, B and C are for the first and second threads to wait on. A is initialized to 1, B & C to 0. Thread 1 gets A, so preventing 2 & 3 from entering. A switch on process_arrived causes thread 1 to inc process_arrived, release A and wait on B. Thread 2 gets A, and the switch causes it to inc process_arrived, switch and so release A and wait on C. Thread 3 gets A and the switch causes it to signal B, signal C, set process_arrived to 0, signal A and continue on.

    Threads 1 and 2 cannot pass B and C until 3 signals them. When B/C is signaled by 3, 1/2 can run but cannot loop back and get into the barrier until 3 releases A, at which point the barrier is in the correct state to act as a barrier again – A has a count of 1, B and C have zero and process_arrived is zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program, to create a barrier, the main thread sends signals to all
I am looking for an inter-processes barrier implementation. Processes are in shared memory (ie.
I am trying to implement a barrier in Zookeeper. My implementation works all of
respected all; I am new to programming and find a barrier i.e on clicking
I'm creating n threads & then starting then execution after a barrier breakdown. In
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I have a buddy that's having a hard time with the language barrier. I
I'm working on a wordpress plugin, and have come across a barrier of sorts.
See the following code: boost::thread_group threads; boost::barrier barrier(10); thing pThing; for( size_t i =
I need to provide synchronization to some members of a structure. If the structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.