6988

Maximum and minimum value of cobegin/coend block

Question:

Based on the following code, I need to find the <strong>minimum and maximum final values of x</strong>

x=1 i=1 cobegin while (i<4) while (i<4) begin begin x=x*2 x=x*2 i=i+1 i=i+1 end end coend

I figured that the <strong>minimum value x can have is 8</strong>, if the the loops are executed in order. And the <strong>maximum value x can have is 16</strong>, if the program enters one of the loops first, switches to the other loop and execute it until x=8 and i=4, and finishes the first loop, then x=16 and i=5. Is this correct? Am I missing any case where x could be either greater or lower?

Answer1:

The answers you came up with are correct!

Answer2:

It depends on whether i=i+1 and x=x*2 are atomic operations (meaning nothing can happen between the value of i is got and before it's set).

<h2>If not atomic:</h2>

<strong>Maximum:</strong> x = 64

x = 1 i = 1 x = 2 (from 1) x = 4 (from 2) i = 2 (from 1+2) // get i=1 for both x = 8 (from 1) x = 16 (from 2) i = 3 (from 1+2) // get i=2 for both x = 32 (from 1) x = 64 (from 2)

<strong>Minimum:</strong> x = 4

x = 1 i = 1 x = 2 (from 1+2) // get x=1 for both i = 2 (from 1) i = 3 (from 2) x = 4 (from 1) i = 4 (from 2) <h2>If atomic:</h2>

<strong>Maximum:</strong> x = 16

x = 1 i = 1 x = 2 (from 1) x = 4 (from 2) i = 2 (from 1) i = 3 (from 2) x = 8 (from 1) x = 16 (from 2) i = 4 (from 1) i = 5 (from 2)

<strong>Minimum:</strong> x = 8

x = 1 i = 1 x = 2 (from 1) x = 4 (from 2) i = 2 (from 1) i = 3 (from 2) x = 8 (from 1) i = 4 (from 2)

Answer3:

It turns out the minimum was 2 and maximum 512 in the non-atomic case.

For x=2:

Process 2 (right loop) executes the [MOV r1, x] assembly instruction of line x=x*2, then switches to Process 1 (left loop). Process 1 loops until x=16 and i=4, then it exits. Back to process 2, which executes [MUL r1, r1], [MOV x,r1], completing the line x=x*2. It then executes i++, yielding i=5, and exits the loop. The final value of x is 2.

For x=512:

Process 2 executes x=x*2 (x=2) and [MOV r1,i], then switches. Process 1 loops, yielding (x=4,i=2), (x=8,i=3), (x=16,i=4), then switches. Process 2 executes [inc r1] and [MOV i,r1]. Now i=2. Process 2 loops and executes x=x*2 (x=32), then [mov r1,i], and switches with i=2. Process 1 loops, yielding (x=64,i=3), (x=128,i=4), then switches. Process 2 executes [inc r1] and [MOV i,r1]. Now i=3. Process 2 loops and executes x=x*2 (x=256), then [mov r1,i], and switches. Process 1 loops, yielding (x=512,i=4), then switches. Process 2 executes [inc r1] and [MOV i,r1]. Now i=4. Process 1 and 2 exit. x=512 and i=4.

Recommend

  • Inputting a character value into an int array? [duplicate]
  • windows store certification kit - c++ library validation issues
  • better way to communicate between ad hoc wifi windows mobile devices
  • Switch to a remote branch getting detached head [duplicate]
  • How to get the input from a text box on a webpage form
  • I want to bind the click event to the li elements in the xpages type-ahead
  • MS Access 2010: Adding transaction management into a form
  • need to identify dynamic input/textareas when inserting to database
  • How to dynamically generate javascript using ScriptSharp?
  • Is it expensive to create the Thread object or to actually start the thread?
  • How do I prepend to a stream in Bash?
  • How do you keep a running instance for Google App Engine
  • How to get the date of next specified day of week
  • Python cosine function precision [duplicate]
  • how to set variables in a php include file?
  • Memory error in python- how to use more memory
  • Security issues with PHP's Readfile method
  • Differences in dis-assembled C code of GCC and Borland?
  • OOP Javascript - Is “get property” method necessary?
  • onBackPressed() not being executed
  • How to define and use opencv mat of user type
  • Mysterious problem with floating point in LISP - time axis generation
  • one Local Olampyad Questions on Informatic in 2011
  • How to know when stdin is empty if it contains EOF?
  • Breeze - Deleted Items nav properties bug
  • Extracting HTML between tags
  • Algorithm for a smudge tool?
  • Illegal mix of collations for operation for date/time comparison
  • javaw.exe and eclipse startup problems
  • vba code to select only visible cells in specific column except heading
  • When should I choose bucket sort over other sorting algorithms?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Delete MySQLi record without showing the id in the URL
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do you join a server to an Active Directory (domain)?
  • need help with bizarre java.net.HttpURLConnection behavior
  • How does Linux kernel interrupt the application?
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Reading document lines to the user (python)