subject

Implement the following project using: a) cb) javaa barrier is a tool for synchronizing the activity of a number of threads. when a thread reaches abarrier point, it cannot proceed until all other threads have reached this point as well. when the last thread reaches the barrier point, all threads are released and can resume concurrent execution. assume that the barrier is initialized to n —thenumber of threads that must wait at the barrier point: init(n); each thread then performs some work until it reaches the barrier point: /* do some work for awhile */barrier point(); /* do some work for awhile */using synchronization tools described in this chapter, construct a barrier that implements the following api: •int init(int n)—initializesthe barrier to the specified size.•int barrier point(void)—identifiesthe barrier point. all threads are released from the barrier when the last thread reaches this point. the return value of each function is used to identify error conditions. each function will return 0 under normal operation and will return −1 if an error occurs. a testing harness is provided in the source code download to test your implementation of the barriertesting code : javaimport java. io.*; /*** a testing harness for the barrier class.** proper output is that we should see is that all threads* output an 'a' before reaching the barrier and then a 'b'* after proceeding through the barrier. therefore, output* should appear as a series of 'a's followed by an equal count* of 'b's. (there should not be an intermingling of 'a's and 'b's.*/public class testbarrier{ public static final int thread_count = 5; public static void main(string args[]) throws java. io. ioexception { system. out. println("proper output is that we should see is that all threads"); system. out. println("output an 'a' before reaching the barrier and then a 'b'"); system. out. println("after proceeding through the barrier. therefore, output"); system. out. println("should appear as a series of 'a's followed by an equal count"); system. out. println("of 'b's. (there should not be an intermingling of 'a's and 'b's."); system. out. println("\n\npress enter to begin the test: "); (new bufferedreader(new inputstreamreader(system.(); // initialize the barrier to the number of threads // waiting upon the barrier barrier barrier = new barrierimpl(thread_count); thread[] workers = new thread[thread_count]; for (int i = 0; i < workers. length; i++) { workers[i] = new thread(new worker(barrier)); workers[i].start(); } try { for (int i = 0; i < workers. length; i++) workers[i].join(); } catch (interruptedexception ie) { } system. out. println("\n\npress enter to begin the freeall() test: "); (new bufferedreader(new inputstreamreader(system.(); barrier = new barrierimpl(thread_count + 1); workers = new thread[thread_count]; for (int i = 0; i < workers. length; i++) { workers[i] = new thread(new worker(barrier)); workers[i].start(); } try { thread. sleep(3000); } catch (interruptedexception ie) { } barrier. freeall(); }}/*** a worker using the barrier*/class worker implements runnable{ private barrier parta; public worker(barrier parta) { this. parta = parta; } /** * each thread will do some work for awhile, and then * invoke a thread may proceed when all * other threads have arrived at the barrier. */ public void run() { system. out. println("a"); sleeputilities. nap(); parta. waitforothers(); system. out. println("b"); }}testing code : c/*** script for testing the barrier implementation.** usage: * ./testbarrier #include #include "barrier. h"int main(int argc, char *argv[]){ if (argc ! = 2) { fprintf(stderr,"usage: ./testbarrier \n"); return -1; } int i; int number = atoi(argv[1]); pthread_t workers[number]; if ( barrier_init(atoi(argv[1])) ! = 0) return -1; for (i = 0; i < number; i++) { pthread_create(& workers[i], 0, worker, 0); } for (i = 0; i < number; i++) pthread_join(workers[i], 0); return 0; }

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 14:30, leannamat2106
Asoftware program that includes tools for entering, editing, and formatting text and graphics is called a word processing program. true or false?
Answers: 1
image
Computers and Technology, 23.06.2019 17:00, taytay1828
What are the 12 colors of the spectrum called?
Answers: 1
image
Computers and Technology, 25.06.2019 01:30, parislover2000
What controls can be found on the file tab of microsoft word
Answers: 1
image
Computers and Technology, 25.06.2019 02:00, Aaraar
Feedback is one of the basic elements games have in common true false
Answers: 2
You know the right answer?
Implement the following project using: a) cb) javaa barrier is a tool for synchronizing the activity...

Questions in other subjects: