[工学]6 Process Synchronization.pptVIP

  • 2
  • 0
  • 约1.5万字
  • 约 43页
  • 2018-03-28 发布于浙江
  • 举报
[工学]6 Process Synchronization

wangfengyu 2008.9 Wang Fengyu 6. Process Synchronization Course Exercise in Operating System Sep. 26, 2010 Email:wangfengyu@sdu.edu.cn Contents Critical Section Semaphore Monitor Thread Synchronization in Nachos Critical Section Sharing in Concurrent Processes address space shared by concurrent threads in a process shared memory can be arranged and shared by UNIX processes although they do not share address spaces Problems with Shared Variables possible context switch by time-out at end of every instruction Data may become inconsistent if shared variables are updated without proper control. Critical Section example: two threads update a shared variable counter. Thread A Thread B { { . . . . . . counter++; --counter; . . . . . . } } the machine code translated by gcc for MIPS computer Process A Process B . . . . . . lw $2,counter lw $4,counter addu $3,$2,1 addu $5,$4,-1 sw $3,counter sw $5,counter . . . . . . Critical Section With counter=5 initially lw $2,counter addu $3,$2,1 lw $4,counter addu $5,$4,-1 sw $3,counter counter=6 sw $5,counter counter=4 Critical Section Critical Section A high-level language concept and structure to enable one thread to execute the code section while blocking other threads trying to execute it Code section to be executed by at most one process at a time Controlled by entry code for entering exit code after exiting Critical Section Common model of critical section repeat critical section remaining section until false; Critical Section Hardware Solution with Test-and-Set Instruction Test-and-Set instruction: an atomic instruction to do 1. read and return the Boolean value of the lock 2. set the value of the lock to be true Critical Section repeat while Test-and-Set(Lock) do no-op; critical section; Lock = false; remaining section; until false Problem Rely on busy-waiting, which wastes CPU cycles Hard to implement on multi-processor co

文档评论(0)

1亿VIP精品文档

相关文档