Concurrency cobegin/coend块的最大值和最小值

Concurrency cobegin/coend块的最大值和最小值,concurrency,operating-system,Concurrency,Operating System,根据以下代码,我需要找到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=1 i=1 科贝金 而(i你得到的答案是正确的!你得到的答案是正确的!这取决于i=i+1和x=x*2是否是原子操作(这意味着i的值在得到和设置

根据以下代码,我需要找到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=1
i=1
科贝金

而(i你得到的答案是正确的!

你得到的答案是正确的!

这取决于
i=i+1
x=x*2
是否是原子操作(这意味着
i
的值在得到和设置之前不会发生任何事情)

如果不是原子的: 最大值: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)
最小值: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)
如果是原子的: 最大值: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)
最小值: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)

这取决于
i=i+1
x=x*2
是否为原子操作(这意味着在获得
i
的值和设置之前不会发生任何事情)

如果不是原子的: 最大值: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)
最小值: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)
如果是原子的: 最大值: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)
最小值: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)

结果表明,在非原子情况下,最小值为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.

结果表明,在非原子情况下,最小值为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.

为什么有两个相同的while循环并排执行?因为它们是并行执行的。我们是否必须为此导入另一个库,请提供任何链接/教程为什么有两个相同的while循环并排执行?因为它们是并行执行的。我们是否必须为此导入另一个库,请提供任何链接/教程我认为如果每一行上的指令都不被认为是原子的,那么更糟糕的事情可能会发生——至少在理论上是如此。@MikyDinescu我记得这是我上过的一门并发课程,所以我不是专家,完全有可能它们总是原子的,但我想我还是要提到它(注意,不管怎么说,答案实际上并没有错)。即使它们不是原子的,也可能是
i+=1
是原子的。我认为如果每一行上的指令都不被认为是原子的,那么更糟糕的事情可能会发生——至少在理论上是这样。@MikyDinescu我记得我上过一个并发课程,所以我不是专家,完全有可能它们总是原子的,但我想我会提到它(请注意,答案实际上并不是错的)。即使它们不是原子的,也可能是
i+=1
是。似乎不可能,你得到解释了吗?我添加了x=2和x=512的解释。似乎不可能,你得到解释了吗?我添加了x=2和x=512的解释。