Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Operating Systems: C Programming in Linux Enviroment: The goal of this question is to write a multithreaded program that simulates a simplified version of token

Operating Systems: C Programming in Linux Enviroment:

The goal of this question is to write a multithreaded program that simulates a simplified version of token ring protocol for communication. The protocol works as follows. In the following figure, only one node can transmit at a time. Let us assume that we have a shared variable Token which is initialized to 1 at the beginning. Before transmitting data, each node first checks the value of the Token, and if it is equal to the node Id, the node transmits data. For instance, after transmitting data, node 1 will change the value of Token to 2.

image text in transcribed

To simulate this protocol, create 4 threads in your program to represent four nodes and assign ID 1, 2, 3, and 4 to them respectively. You can pass the ID as a parameter when you create the threads. Initialize the value of Token to 1.

Each of the threads will try to access the variable Token.

Whenever a thread acquires the variable, it checks whether the Token is equal to its own Id or not.

If it is not equal, it will output Not my turn!, followed by its ownId and then release the variable (e.g., sample output: Not my turn! My Id=X).

If it is equal, the thread will print My turn!, then print its OwnId, increase the Token by 1, and then release the variable (e.g., sample output: My turn! My Id=X). However, after increasing Token by 1, the thread will check if the value is 5 or not. If it is 5, it will reset it to 1 before releasing the variable.

The program should execute until each thread prints My Turn! 10 times. Once a thread prints for 10 times, it terminates. Count the number of times each thread prints Not my Turn! and include that in the report.

You must use condition variable to receive points.

Please make sure to comment the code well so I can understand.

I will give you an upvote, Thank You!

Node 1 Node 4 Node 2 Node 3

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago