【并发缺陷】data race数据竞争、atomicity violation原子违背、order violation顺序违背

三类均是跟共享变量的内存访问有关的缺陷。
对于并发缺陷的分类目前国内许多是分死锁、数据竞争、原子违背、顺序违背。
在这里插入图片描述或者在并发缺陷中又细分 concurrency vulnerability :死锁和数据竞争。
感觉各个作者有自己的分类方法?

以下引用的中文解释来自<并发缺陷暴露、检测与规避研究综述>哈工大的苏小红老师实验室发表在2015年计算机学报上

目前找到外文文献分为7类。其他四类是我上篇说的死锁(Deadlock)、活锁(LiveLock)、饥饿(Starvation)、挂起(Suspension)

data race数据竞争

race condition竞用条件:

  1. 并发线程在同一个内存地址上操作
  2. 其中至少一个是写操作
  3. 内存没有使用同步机制

注意:满足竞用条件时,不一定产生数据竞争。
Data race occurs when at least two threads access the same memory location , with at least one of them writing data to that location and these accesses are not protected by locks
谷歌翻译: 当至少两个线程访问同一内存位置时发生数据争用,其中至少一个线程将数据写入该位置,并且这些访问不受锁的保护
中文解释: 对某一共享内存单元,存在来自不同线程的两个并发访问,且至少一个为写访问.并发意指这两个访问之间没有happens-before关系,存在“同时”发生的可能.

happens-before:
Chapter 17 of the Java Language Specification defines the happens-before relation on memory operations such as reads and writes of shared variables. The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation. The synchronized and volatile constructs, as well as the Thread.start() and Thread.join() methods, can form happens-before relationships.

atomicity violation原子违背

Atomicity violation occurs when the execution of two code blocks(sequences of statements protected by locks) in one thread is concurrency overlapping with the execution of one or more code blocks of other threads ,in such a way that the resulting content of memory cannot be achieved by executing the involved blocks in any non-overlapping order.
谷歌翻译: 当一个线程中的两个代码块(受锁保护的语句序列)的执行与其他线程的一个或多个代码块的执行并发重叠时,就会发生原子性冲突,以这样一种方式,使得不能通过以任何非重叠的顺序执行代码块来实现所得到的存储器内容。
中文解释: 对某一为保证正确性必须原子性执行的指令序列,存在一个执行交错,其执行效果不与任何该指令序列原子性执行时的执行交错的效果相同.

单变量的四种原子性违背的例子

T1两次读值不一致;T2写的值被覆盖; T1读值不一致 ; T2读到了中间值

在这里插入图片描述
解释:正确顺序L1-L2-L3 判断缓冲区剩余是否够,不够就加,之后写入。错误顺序L1-L3-L2使得缓冲区还未扩容便填充,造成缓冲区溢出。
在这里插入图片描述
解释:指针指向proc_info后,再写入数据,S3造成空指针异常NPD

order violation顺序违背

Order violation occurs when the expected order of at least two memory accesses is not respected, i.e., the programmer’s intended order of execution is not enforced. 两个存储访问的顺序违反
谷歌翻译: 如果不遵守至少两次内存访问的预期顺序,即不执行程序员的预期执行顺序,则会发生顺序冲突。
中文解释: 顺序违背.某一指令(组)没有按照预期,总是在另一指令(组)之前或者之后执行.
消除顺序违背的措施一般是使用条件变量在指令(组)之间形成固定的执行次序.
在这里插入图片描述
解释:thread1初始化io_pending S4必须在S2之后执行
在这里插入图片描述
解释:需要先初始化在操作,造成Use before init缺陷

三者区别

data race order violation atomicity violation
至少一个线程在等待状态
至少一个线程执行状态
所有线程都在执行态
产生错误、或非预期结果
至少一个线程持有锁
不同的线程访共享内存
至少一个内存访问操作是写
内存访问没有同步机制保护
在内存访问中,至少有一个正确的执行顺序没有被保证
序列需要被原子执行
发布了27 篇原创文章 · 获赞 1 · 访问量 681

猜你喜欢

转载自blog.csdn.net/SUKI547/article/details/102742890