Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SOLVE THIS PROBLEM USING THE C PROGRAMMING LANGUAGE. In this question, you are asked to create three threads and run them simultaneously with each other.

SOLVE THIS PROBLEM USING THE C PROGRAMMING LANGUAGE.

In this question, you are asked to create three threads and run them simultaneously with each other. 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) Synchronize the threads of the program you wrote above, using semaphores, 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. Write your program using the Linux semaphore library (#include ). It is up to you to determine which functions from the library you should use.

WRITE A AND B IN PROGRAMMING LANGUAGE C.

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

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

state what is meant by the term performance management

Answered: 1 week ago