In every computer system there are resources. But what are these resources??
resources can be software or hardware. it can be a printer, it can be a file or even a record in a database. each resource can be only assigned to a process with an exclusive access. means only one process can work on the resource.
resources are two types (preemptable – nonpreemptable) the former is the resource that can be taken away from a process during it’s execusion like a memory. if we assumed we have a 32MB memory, printer and two (32MB) processes(A,B). and A requests the printer and printer assigned to it. But while A begin printing it’s swapped out of memory and B is swapped in. so it begins it’s execusion and requests the printer (which A owns) so B will stay waiting for A to release the printer so it can go on printing. but that will never happen, Only if A completes it’s execusion. so we have to take the memory away from B and give it to A by swapping B out and swapping B in. if the previous example the memory was the preemptable resource.
and to complete the picture let’s give an example about nonpreemptable resource :-
assume we have two processes and 1st one has the CD-Rom and started burning a CD already. in that case you can take away thee CD-Rom from it and give it to another process or you will spoil the CD. so the process has to go on the burning and then release the CD-Rom.
resource life for a process is:-
1- request a resource
2- use a resource
3-release a resource
on deadlocks we care about those nonpreemptable resources.
but before we define what a deadlock is let’s give an example
Assume we have two processes (A,B) and two resources (R1,R2).
Both A and B need both resources to continue executing so A requests access to R1 and has it and B requests access to R2 and has it. so A then tries to have access to R2 and B tries to have access to R1 but both fail, because they are being hold by another process so A will wait for B to release R2 and B will wait for A to release R1. and they will be waiting forever because no one will release a resource without having the other resource.
so deadlock is a circular chain of processes each needs a resource that only the next process in the chain has it.
a deadlock happens only if the next conditions are met :-
1- Mutual Exlusion
- one and only one process can have access to the resource
2- Hold and wait
- a process may hold a resource and wait for another resource to be released
3- no preemption
- a resource can be released only by the process that owns it without forcing it to release it
4- Circular wait
- a closed chain of processes exist that each process needs a resource only the next process has so it can continue executing
enough for now 🙂