Permalink
Browse files

added first notes on deadlock

  • Loading branch information...
omarsar committed Jul 26, 2016
1 parent 401649b commit 392365cf7fea03158da226cf702283c53a2052da
Showing with 77 additions and 7 deletions.
  1. +75 −6 5. Critical Section Problem.md
  2. +1 −0 README.md
  3. +1 −1 log.md
@@ -6,16 +6,16 @@
- These are the following requirements that must be met to solve the critical section problem:
+ __Mutual Exclusion (ME)__ - only one process should be given access to the critical section at a time.
+ __Progress__ - Given a set of processes _P = {P1, P2, ...,Pn}_, only one process _P(i)_ should be allowed to enter the __critical region_. The rest of processes must wait until _P(i)_ finishes. The decision about who goes into the critical section can only be taken by the processes that are set to go into the critical section, and must be taken in finite time.
+ __Bounded Waiting__ - When a process _P(i)_ has put a request to enter the critical section at time _T(o)_, it must wait until no process is running in the critical section. It also means that no other process can enter the critical section before the selecting process _P(i)_. It serves as a assurance that its request to enter the critical section is satisfied.
+ __Progress__ - Given a set of processes _P = {P1, P2, ...,Pn}_, only one process _Pi_ should be allowed to enter the __critical region_. The rest of processes must wait until _Pi_ finishes. The decision about who goes into the critical section can only be taken by the processes that are set to go into the critical section, and must be taken in finite time.
+ __Bounded Waiting__ - When a process _Pi_ has put a request to enter the critical section at time _To_, it must wait until no process is running in the critical section. It also means that no other process can enter the critical section before the selecting process _Pi_. It serves as a assurance that its request to enter the critical section is satisfied.
- The __Entry Section__ - decides what process can enter into the critical section. A __lock__ is used to indicate that no other process can enter the critical section.
- The __Exit Section__ - decides when the critical section becomes unlocked and free to accept another process.
- The __Remainder Section__ - this is part of the code that doesn't involve the critical section.
- Given _P(1)_ and _P(2)_ and a shared variable __turn__, which takes value of 1 and 2, the following code inserts a process into the critical section:
- Given _P1_ and _P2_ and a shared variable __turn__, which takes value of 1 and 2, the following code inserts a process into the critical section:
```Pascal
P(i): While turn != i do skip; //execute next line if its a process turn
@@ -25,13 +25,13 @@ P(i): While turn != i do skip; //execute next line if its a process turn
- The previous code satisfies ME, ensuring that no two processes enter the critical section simultaneously.
- If a process _P(1)_ in the critical section terminates and gives control to a Process _P(2)_ that doesn't want to go into the critical section, we won't be able to meet progress. The reason is because _P(2)_ doesn't have an exit statement that indicates to give the critical section to another process _P(1)_
- If a process _P1_ in the critical section terminates and gives control to a Process _P2_ that doesn't want to go into the critical section, we won't be able to meet progress. The reason is because _P2_ doesn't have an exit statement that indicates to give the critical section to another process _P1_
- The following algorithm satisfies the __Progress__ criteria:
```Pascal
flag: array [0..1] of boolean
P(i) While flag[j] do skip; // decides if P(i) can go into the critical section
P(i) While flag[j] do skip; // decides if _Pi_ can go into the critical section
flag[i] = True;
CS
flag[i] = False;
@@ -44,7 +44,7 @@ P(i) While flag[j] do skip; // decides if P(i) can go into the critical section
var flag: array[0..1] of boolean;
turn: 0..1;
P(i): flag[i] = true
turn = j; // check if P(j) wants access to Critical Section
turn = j; // check if _Pj_ wants access to Critical Section
while(flag[j] and turn = j) do skip; // if any is false, then execute next line.
CS
flag[i] = false;
@@ -216,3 +216,72 @@ V(S): S.value = S.value + 1;
end;
end;
```
- Semaphore variables can also be used to solve the Producer/Consumer Problem. The following is a possible implementation:
```Pascal
full, empte, mutex: semaphore;
nextp, nextc: item;
full = 0;
empty = n; // indicate the number of items in buffer
mutex = 1; // maintain mutual exclusion of buffer
// Producer process
P: repeat
produce an item in nextp;
...
P(empty)
P(mutex)
...
add nextp to buffer;
...
V(mutex)
V(full)
until false;
// Consumer process
C: repeat
P(full);
P(mutex);
...
remove an item from buffer to nextc;
...
V(mutex)
V(empty)
...
until false;
```
- Another problem that can be solved using semaphore variables is the __Reader/Writer__ Problem. Only one writer process should be allowed to _access_ the file at a time. Also, many reader processes can access the file simultaneously. But if the one writer process is _modifying_ a file, then no reader process should be given access to that file. If there is one reader process accessing the file, then no other writer process should be allowed to access the file.
- The following implementation is a solution for the reader/writer problem using semaphore variables:
```Pascal
mutex, wrt: semaphore; <-- 1
readcount: integer; <-- 0
// reader process
R:
P(mutex);
readcount = readcount + 1;
if readcount = 1 then P(wrt);
V(mutex) // before this statement is executed, no other process can execute the previous two lines of code.
...
Read
...
P(mutex);
readcount = readcount - 1;
if readcount = 0 then V(wrt); // release the file when exiting the last reader process.
V(mutex);
// writer process
W:
P(wrt);
...
Writes
...
V(wrt);
```
- In such operations, __atomicity__ in ensured by providing priority levels in the CPU scheduling.
@@ -9,6 +9,7 @@ A useful handbook for getting to know the basics on **Operating System Concepts*
- Chapter 3 - [Process Management (Distributed Environment)](https://github.com/omarsar/os/blob/master/3.%20Process%20Managment%20(Disitributed%20Environment).md)
- Chapter 4 - [Concurrent Processing](https://github.com/omarsar/os/blob/master/4.%20Concurrent%20Processing.md)
- Chapter 5 - [Critical Section Problem](https://github.com/omarsar/os/blob/master/5.%20Critical%20Section%20Problem.md)
- Chapter 6 - [Deadlock Problem]()
### :point_right: TODO List
- Add examples through images.
2 log.md
@@ -1,3 +1,3 @@
### Log Sheet
- **7/25/2016** - Finished notes on process management and started notes on process management (Distributed Environment).
- **7/26/2016** - Finished notes on concurrent management and started critical section problem
- **7/26/2016** - Finished notes on concurrent management and started critical section problem; started notes on deadlock problem

0 comments on commit 392365c

Please sign in to comment.