肌酐700多意味着什么| 神经肌电图检查什么| 受精卵着床有什么感觉| 为什么蝙蝠会飞进家里| 铁锚是什么意思| 佳什么意思| 胸有成竹什么意思| 什么是闭口| 天神是什么意思| siemens是什么品牌| 苦瓜和什么不能一起吃| 血块多是什么原因| 只吐不拉是什么原因| 尿蛋白阳性什么意思| 周围型肺ca是什么意思| 发烧拉稀是什么原因| 脚趾麻是什么病的前兆| 肾盂肾炎吃什么药好| 月经来了有血块是什么原因| 麻烦是什么意思| 大腿根疼是什么原因| 高血脂吃什么药效果好| 雍正叫什么| 丝瓜什么人不能吃| 什么动物可以贴在墙上| 糖原是什么| 脑梗吃什么药可以恢复的快| 膀胱尿潴留是什么意思| rush是什么| 育红班是什么意思| 梦见生了个孩子是什么意思| 离婚的女人叫什么| 怀孕抽烟对孩子有什么影响| 蝉吃什么食物| 红酒兑什么好喝| 补肾壮阳吃什么药好| 检查肝肾功能挂什么科| ft是什么| 北海龙王叫什么| 阑尾炎吃什么药效果好| 乳核是什么| 咽喉炎吃什么药管用| 烤乳猪用的是什么猪| 匝道什么意思| 吃桃子对身体有什么好处| 什么饮料解渴| 丝瓜水敷脸有什么作用| 1952属什么生肖| 翘嘴鱼是什么鱼| 医学影像技术是什么| 罗汉果泡水有什么好处| 花椒和麻椒有什么区别| 什么的向日葵| 溥仪为什么没有后代| 加号是什么意思| 低血压吃什么调理| 什么是优质蛋白食物| 一岁半宝宝反复发烧是什么原因| 乌托邦什么意思| 楞严神咒是什么意思| 内科是看什么病的| 什么是远视| 调休是什么意思| 越来越什么| 超五行属什么| 鸡枞是什么| hcg是什么| 铊是什么东西| 三月十五日是什么星座| 三七粉是治什么病的| 白蛋白偏低是什么意思| 止血芳酸又叫什么名| 阑尾疼吃什么药| 月子餐吃什么| 乙肝是什么病| 女人打呼噜是什么原因| 感觉有痰咳不出来是什么原因| 膝盖痛吃什么药| nap是什么意思| fsh是什么意思| 左肺下叶纤维灶是什么意思| 六爻小说讲的什么| 皮肤癣用什么药| 咽干是什么原因造成的| 尿蛋白2加是什么意思| 八月17号是什么星座的| 农历12月是什么月| 唐氏综合征是什么意思| 前列腺按摩什么感觉| 脑供血不足中医叫什么| 喝什么养肝护肝| 维生素b3又叫什么| 出殡什么意思| 减肥每天吃什么三餐| 牙疼吃什么食物好得快| 喘不上来气是什么原因| 梦见花蛇是什么预兆| 50米7秒什么水平| 柠檬加蜂蜜泡水喝有什么功效| 如字五行属什么| 万事如意是什么生肖| 为什么遗精| 窦性心律过速吃什么药| 肛门瘙痒是什么问题| 莲藕是荷花的什么部位| 遮羞布是什么意思| 便秘灌肠用什么水| 什么是ci| 9月10号是什么星座| 男人肝火旺吃什么药| coa什么意思| 名媛是什么意思| 鼻涕倒流到咽喉老吐痰吃什么药能根治| 梦见苍蝇是什么预兆| 梦见孩子拉屎是什么意思| 什么是乐高| 金牛男最烦什么女孩| 子宫糜烂有什么症状| 吃什么养胃又补胃| 万能输血者是什么血型| 低血压吃什么补得最快| 梦见换房子是什么预兆| 我想成为一个什么样的人| 玉和石头有什么区别| 正月初四是什么星座| 钡餐是什么| 对辣椒过敏有什么症状| 秋天有什么景物| 差强人意是什么意思| 飞机用什么燃油| 女人做爱什么感觉| 怪力乱神是什么意思| 儿童水痘吃什么药| 铜绿假单胞菌用什么抗生素| 物流专员是做什么的| 助产学出来是干什么的| msgm是什么品牌| 翊字是什么意思| 二十岁是什么之年| 梦见缝被子是什么意思| hpv会有什么症状| 2001年属什么生肖| 字读什么| 属蛇的贵人是什么属相| 遗精是什么感觉| 正值当年什么意思| 5月25日什么星座| 什么心什么目| 酒精和碘伏有什么区别| 落枕是什么意思| 莲子心泡水喝有什么功效和作用| 什么现象说明奶吸通了| 鲸鱼属于什么类动物| 哺乳期可以喝什么饮料| 蛇胆疮是什么引起的| 胃肠感冒吃什么药| 预防心肌梗塞吃什么药最好| 老想放屁是什么原因| 莫拉古是什么意思| pvc是什么意思| 嗳气吃什么药最有效| 两点是什么时辰| 肌酐高可以吃什么水果| 肝胆胰脾挂什么科| 胰腺炎吃什么消炎药| 化学阉割是什么| 长期打嗝是什么原因| 指甲变形是什么原因| 梦见血是什么意思| 什么是戒断反应| 更年期吃什么药调理| 貌不惊人什么意思| 为什么微信附近的人看不到我| 海苔是什么| 成都人民公园有什么好玩的| 复方北豆根氨酚那敏片是什么药| 大排是什么肉| 心梗是什么原因造成的| 气血亏吃什么补的快| 腿麻是什么原因引起的| 27属什么| 五行中水是什么颜色| 智能眼镜有什么功能| 拉仇恨是什么意思| 痛包是什么| 什么水果含糖量最低| 额头和下巴长痘痘是什么原因| 青钱柳有什么功效与作用| 补体c3偏高说明什么| 全身水肿是什么原因引起的| 高血压吃什么降压药| 人渣是什么意思| 灰色t恤配什么颜色裤子| 熬药用什么锅熬最好| 阳强易举是什么意思| 父亲节该送什么礼物| elle中文叫什么| 吃秋葵有什么好处| 腰痛吃什么药| 宝付支付是什么| 吸渣体质是什么意思| 胸痛是什么原因导致的| 脚底发烫是什么原因| 夏天哈尔滨有什么好玩的地方| 广西为什么简称桂| 为什么不建议儿童做胃镜| 百步穿杨是什么意思| 长颈鹿代表什么生肖| 黄连泡水喝有什么功效| 阴阳八卦是什么生肖| 圣诞节送女生什么礼物好| 珩是什么意思| 什么手什么足| 子不孝父之过下一句是什么| 狗狗感冒吃什么药| 什么时候会怀孕| 体脂是什么意思| 风湿性关节炎用什么药效果好| 双插头是什么意思| 听什么歌写作业快| 对付是什么意思| 龙的九个儿子都叫什么名字| 人间炼狱是什么意思| 小腿骨头疼是什么原因| 哑巴是什么生肖| 白细胞低吃什么补得快| 集锦是什么意思| 房子风水主要看什么| 截疟是什么意思| 左脚麻是什么原因| 蛋糕用什么奶油好| hpv阳性是什么病| 尿路感染吃什么| 劲酒是什么酒| 什么的火焰| 重金属是什么| 皮脂腺囊肿用什么药膏| 陶弘景有什么之称| 赵丽颖的真名叫什么| 女性更年期挂什么科| 甘油三酯查什么项目| 鸡蛋炒什么好吃| 宋朝后面是什么朝代| 梦到喝酒是什么意思| 高血糖挂什么科室的号| 女人脸肿是什么原因引起的| 为什么叫香港脚| 山楂泡水有什么好处| 什么得什么造句| 老花眼有什么症状| 下午14点是什么时辰| 向日葵代表什么象征意义| 胆的作用和功能是什么| 眼皮黑是什么原因| 什么叫造影| 海带和什么不能一起吃| 醪糟是什么| 义字少一点念什么| 什么是浅表性胃炎| 真五行属什么| 纵隔是什么意思| 青少年额头长痘痘是什么原因| 喉癌是什么原因引起的| 吃杏仁有什么好处| 百度Jump to content

【见义勇为】王亚静:危急时刻勇救路人于尖刀之下

From Wikipedia, the free encyclopedia
(Redirected from Synchronization primitive)
百度 截至25日下午,共回收有效问卷1357份。

In computer science, synchronization is the task of coordinating multiple processes to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action.

Motivation

[edit]

The need for synchronization does not arise merely in multi-processor systems but for any kind of concurrent processes; even in single processor systems. Mentioned below are some of the main needs for synchronization:

Forks and Joins: When a job arrives at a fork point, it is split into N sub-jobs which are then serviced by n tasks. After being serviced, each sub-job waits until all other sub-jobs are done processing. Then, they are joined again and leave the system. Thus, parallel programming requires synchronization as all the parallel processes wait for several other processes to occur.

Producer-Consumer: In a producer-consumer relationship, the consumer process is dependent on the producer process until the necessary data has been produced.

Exclusive use resources: When multiple processes are dependent on a resource and they need to access it at the same time, the operating system needs to ensure that only one processor accesses it at a given point in time. This reduces concurrency.

Requirements

[edit]
Figure 1: Three processes accessing a shared resource (critical section) simultaneously.

Thread synchronization is defined as a mechanism which ensures that two or more concurrent processes or threads do not simultaneously execute some particular program segment known as critical section. Processes' access to critical section is controlled by using synchronization techniques. When one thread starts executing the critical section (serialized segment of the program) the other thread should wait until the first thread finishes. If proper synchronization techniques[1] are not applied, it may cause a race condition where the values of variables may be unpredictable and vary depending on the timings of context switches of the processes or threads.

For example, suppose that there are three processes, namely 1, 2, and 3. All three of them are concurrently executing, and they need to share a common resource (critical section) as shown in Figure 1. Synchronization should be used here to avoid any conflicts for accessing this shared resource. Hence, when Process 1 and 2 both try to access that resource, it should be assigned to only one process at a time. If it is assigned to Process 1, the other process (Process 2) needs to wait until Process 1 frees that resource (as shown in Figure 2).

Figure 2: A process accessing a shared resource if available, based on some synchronization technique.

Another synchronization requirement which needs to be considered is the order in which particular processes or threads should be executed. For example, one cannot board a plane before buying a ticket. Similarly, one cannot check e-mails before validating the appropriate credentials (for example, user name and password). In the same way, an ATM will not provide any service until it receives a correct PIN.

Other than mutual exclusion, synchronization also deals with the following:

  • deadlock, which occurs when many processes are waiting for a shared resource (critical section) which is being held by some other process. In this case, the processes just keep waiting and execute no further;
  • starvation, which occurs when a process is waiting to enter the critical section, but other processes monopolize the critical section, and the first process is forced to wait indefinitely;
  • priority inversion, which occurs when a high-priority process is in the critical section, and it is interrupted by a medium-priority process. This violation of priority rules can happen under certain circumstances and may lead to serious consequences in real-time systems;
  • busy waiting, which occurs when a process frequently polls to determine if it has access to a critical section. This frequent polling robs processing time from other processes.

Minimization

[edit]

One of the challenges for exascale algorithm design is to minimize or reduce synchronization. Synchronization takes more time than computation, especially in distributed computing. Reducing synchronization drew attention from computer scientists for decades. Whereas it becomes an increasingly significant problem recently as the gap between the improvement of computing and latency increases. Experiments have shown that (global) communications due to synchronization on distributed computers takes a dominated share in a sparse iterative solver.[2] This problem is receiving increasing attention after the emergence of a new benchmark metric, the High Performance Conjugate Gradient(HPCG),[3] for ranking the top 500 supercomputers.

Problems

[edit]

The following are some classic problems of synchronization:

These problems are used to test nearly every newly proposed synchronization scheme or primitive.

Overhead

[edit]

Synchronization overheads can significantly impact performance in parallel computing environments, where merging data from multiple processes can incur costs substantially higher—often by two or more orders of magnitude—than processing the same data on a single thread, primarily due to the additional overhead of inter-process communication and synchronization mechanisms. [4][5][6]

Hardware synchronization

[edit]

Many systems provide hardware support for critical section code.

A single processor or uniprocessor system could disable interrupts by executing currently running code without preemption, which is very inefficient on multiprocessor systems.[7] "The key ability we require to implement synchronization in a multiprocessor is a set of hardware primitives with the ability to atomically read and modify a memory location. Without such a capability, the cost of building basic synchronization primitives will be too high and will increase as the processor count increases. There are a number of alternative formulations of the basic hardware primitives, all of which provide the ability to atomically read and modify a location, together with some way to tell if the read and write were performed atomically. These hardware primitives are the basic building blocks that are used to build a wide variety of user-level synchronization operations, including things such as locks and barriers. In general, architects do not expect users to employ the basic hardware primitives, but instead expect that the primitives will be used by system programmers to build a synchronization library, a process that is often complex and tricky."[8] Many modern pieces of hardware provide such atomic instructions, two common examples being: test-and-set, which operates on a single memory word, and compare-and-swap, which swaps the contents of two memory words.

Support in programming languages

[edit]

In Java, one way to prevent thread interference and memory consistency errors, is by prefixing a method signature with the synchronized keyword, in which case the lock of the declaring object is used to enforce synchronization. A second way is to wrap a block of code in a synchronized(someObject){...} section, which offers finer-grain control. This forces any thread to acquire the lock of someObject before it can execute the contained block. The lock is automatically released when the thread which acquired the lock leaves this block or enters a waiting state within the block. Any variable updates made by a thread in a synchronized block become visible to other threads when they similarly acquire the lock and execute the block. For either implementation, any object may be used to provide a lock because all Java objects have an intrinsic lock or monitor lock associated with them when instantiated.[9]

Java synchronized blocks, in addition to enabling mutual exclusion and memory consistency, enable signaling—i.e. sending events from threads which have acquired the lock and are executing the code block to those which are waiting for the lock within the block. Java synchronized sections, therefore, combine the functionality of both mutexes and events to ensure synchronization. Such a construct is known as a synchronization monitor.

The .NET Framework also uses synchronization primitives.[10] "Synchronization is designed to be cooperative, demanding that every thread follow the synchronization mechanism before accessing protected resources for consistent results. Locking, signaling, lightweight synchronization types, spinwait and interlocked operations are mechanisms related to synchronization in .NET."[11]

Many programming languages support synchronization and entire specialized languages have been written for embedded application development where strictly deterministic synchronization is paramount.

Implementation

[edit]

Spinlocks

[edit]

Another effective way of implementing synchronization is by using spinlocks. Before accessing any shared resource or piece of code, every processor checks a flag. If the flag is reset, then the processor sets the flag and continues executing the thread. But, if the flag is set (locked), the threads would keep spinning in a loop and keep checking if the flag is set or not. Spinlocks are effective only if the flag is reset for lower cycles; otherwise, it can lead to performance issues as it wastes many processor cycles waiting.[12]

Barriers

[edit]

Barriers are simple to implement and provide good responsiveness. They are based on the concept of implementing wait cycles to provide synchronization. Consider three threads running simultaneously, starting from barrier 1. After time t, thread1 reaches barrier 2 but it still has to wait for thread 2 and 3 to reach barrier2 as it does not have the correct data. Once all the threads reach barrier 2 they all start again. After time t, thread 1 reaches barrier3 but it will have to wait for threads 2 and 3 and the correct data again.

Thus, in barrier synchronization of multiple threads there will always be a few threads that will end up waiting for other threads as in the above example thread 1 keeps waiting for thread 2 and 3. This results in severe degradation of the process performance.[13]

The barrier synchronization wait function for ith thread can be represented as:

(Wbarrier)i=f ((Tbarrier)i, (Rthread)i)

Where Wbarrier is the wait time for a thread, Tbarrier is the number of threads has arrived, and Rthread is the arrival rate of threads.[14]

Experiments show that 34% of the total execution time is spent in waiting for other slower threads.[13]

Semaphores

[edit]

Semaphores are signalling mechanisms which can allow one or more threads/processors to access a section. A Semaphore has a flag which has a certain fixed value associated with it and each time a thread wishes to access the section, it decrements the flag. Similarly, when the thread leaves the section, the flag is incremented. If the flag is zero, the thread cannot access the section and gets blocked if it chooses to wait.

Some semaphores would allow only one thread or process in the code section. Such Semaphores are called binary semaphore and are very similar to Mutex. Here, if the value of semaphore is 1, the thread is allowed to access and if the value is 0, the access is denied.[15]

Distributed transaction

[edit]

In event driven architectures, synchronous transactions can be achieved through using request-response paradigm and it can be implemented in two ways: [16]

  • Creating two separate queues: one for requests and the other for replies. The event producer must wait until it receives the response.
  • Creating one dedicated ephemeral queue for each request.

Mathematical foundations

[edit]

Synchronization was originally a process-based concept whereby a lock could be obtained on an object. Its primary usage was in databases. There are two types of (file) lock; read-only and read–write. Read-only locks may be obtained by many processes or threads. Readers–writer locks are exclusive, as they may only be used by a single process/thread at a time.

Although locks were derived for file databases, data is also shared in memory between processes and threads. Sometimes more than one object (or file) is locked at a time. If they are not locked simultaneously they can overlap, causing a deadlock exception.

Java and Ada only have exclusive locks because they are thread based and rely on the compare-and-swap processor instruction.

An abstract mathematical foundation for synchronization primitives is given by the history monoid. There are also many higher-level theoretical devices, such as process calculi and Petri nets, which can be built on top of the history monoid.

Examples

[edit]

Following are some synchronization examples with respect to different platforms.[17]

In Windows

[edit]

Windows provides:

In Linux

[edit]

Linux provides:

Enabling and disabling of kernel preemption replaced spinlocks on uniprocessor systems. Prior to kernel version 2.6, Linux disabled interrupt to implement short critical sections. Since version 2.6 and later, Linux is fully preemptive.

In Solaris

[edit]

Solaris provides:

In Pthreads

[edit]

Pthreads is a platform-independent API that provides:

  • mutexes;
  • condition variables;
  • readers–writer locks;
  • spinlocks;
  • barriers.

See also

[edit]

References

[edit]
  1. ^ Gramoli, V. (2015). More than you ever wanted to know about synchronization: Synchrobench, measuring the impact of the synchronization on concurrent algorithms (PDF). Proceedings of the 20th ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming. ACM. pp. 1–10.
  2. ^ Shengxin, Zhu and Tongxiang Gu and Xingping Liu (2014). "Minimizing synchronizations in sparse iterative solvers for distributed supercomputers". Computers & Mathematics with Applications. 67 (1): 199–209. doi:10.1016/j.camwa.2013.11.008. hdl:10754/668399.
  3. ^ "HPCG Benchmark".
  4. ^ Silberschatz, Abraham; Galvin, Peter B.; Gagne, Greg (29 July 2008). Operating System Concepts. Wiley. ISBN 978-0470128725.
  5. ^ Computer Organization and Design MIPS Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design). Morgan Kaufmann. 2013. ISBN 978-0124077263.
  6. ^ Parallel Programming: Techniques and Applications Using Networked Workstations and Parallel Computers. Pearson. 2005. ISBN 978-0131405639.
  7. ^ Silberschatz, Abraham; Gagne, Greg; Galvin, Peter Baer (July 11, 2008). "Chapter 6: Process Synchronization". Operating System Concepts (Eighth ed.). John Wiley & Sons. ISBN 978-0-470-12872-5.
  8. ^ Hennessy, John L.; Patterson, David A. (September 30, 2011). "Chapter 5: Thread-Level Parallelism". Computer Architecture: A Quantitative Approach (Fifth ed.). Morgan Kaufmann. ISBN 978-0-123-83872-8.
  9. ^ "Intrinsic Locks and Synchronization". The Java Tutorials. Oracle. Retrieved 10 November 2023.
  10. ^ "Overview of synchronization primitives". Microsoft Learn. Microsoft. September 2022. Retrieved 10 November 2023.
  11. ^ Rouse, Margaret (19 August 2011). "Synchronization". Techopedia. Retrieved 10 November 2023.
  12. ^ Massa, Anthony (2003). Embedded Software Development with ECos. Pearson Education Inc. ISBN 0-13-035473-2.
  13. ^ a b Meng, Jinglei; Chen, Tianzhou; Pan, Ping; Yao, Jun; Wu, Minghui (2014). "A Speculative Mechanism for Barrier Synchronization". 2014 IEEE Intl Conf on High Performance Computing and Communications, 2014 IEEE 6th Intl Symp on Cyberspace Safety and Security, 2014 IEEE 11th Intl Conf on Embedded Software and Syst (HPCC,CSS,ICESS). pp. 858–865. doi:10.1109/HPCC.2014.148. ISBN 978-1-4799-6123-8.
  14. ^ Rahman, Mohammed Mahmudur (2012). "Process synchronization in multiprocessor and multi-core processor". 2012 International Conference on Informatics, Electronics & Vision (ICIEV). pp. 554–559. doi:10.1109/ICIEV.2012.6317471. ISBN 978-1-4673-1154-0. S2CID 8134329.
  15. ^ Li, Yao, Qing, Carolyn (2003). Real-Time Concepts for Embedded Systems. CMP Books. ISBN 978-1578201242.{{cite book}}: CS1 maint: multiple names: authors list (link)
  16. ^ Richards, Mark (2020). Fundamentals of Software Architecture: An Engineering Approach. O'Reilly Media. ISBN 978-1492043454.
  17. ^ Silberschatz, Abraham; Gagne, Greg; Galvin, Peter Baer (December 7, 2012). "Chapter 5: Process Synchronization". Operating System Concepts (Ninth ed.). John Wiley & Sons. ISBN 978-1-118-06333-0.
  18. ^ "What is RCU, Fundamentally? [LWN.net]". lwn.net.
  19. ^ "Adaptive Lock Probes". Oracle Docs.
  20. ^ Mauro, Jim. "Turnstiles and priority inheritance - SunWorld - August 1999". sunsite.uakom.sk.
  • Schneider, Fred B. (1997). On concurrent programming. Springer-Verlag New York, Inc. ISBN 978-0-387-94942-0.
[edit]
属虎是什么命 abob白色药片是什么药 1933年属什么生肖 9月10号是什么星座 什么叫有气质
pd是什么病 凌迟是什么意思 跨性别是什么意思 舌头热灼是什么原因 湿热便秘吃什么中成药
无创什么时候做 羊癫疯有什么症状表现 桃园三结义是什么生肖 九月23日是什么星座 拉烂屎是什么原因
舌头有裂纹是什么原因 皮质醇高是什么原因 家里镜子放在什么位置比较好 黄疸严重会造成什么后果 怀孕可以吃什么
12月25日是什么日子hcv8jop9ns1r.cn 风寒感冒吃什么中成药hcv9jop3ns6r.cn 1和0是什么意思hcv9jop3ns5r.cn 经期血量少是什么原因hcv9jop3ns3r.cn 什么的长江hcv9jop3ns5r.cn
米粉是用什么做出来的96micro.com 世态炎凉是什么意思gysmod.com 1月27日什么星座hcv8jop1ns2r.cn 风口浪尖是什么意思hcv8jop7ns0r.cn 职别是什么意思hcv8jop4ns0r.cn
长脸适合什么眼镜框hcv8jop3ns1r.cn 村支部书记是什么级别hcv8jop0ns1r.cn 萎缩性胃炎什么意思hcv8jop0ns8r.cn 肽是什么意思hcv8jop3ns3r.cn 路人皆知的上一句歇后语是什么hcv8jop6ns0r.cn
补肺养肺吃什么食物最好beikeqingting.com 香油是什么油hcv9jop5ns0r.cn 抠脚大汉什么意思hcv8jop8ns2r.cn 丁克什么意思hcv8jop8ns6r.cn 1996年属鼠五行属什么imcecn.com
百度