Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C code only. implement in C with exact output values of counters, bonus and updates. Expected output: both threads need to report the number of

C code only. implement in C with exact output values of counters, bonus and updates.

Expected output:

both threads need to report the number of updates done at the end of their remainder sections.

both threads need to report the current value of the shared variable counter at the end of their remainder sections.

thread1 needs to report the number of times it got the bonus at the end of its remainder sections.

Im thread1, I did 2000000 updates and I got the bonus for 16089 times, counter = 2554849 Im thread2, I did 2000000 updates, counter = 4000000 from parent counter = 4000000

Assignment description :

Implement a solution to the critical section problem for 2 threads using mutex locks. Specifically, in pthreads using pthread_mutex_trylock and pthread_mutex_unlock.

both two threads need to concurrently increment a shared variable called counter by one 2,000,000 times starting from zero. all global memory is shared among threads of a process. Both threads need to count to 2,000,000 which means the only correct overall count is 4,000,000.

the shared variable count should be defined as follows:

struct shared_data { int value; /* shared variable to store result*/ };

struct shared_data *counter;

remember to allocate memory to your shared data as follows:

counter = (struct shared_data *) malloc(sizeof(struct shared_data));

Thread1 needs to be designed in a way that every time it sees (counter> value%100) == 0 it increments counter> value by 100 (bonus). That counts for 100 individual updates. You also need to keep track of how many times the thread got the bonus and report it in its remainder section.

Thread2 does not get any bonus, it only increments counter->value by 1.

you should prevent each thread from counting to more than 2,000,000.

remember the parent process needs to wait for its 2 threads to join.

in the main body of your code add the following lines :

/* Required to schedule thread independently. Otherwise, use NULL in place of attr. */ pthread_attr_init(&attr[0]); pthread_attr_setscope(&attr[0], PTHREAD_SCOPE_SYSTEM); /* system-wide contention */ /* end to schedule thread independently */

remember to initialize the mutex using pthread_mutex_init .

Make comments indicating the following sections in your code:

entry section

critical section

exit section

remainder section

Expected output:

both threads need to report the number of updates done at the end of their remainder sections.

both threads need to report the current value of the shared variable counter at the end of their remainder sections.

thread1 needs to report the number of times it got the bonus at the end of its remainder sections.

Im thread1, I did 2000000 updates and I got the bonus for 16089 times, counter = 2554849 Im thread2, I did 2000000 updates, counter = 4000000 from parent counter = 4000000

Use the POSIX implementation of threads. You will need to look at the pthread_create, pthread_join and threads manual pages.

IMPORTANT!

No loop should be inside the critical section! When a thread acquires the mutex, it is allowed to do ONLY ONE manipulation of counter->value in the critical section to give the other thread a chance to make progress.

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

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions

Question

Can we invent a drug that will make people tell the truth?

Answered: 1 week ago

Question

The amount of work I am asked to do is reasonable.

Answered: 1 week ago

Question

The company encourages a balance between work and personal life.

Answered: 1 week ago