如何将非原子操作转换为原子 [英] how can I convert non atomic operation to atomic

查看:215
本文介绍了如何将非原子操作转换为原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解原子和非原子操作.关于操作系统以及关于C的知识. 根据Wikipedia页面此处

I am trying to understand atomic and non atomic operations.With respect to Operating System and also with respect to C. As per the wikipedia page here

考虑一个简单的计数器,不同的进程可以递增该计数器.
非原子

Consider a simple counter which different processes can increment.
Non-atomic

天真的,非原子的实现:
读取存储位置中的值;
将值加一;
将新值写回到存储位置.

The naive, non-atomic implementation:
reads the value in the memory location;
adds one to the value;
writes the new value back into the memory location.

现在,假设有两个进程正在运行,以增加一个共享内存位置:
第一个进程读取内存位置中的值;
第一个过程将值加一个;
但在它可以将新值写回到内存位置之前,它已被挂起,并允许第二个进程运行:
第二个进程读取内存位置中的值,该值与第一个进程读取的值相同;
第二个过程将值加一个;
第二个过程将新值写入内存位置.

Now, imagine two processes are running incrementing a single, shared memory location:
the first process reads the value in memory location;
the first process adds one to the value;
but before it can write the new value back to the memory location it is suspended, and the second process is allowed to run:
the second process reads the value in memory location, the same value that the first process read;
the second process adds one to the value;
the second process writes the new value into the memory location.

如何使上述操作成为有气氛的操作. 我对原子操作的理解是,任何不间断执行的事物都是原子的. 例如

How can the above operation be made an atmoic operation. My understanding of atomic operation is that any thing which executes without interruption is atomic. So for example

int b=1000;
  b+=1000;

根据我的理解,这应该是原子操作,因为两条指令都没有间断地执行,但是我从某个人那里得知,在C语言中没有所谓的原子操作,因此以上两条语句都是非原子的. 因此,我想了解的是,编程语言与操作系统在原子性上有什么不同?

Should be an atomic operation as per my understanding because both the instructions executed without an interruption,how ever I learned from some one that in C there is nothing known as atomic operation so above both statements are non atomic. So what I want to understand is what is atomicity is different when it comes to programming languages than the Operating Systems?

推荐答案

int b = 1000;
b+=1000;

在指令级别变成多条语句.至少,准备一个寄存器或内存,分配1000,然后获取该寄存器/内存的内容,将1000加到内容中,然后将新值(2000)重新分配给该寄存器.在没有锁定的情况下,操作系统可以在该操作中的任何时候挂起进程/线程.此外,在多进程系统上,当您进行操作时,其他处理器可以访问该内存(在这种情况下将不是寄存器).

gets turned into multiple statements at the instruction level. At the very least, preparing a register or memory, assigning 1000, then getting the contents of that register/memory, adding 1000 to the contents, and re-assigning the new value (2000) to that register. Without locking, the OS can suspend the process/thread at any point in that operation. In addition, on multiproc systems, a different processor could access that memory (wouldn't be a register in this case) while your operation is in progress.

当您进行锁定时(这是使该原子化的方式),部分是在通知操作系统,暂停该进程/线程是不行的,并且不应由该用户访问该内存.其他过程.

When you take a lock out (which is how you would make this atomic), you are, in part, informing the OS that it is not ok to suspend this process/thread, and that this memory should not be accessed by other processes.

现在,上面的代码可能会被编译器优化为b的内存位置的简单分配2000,但是出于这个答案的目的,我将其忽略.

Now the above code would probably be optimized by the compiler to a simple assignment of 2000 to the memory location for b, but I'm ignoring that for the purposes of this answer.

这篇关于如何将非原子操作转换为原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆