Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this question, you are asked to create three threads and run them simultaneously. Threads, which we will call A, B, and C, have consecutive

In this question, you are asked to create three threads and run them simultaneously. Threads, which we will call A, B, and C, have consecutive code blocks as follows: - Thread A has three code blocks: A1, A2, A3 - Thread B has four code blocks: B1, B2, B3, B4 - Thread C has three code blocks: C1, C2, C3 What we call a code block actually consists of this piece of code: #include #include #include #include void print(const char *data){ int i = 0; for(i;i<5;i++){ // write to STDOUT write(STDOUT_FILENO,data,strlen(data)); sleep (lrand48()%3); } } a. By writing a main program, create 3 threads (A, B, C) in your program. Each thread should show which code block it is currently running by using the print function you wrote above (by sending the function's data consisting of its name and code block number - A1, A2, A3, B1, etc. - as a parameter). Therefore, as can be understood from here, a code block for each thread will consist of writing the relevant block tag five times in a row (A1A1A1A1A1 etc.). Code blocks are ordered sequentially in threads (A2 block does not start until A1 block ends, etc.). When you run your program without applying any synchronization control, you should observe that the code blocks of different threads are nested (like A1A1C1B1A1B1B1..........A3C2C3...).

b. Using semaphores, synchronize the threads of the program you wrote above in such a way that every time you run your program: - Let C1 code block be run AFTER A1 code block. - Let code block A2 be run AFTER code block B1. - Let C2 code block be run AFTER B2 code block. - Let B3 code block be run AFTER A2 code block. - Let A3 code block be run AFTER C2 code block. - Let B4 be the LAST executed code block. It doesn't matter when or in what order code blocks not mentioned above run. You can write your program using the Linux semaphore library (#include ).

In option B, we need to use the semaphore.h library.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions