Deadlock - Operating Systems | Simply Explained TechPrep https://www.youtube.com/watch?v=y7DOHyBTWps Transkript (automatisch erstellt) 0:00 so what actually is deadlock so deadlock is a situation in an operating system where two or more processes are unable to proceed because each is waiting for 0:08 one of the others to release a resource and that resource could be Memory it could be files it could be printers so let's say we have process one and it is 0:14 waiting to get access to Resource One however resource one has already been assigned to process 2 and process 2 is waiting to get access to Resource 2 0:24 which has already been assigned to process one and so here we have a circular chain and as you can see process one will never never be able to 0:30 access Resource One because that has already been assigned to process two and process two is waiting for resource 2 which has already been assigned to 0:37 process one and therefore we have deadlock a deadlock can only occur if the following four conditions are met simultaneously the first one is mutual 0:45 exclusion and so what does that mean only one process can use a resource at any one time if a process has taken a resource no other process can use it 0:53 until it is released so an analogy is think of a single lane bridge where only one car can pass at a time if car is on the bridge no other car can enter until 1:02 the first car has crossed and left the bridge the second condition is hold and weight and what this means is a process is holding at least one resource and is 1:08 waiting to acquire more resources that are currently being held by other processes an analogy is imagine a dinner table where Bob is holding a fork and he 1:17 needs a knife to e and so he waits for a knife that Mary is using at the same time Mary is holding a knife and is waiting for a spoon Bob is using and so 1:25 both are holding one thing but waiting for another the third condition is no preemption and what is does this mean this means that a resource cannot be 1:32 forcibly taken from a process a process must release its resources voluntarily and an analogy is like being at a library with a rule that says you can't 1:40 take a book from someone else until they're done with it and so if everyone holds on to their books and waits for others to finish without reading theirs 1:47 no one will give up their book and everyone ends up waiting indefinitely the fourth and final condition is circular weight and this means that 1:54 there is a set a circle of processes each waiting for a resource that the next process in the circle holes and so we've already seen this in the first 2:02 example where there's a circular nature of processes that are waiting for a resource being held by another process another key topic is deadlock prevention 2:09 and so prevention focuses on structurally eliminating the possibility of Deadlock by negating at least one of the four necessary conditions so mutual 2:17 exclusion hold and weight no preemption and circular weight an example is lock ordering and so this breaks the circular weight condition and so what is it this 2:26 involves imposing a total ordering of all resource types and requiring that each process requests resources in an increasing order of enumeration and so 2:34 how this is done for example is that if we have resources labeled or one or two or three or four and or n then our process must request these resources in 2:41 order so it must request or one before it requests or two it must request or two before or three or three before or four and or four before or n and this 2:50 ordering prevents circular weights and in the real world this is widely used in database and file systems there is also deadlock avoidance and avoidance is all 2:58 about dynamically examining the the state of resource allocation and ensuring that the system can always reach a state where all processes can 3:05 complete their tasks one example is Banker's algorithm and so this is a strategy used to avoid deadlock by simulating resource allocation for all 3:12 possible sequences and determining if a safe sequence exists and so this is done by before granting a resource the system checks if doing so will leave the system 3:21 in a safe state where all processes can still complete with the resources available Banker's algorithm is more theoretical because its overhead can be 3:28 quite high in systems with many process and resources and it's not commonly used in most operating systems due to its complexity but might be seen in systems 3:35 with highly critical and predictable processes however in reality while theoretical models of Deadlock prevention and avoidance are well 3:41 defined the actual implementation tends to prefer practicality and efficiency and so techniques like lock ordering monitoring and judicious uses of 3:49 timeouts form the backbone of handling deadlocks in most real world applications and to recap the definition deadlock is a situation in an operating 3:57 system where two or more processes are unable to process because each is waiting for one of the others to release a resource the four necessary conditions 4:04 for deadlock include Mutual exclusion so only one process can use a resource at any one time hold and wait a process is holding at least one resource and is 4:12 waiting to acquire more resources that are currently being held by other processes no preemption so a resource cannot be forcibly taken from a process 4:19 a process must release its resources voluntarily and finally circular weight there's a set a circle of processes each waiting for a resource that the next 4:28 process in the circle holds then we have prevention which focus on structurally eliminating the possibility of Deadlock by negating at least one of the four 4:34 necessary conditions and avoidance which dynamically examines the state of resource allocation and ensures that the system can always reach a safe state 4:42 where all processes can complete their tasks in the real world actual implementation tends to prefare practicality and efficiency I hope this 4:49 was a clear explanation of what deadlock is the conditions required for it and how it can be prevented and avoided if you want more in detail Technical 4:55 Solutions make sure to like And subscribe and also don't forget to check out Tech prep. if you want to see the most upto-date technical interview 5:02 questions and solutions thanks for watching and I'll see you in the next one