The Distributed Computation Barrier (Without using barrier v…

Written by Anonymous on March 25, 2026 in Uncategorized with no comments.

Questions

The Distributed Cоmputаtiоn Bаrrier (Withоut using bаrrier variable) You have to develop a parallel processing engine. You have N worker threads performing a "Partial Sum" calculation. No thread can move to the final "Aggregation" phase until all N threads have finished their partial calculation. Requirements: Create N threads. Each thread must simulate a workload (e.g., printf("Thread %d: Finished partial sumn")). Implement a Barrier using POSIX Semaphores and a Mutex. The N-th thread to arrive at the barrier must be the one to "wake up" all other N-1 threads. Once the barrier is released, every thread must print: Thread %d: Proceeding to Aggregation. Goal: Use a shared counter to track how many threads have arrived. Constraint: Do not use the built-in pthread_barrier_t. You must implement the logic yourself using sem_t and pthread_mutex_t. Compile command: gcc -pthread barrier_exam.c -o barrier_exam -lpthread I have attached a skeleton code for you to start with Hints  The Counter: You need a shared integer count initialized to 0. The Semaphore: Use a semaphore initialized to 0. The first N-1 threads will call sem_wait, and the last thread will call sem_post exactly N-1 times  The Mutex: Use a mutex to protect the increment of the count variable. Important to note that the order of the thread does not matter, who gets to run the thread function first, but the count matters. The thread that reaches last should unblock others. barrier_exam.c Rubric Mutex Usage (20%) Conditional Logic (30%) Semaphore Signaling (30%) Deadlock Avoidance (20%) Here is a sample output that looks like after a successful run with 5 threads Thread 0: Finished partial sum.Thread 2: Finished partial sum.Thread 1: Finished partial sum.Thread 4: Finished partial sum.Thread 3: Finished partial sum.Thread 3: Last thread arrived. Releasing barrier...Thread 3: Proceeding to Aggregation.Thread 0: Proceeding to Aggregation.Thread 2: Proceeding to Aggregation.Thread 1: Proceeding to Aggregation.Thread 4: Proceeding to Aggregation.

Comments are closed.