Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

OS30 - Mutex Lock

EZCSE9:53 30.947 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 57 Zeilen
Herunterladen
  1. mutex logs are software solutions to help in synchronization of processes which wish to enter a critical section it is short for mutual exclusion
  2. so we have a lock variable which can take two values 0 or 1. so any process which wishes to enter the critical section
  3. will try to acquire the lock so if the lock is available that means the value of lock is 0 then the process can acquire the lock
  4. that means it can change its value to now 1 and then enter the critical section if the value of lock is 1
  5. then that means the lock is not available and the process cannot acquire it so then in that case it will keep on waiting for the lock value to become
  6. zero after a process has acquired the lock and manipulated the critical section it comes out of the critical section and
  7. can release the lock by making its value back to zero so let's see how the basic definitions of acquire and release can be
  8. implemented so let's say we have this variable lock which is initialized to zero
  9. and we have a process one this is implementing a basic definition of the acquire and a basic definition of the
  10. release lock so any process which is sharing a critical section with the other processes can implement the same acquire and release lock definitions
  11. so this definition says that while lock is not equal to 0 then you keep spinning so this semicolon over here says that if lock is not equal
  12. to 0 the process has to keep on looping into this while loop and keep on waiting till the value of rock becomes available but here we can see that the value of
  13. flock is 0 so that means this process 1 can go out of this while loop and execute the next instruction and assign a value of 1 to the lock
  14. once it has assigned a value of 1 to the lock then it can enter the critical section manipulate the data and after it has come out of the critical section
  15. again it will change the value of lock to 0 which is release of the lock now let's say a process p1 was in the critical section that means it
  16. had acquired the lock it had changed its value to 1 and now it is working in the critical section another process too now also wishes to
  17. enter the critical section so it will execute this instruction while lock not equal to 0 keep on spinning
  18. so here we see that the lock value is not equal to 0 as another process has acquired it and its current value is 1 so process 2 will keep on looping in the
  19. while loop over here only when the value of lock becomes back to 0 after the release of the lock by process 1 then only process 2 will be
  20. able to come out of this while loop and take the value of change the value of lock to 1 that means it will acquire the lock and then only be able to enter the
  21. critical section but with the use of these acquire and release definitions is mutual exclusion
  22. guaranteed do we say that when one process is in the critical section the other process will never be able to enter the critical section let's
  23. see what happens so again here are the definitions of acquire and reduce same for another
  24. process which is sharing or using the same critical section the value of flock is initially zero so let's say first p1 executes the
  25. instruction while lock not equal to 0. so it is executing this instruction and checking whether the value of log is 0 or not
  26. the current value of lock is 0 so that means process p1 comes out of this while loop and now it is almost
  27. about to execute the next instruction but before it can do so there is a context switch over here and p2 starts running so now when process p2 starts
  28. running it will also check the value of lock over here process p1 has not been able to update the value of lock to 1 because it was
  29. preempted there was a context switch the value of lock has not changed so the value of lock is still 0 so when process 2 checks this value it sees that the
  30. value of lock is still 0 and it comes out of the while loop and now it executes the next instruction and let's say the value of lock is now changed to
  31. 1 by process p2 again let's say there is a context switch and p1 starts running p1 was already here at this instruction so p1
  32. also executes this instruction and makes the value of lock equal to 1. so both the processes have changed the value of log 2 1 and both the processes are now
  33. about to enter the critical section so we see that by this way because these instructions this checking of the
  34. lock and modifying it this was not atomic in nature now both the processes have entered the critical section that this means that this definition of
  35. acquire it has to be atomic only then mutual exclusion can be maintained otherwise if these this
  36. acquired definition is not atomic then as we have just seen because of the context switch that takes place both the processes will enter the critical
  37. section so this definition of acquire which has to be atomic in nature can be done so by
  38. the use of hardware instructions at the machine level so if there is an instruction at the machine level which can ensure that this
  39. swapping of the value of the lock will happen atomically then the mutual exclusion can be guaranteed so suppose lock was initialized to 0 now
  40. process 1 says acquire and it sends the address of the lock to this function over here now since the pointer is being sent that
  41. means this l whatever manipulations are being done over here it will directly be able to manipulate the value of the lock over here so while lock is not equal to
  42. 0 spin but lock is equal to 0 over here so this is executed and the contents of the pointer are changed to 1. now
  43. the process p1 enters the critical section now in this time now suppose if process 2 up wants to acquire the lock and now
  44. it sends the address of the lock to the function over here it checks the contents of the lock pointer and it sees that it is not equal
  45. to 0 because the now the value of block is 1 which has been manipulated by process p1 and so process p2 will continue looping
  46. in the while loop till the time the lock value becomes 0. once process p1 is out it will release the lock and make this 0. when process
  47. p2 runs this instruction again and now when the value of lock is 0 now process p2 will be able to acquire the lock and enter the critical section so now we can
  48. see that only if the acquired definition is atomic in nature then mutual exclusion will be guaranteed the limitation of mutex locks is that it
  49. requires busy waiting by one process so if process p1 has acquired the lock process p2 will keep on checking the lock variable and keep on trying to
  50. acquire the log so this will continue spinning in the while loop over here so this causes or wastes the cpu cycle and this busy weighting is also referred
  51. to as spin lock mutex locks are advantages to use if the lock is to be held for a very short duration so if the critical section is
  52. very small that means if only a variable value is being incremented or decremented if a very short small operation is required then mutex locks
  53. are advantages for use because then context switch is not required in that case the other process which is waiting for the
  54. lock can spin for a few cycles till the time the lock is released also here by one process which is spinning on one processing core if we
  55. are having a multi-core system one process can keep on spinning on one processing core while the other thread is using the critical section on another
  56. core so as soon as the process which was using the critical section for a short duration as soon as it finishes the thread was which was spinning can then
  57. acquire the lock

Zum Nachlesen