Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Considering the following code int y = 3; x = 1; void foo () { y = y + 1; x = y; } void

Considering the following code

int y = 3; x = 1;

void foo () {

y = y + 1;

x = y;

}

void bar () {

y = y * 2;

}

main () {

Thread t1 = createThread(foo);

Thread t2 = createThread(bar);

WaitForAllDone();

cout << x << y <

}

Note that function WaitForAllDone blocks the main thread until both threads it creates are done. Assume that memory load and memory store are atomic, but add and time are not atomic. Note,

y = y + 1, consists of three assemby instructions :

Load R1, y

Add R1, 1, R1

Store R1, y

x = y, consists of two assemby instructions :

Load R1, y

Store R1, x

y = y * 2, consists of three assemby instructions :

Load R1, y

Mul R1, 2, R1

Store R1, y

Registers not shared between the threads.

a.) What is the maximum number of threads that are ever alive (this includes those one the ready or waiting lists, or running) in this program?

b.) Give all possible outputs of this program. Explain your answer and be specific.

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions