"mutex"和"mutex"之间有什么区别吗?和“原子操作"? [英] Is there any difference between "mutex" and "atomic operation"?

查看:185
本文介绍了"mutex"和"mutex"之间有什么区别吗?和“原子操作"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习操作系统,我对 mutex atomic operation 这两个概念感到非常困惑.以我的理解,它们是相同的,但是我的OS老师向我们提出了这样一个问题,

假设多处理器操作系统内核跟踪每个用户创建的进程数.此操作系统内核为每个用户维护一个计数器变量,该变量在每次为用户创建新进程时都会增加,并在每次该用户的进程终止时都会减少.此外,该操作系统在提供原子获取和递增以及获取和递减指令的处理器上运行. 操作系统是应该使用原子增量和减量指令来更新计数器,还是应该在由互斥锁保护的关键部分中更新计数器?

此问题表明互斥和原子操作是两件事.有人可以帮我吗?

解决方案

一个 原子 操作是不能细分为更小的部分.这样,它将永远不会中途完成,因此您可以保证始终以一致的状态对其进行观察.例如,现代硬件实现了原子比较和交换操作.

互斥体( 互斥 <的缩写/a>)将其他进程或线程排除在执行同一部分代码之外( 关键部分 ).基本上,它确保最多一个线程正在执行给定的代码部分.互斥锁也称为 锁定 .

在引擎盖下,必须以某种方式使用硬件来实现锁,并且实现必须利用底层硬件的原子性保证.

大多数非平凡的操作都不能被原子化,因此您必须在关键部分执行时使用锁来阻止其他线程运行,否则您必须仔细设计 解决方案

An atomic operation is one that cannot be subdivided into smaller parts. As such, it will never be halfway done, so you can guarantee that it will always be observed in a consistent state. For example, modern hardware implements atomic compare-and-swap operations.

A mutex (short for mutual exclusion) excludes other processes or threads from executing the same section of code (the critical section). Basically, it ensures that at most one thread is executing a given section of code. A mutex is also called a lock.

Underneath the hood, locks must be implemented using hardware somehow, and the implementation must make use of the atomicity guarantees of the underlying hardware.

Most nontrivial operations cannot be made atomic, so you must either use a lock to block other threads from operating while the critical section executes, or else you must carefully design a lock-free algorithm that ensures that all the critical state-changing operations can be safely implemented using atomic operations.

This is a very deep subject, and there is a large body of literature on all these topics. The Wikipedia links I've given are a good starting point, but since you're taking a class on operating systems right now, it might be best for you to ask your professor to provide good resources for learning and understanding this stuff.

这篇关于"mutex"和"mutex"之间有什么区别吗?和“原子操作"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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