0

次のコードに基づいて、xの最小値と最大値を見つける必要があります

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

ループが順番に実行される場合、xが持つことができる最小値は8であると考えました。また、xが持つことができる最大値は16です。プログラムが最初にループの1つに入り、他のループに切り替えてx=8およびi=4になるまで実行し、最初のループを終了すると、x=16およびi= 5.5。これは正しいです?xが大きくなったり小さくなったりする可能性があるケースを見逃していますか?

4

3 に答える 3

1

非アトミックの場合、最小値は 2 で最大値は 512 であることがわかります。

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.

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.
于 2013-03-15T23:42:02.523 に答える
1

あなたが思いついた答えは正しいです!

于 2013-02-20T19:24:47.280 に答える