Documentos de Académico
Documentos de Profesional
Documentos de Cultura
PROPRIETARY MATERIAL. © 2009 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or
distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators
permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
1
@ McGraw-Hill Education
3
@ McGraw-Hill Education
4
@ McGraw-Hill Education
Process 1
……………………………….
00401028:
mov eax,dword ptr [ebp-4]
and eax,0FFh
test eax,eax
jne main+26h (00401036)
jmp main+18h (00401028) Process 2
Context Switch ……………………………….
00401036: 00401028:
mov byte ptr [ebp-4],1 mov eax,dword ptr [ebp-4]
……………………………….. and eax,0FFh
Context Switch
00401036:
mov byte ptr [ebp-4],1
………………………………..
………………………………..
………………………………..
………………………………..
8
@ McGraw-Hill Education
9
@ McGraw-Hill Education
CMPXCHG dest,src
10
@ McGraw-Hill Education
11
@ McGraw-Hill Education
#include "stdafx.h"
#include <intrin.h>
#include <windows.h>
void child_thread(void)
{
//Inside the child thread of a process
//Check the lock for availability & acquire the lock if available.
//The lock can be set by any other threads
while (_InterlockedCompareExchange (&bFlag, 1, 0) == 1);
//Check the lock for availability & acquire the lock if available.
//The lock can be set by any other threads
while (_InterlockedCompareExchange (&bFlag, 1, 0) == 1);
004113DE mov ecx,1
004113E3 mov edx,offset bFlag (417178h)
004113E8 xor eax,eax
004113EA lock cmpxchg dword ptr [edx],ecx
004113EE cmp eax,1
004113F1 jne child_thread+35h (4113F5h)
004113F3 jmp child_thread+1Eh (4113DEh)
//Rest of the source code dealing with shared resource access 14
@ McGraw-Hill Education
The lock based mutual exclusion implementation always checks the state of a lock and
waits till the lock is available. This keeps the processes/threads always busy and forces
the processes/threads to wait for the availability of the lock for proceeding further.
The ‘Busy waiting’ technique can also be visualized as a lock around which the
process/thread spins, checking for its availability
Spin locks are useful in handling scenarios where the processes/threads are likely to be
blocked for a shorter period of time on waiting the lock, as they avoid OS overheads on
context saving and process re-scheduling
Another drawback of Spin lock based synchronization is that if the lock is being held for
a long time by a process and if it is preempted by the OS, the other threads waiting for
this lock may have to spin a longer time for getting it.
The ‘Busy waiting’ mechanism keeps the process/threads always active, performing a
task which is not useful and leads to the wastage of processor time and high power
consumption.
15
@ McGraw-Hill Education
17
@ McGraw-Hill Education
18
@ McGraw-Hill Education
19
@ McGraw-Hill Education
20
@ McGraw-Hill Education
21
@ McGraw-Hill Education