rogramming Originally, pthreads did not provide a native barrier so it had to be coded manually. Your task is to implement a pthreads barrier without using the provided API call pthread_barrier_wait. For full marks, your barrier should have time complexity O(log p) where p is the number of threads. You should implement and test your barrier in the provided template code in 'p_barrier.c'. You will note that the starting code is identical to that provided in 'p_barrier_native.c' - this is intenional, as it allows you to compare the program output and runtimes to the native version. void* threadFunc(void *arg) { long threadNum = (long)arg; printf("Thread %ld started\n", threadNum); srandom(time(NULL) + threadNum); int nsecs = random() % 5 + 1; /* Sleep for 1 to 5 seconds */ sleep(nsecs); printf("Thread %ld about to wait on barrier after sleeping %d seconds\n", threadNum, nsecs); // ***** YOUR BARRIER GOES HERE ***** printf("Thread %ld passed barrier.\n", threadNum); return NULL; } int main(int argc, char *argv[]) { int numThreads = 10; if (argc > 1) numThreads = atoi(argv[1]); pthread_t *tid = calloc(sizeof(pthread_t), numThreads); for (long threadNum = 0; threadNum < numThreads; threadNum++) { pthread_create(&tid[threadNum], NULL, threadFunc, (void *)threadNum); } /* Wait for all of the threads to terminate */ for (int threadNum = 0; threadNum < numThreads; threadNum++) { pthread_join(tid[threadNum], NULL); } free(tid); return 0; }
rogramming Originally, pthreads did not provide a native barrier so it had to be coded manually. Your task is to implement a pthreads barrier without using the provided API call pthread_barrier_wait. For full marks, your barrier should have time complexity O(log p) where p is the number of threads. You should implement and test your barrier in the provided template code in 'p_barrier.c'. You will note that the starting code is identical to that provided in 'p_barrier_native.c' - this is intenional, as it allows you to compare the program output and runtimes to the native version. void* threadFunc(void *arg) { long threadNum = (long)arg; printf("Thread %ld started\n", threadNum); srandom(time(NULL) + threadNum); int nsecs = random() % 5 + 1; /* Sleep for 1 to 5 seconds */ sleep(nsecs); printf("Thread %ld about to wait on barrier after sleeping %d seconds\n", threadNum, nsecs); // ***** YOUR BARRIER GOES HERE ***** printf("Thread %ld passed barrier.\n", threadNum); return NULL; } int main(int argc, char *argv[]) { int numThreads = 10; if (argc > 1) numThreads = atoi(argv[1]); pthread_t *tid = calloc(sizeof(pthread_t), numThreads); for (long threadNum = 0; threadNum < numThreads; threadNum++) { pthread_create(&tid[threadNum], NULL, threadFunc, (void *)threadNum); } /* Wait for all of the threads to terminate */ for (int threadNum = 0; threadNum < numThreads; threadNum++) { pthread_join(tid[threadNum], NULL); } free(tid); return 0; }
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
programming Originally, pthreads did not provide a native barrier so it had to be coded manually. Your task is to implement a pthreads barrier without using the provided API call pthread_barrier_wait. For full marks, your barrier should have time complexity O(log p) where p is the number of threads. You should implement and test your barrier in the provided template code in 'p_barrier.c'. You will note that the starting code is identical to that provided in 'p_barrier_native.c' - this is intenional, as it allows you to compare the program output and runtimes to the native version.
void* threadFunc(void *arg) {
long threadNum = (long)arg;
printf("Thread %ld started\n", threadNum);
srandom(time(NULL) + threadNum);
int nsecs = random() % 5 + 1; /* Sleep for 1 to 5 seconds */
sleep(nsecs);
printf("Thread %ld about to wait on barrier after sleeping %d seconds\n", threadNum, nsecs);
// ***** YOUR BARRIER GOES HERE *****
printf("Thread %ld passed barrier.\n", threadNum);
return NULL;
}
int main(int argc, char *argv[]) {
int numThreads = 10;
if (argc > 1)
numThreads = atoi(argv[1]);
pthread_t *tid = calloc(sizeof(pthread_t), numThreads);
for (long threadNum = 0; threadNum < numThreads; threadNum++) {
pthread_create(&tid[threadNum], NULL, threadFunc, (void *)threadNum);
}
/* Wait for all of the threads to terminate */
for (int threadNum = 0; threadNum < numThreads; threadNum++) {
pthread_join(tid[threadNum], NULL);
}
free(tid);
return 0;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY