Title: Processes
1?????? ?????
????? 3 ?????? ???????
2Concurrent processes
- In a multiprogramming environment, processes
executing concurrently are either - Independent process cannot affect or be affected
by the execution of another process. - Cooperating process can affect or be affected by
the execution of another process. - For independent processes the OS carefully
allocates resources and properly isolates
processes from each other. - For cooperating processes the OS provides
mechanisms to share some resources in certain
ways and interact with each other.
3Processes cooperating
- Directly e.g. by exchanging messages or
- Indirectly e.g. by sharing common memory
- Advantages of process cooperation
- Information sharing
- Computation speed-up
- Modularity
- Convenience
4A potential problem
- Instructions of cooperating processes can be
interleaved arbitrarily. Hence, the order of
(some) instructions are irrelevant. However,
certain instruction combinations must be
eliminated. For example - Process A Process B concurrent access
- A 1 B 2 does not matter
- A B 1B B 2 important!
5Race Condition
- Race condition The situation where several
processes access and manipulate shared data
concurrently. The final value of the shared data
depends upon which process finishes last..
6????? ??????
- time Person A Person B
- 300 Look in fridge. Out of milk.
- 305 Leave for store.
- 310 Arrive at store.
Look in fridge. Out of milk. - 315 Buy milk. Leave for store.
- 320 Leave the store.
Arrive at store. - 325 Arrive home, put milk away. Buy
milk. - 330 Leave the store.
- 335 Arrive home. OH! OH!
7Printer Spooler
- SPOOL (Simultaneous Peripheral Operation On Line)
- ?- Printer SPOOL ???? ????? ?? ????, ???? ??? ??
???? ????? ???? ??????. - ?"? ?????? ????? ?????? next_free_slot ????? ???
??? ????? ???. - ???? ????? ????? ???? ?????? ??? ???? ?? ???? ??
next_free_slot, ????? ????? ?? ????? ?????? ?????
?? next_free_slot ?????. - Printer Daemon ????? ?? ?"? ????? ?? ??? ??? ??
????? ?????? ????.
8???? ?????
- ????? A ???? ?????? ???? ??? ???? ??
next_free_slot (???? ????? 4). - ??? ???? ???? ?? ?? ????? ?????? ?"? ?????? ??
????? B ?????. - ?? ????? B ??????? ?????? ??? ???? ?? ???? ??
next_free_slot, ????? ?? ?? ????? ?????? ????? ??
next_free_slot ?-5. - ??? ?"? ?????? ?? ????? A ?????. ?? ????? ??
????? ??? ??? 4 ????? ?? ?-next_free_slot ?-6.
9The Critical-Section
- n processes all competing to use some shared data
- Each process has a code segment, called critical
section, in which the shared data is accessed.
10?? ???? ??? ????? ?? ??????
- 1. Mutual Exclusion. If process Pi is executing
in its critical section, then no other processes
can be executing in their critical sections. - 2. Progress. If no process is executing in its
critical section and there exist some processes
that wish to enter their critical section, then
the selection of the processes that will enter
the critical section next cannot be postponed
indefinitely.
11????? ?????? ?? ?"?
- ???? ????? ???? ????? ????? ??? ???? ?? ?????
?????? ?? ?"?. - ????? ???? ?????? ??? ????? ??"? ????? ?????
??????. - ?????? ?? ????? ??? ???????.
- ?????? ???? ????? ?????? ??"? ????? ?? ??????.
12???? ??????
- ????? ????? ?-?????????
- ??????? ????, ?????, ?????, ????
- ????? ????? ?????????
- ??????? ????? ????? (system call) ???? ???? ??
?????? - ???? ????? ?????????
- ??????? ????? ?- 0, page fault, ????? ???
13?????? ????? ?????? ???????
- ?????? ????? ?? ?? ?????? (interrupt request
line) ???? ??? ????? ???? ????? ?? ????? ????? - ???? ??????? ?? ?? ??????? ????? ????? (?????),
????? ?????????? ????? ???? ????? ?? ???? ??????
(?? ?? ?????) ??????? ???? ??? ?????? ???? ??????
?????? (???????? ???) - ???? ?????? ?????? (ISR interrupt service
routine) ????? ?? ?????? ????? ?????? ?????? - ????? ??????? ?"? ????? ???? ???? ????? ??????
?????? - Â ?????? ???? ?? ?????? ?????? ?????? ?????? ????
????? ???? ???????
14(No Transcript)
15?????? ???????
- ??? ?????? ?????? ?????? ???? ?????? ??????
???????, ??????? ???? ???? ????? ?? ????? ????
??????. - ???? ????????????, ????? ??? offset ????? ??????
?????? ????? ??????? (interrupt vector) - ?????? ??????? ???? ?? ??????? ?? ?????? ??????
?????? ??????? ???? ?????? ?????.
16?????
- ??????, ?????? ??????? ?? Pentium ???? 256
"??????"
17????? ????? ????
- ????? ??????? ??? ??????
- ?. ????? ??? ??? ???????? (timesharing).
- ?. ????? ????? ????? ???? ???? ??? ????.
- ?. ???? ???? ???.
18?????? ??? ????
- ?????? ??? ???? (exception) ?????? ????? ?????
?????? (????????) ??????? ??????? ?????? - ??????? ?????? ?????? ????? ?- 0, ????? ???,
???? ?????? ????? (?? ?? ?????), ????? ????? ??
????? - ????? ?????? ??? ?????? ??????? ???? ??? ??
????? ????? ????? - ???? ?????? ??? ???? ???, ??? ??? ?????? ??? ????
??? ??????, ???? ??? ????? ??? ?????? ?? ??????
?????? ?? ????? ?????? ??????? - ??????? ?? ???? ????? ???? ????? ????? ???????
?????. - ?????? ??? ???? ?? ????? ?????? ???? ?????
??????? ????? (?? nonmaskable) - ?????? ??? ???? ?? ??????????
19????? ?????
- ????? ????? ?????.
- ???? ?????? ???? ??? (?? ????)
- ???? ????? ???? ?????? ????? ?????? ??? ???? ??
???? ?? ????? ??????. - ?? ??? ?? ???? ??? ???? ????? ?????? ????? ??
???? ???? (????). - ????? ????? ?????? ??? ????? ?? ???? ????.
- ??? ????? ?? ?????? ??? ????? ?-SPOOL.
20????? ???
???? ????? 0
- while (TRUE)
- while (turn ! 0) / wait /
- critical_section()
- turn1
- noncritical_section()
while (TRUE) while (turn ! 1) / wait
/ critical_section() turn0 noncritical_sect
ion()
???? ????? 1
21????? ???
- ???? ?? ????? ?-ME.
- ??? ?? ????? ?? ???? ?-PROGRESS. ?????
- ????? ???? ??????? ?-Busy Waiting ?? ???? ??????
???? ?? ???? ????? ??????.
22Petersons algorithm
???? ????? 0
- while (TRUE)
- interested0TRUE
- turn0
- while (interested1 turn0)
- critical_section()
- interested0FALSE
- noncritical_section()
while (TRUE) interested1TRUE turn1 while
(interested0 turn1) critical_section() in
terested1FALSE noncritical_section()
???? ????? 1
23Yes, correct but...
- Petersons algorithm is correct. However, it only
work for 2 processes. - It can be generalized for N processes, but
- N must be fixed (known a priori).
- The algorithm become much too complicated and
expensive.
24Bakery algorithm of Lamport
- Critical section algorithm for any ngt1
- Each time a process is requesting an entry to CS,
assign it a ticket which is - Unique and monotonically increasing
- Let the process into CS in the order of their
numbers
25Bakery algorithm
- boolean choosingn
- int numbern
- ...
- while( TRUE )
- choosingi TRUE
- numberi max(number0,...,
numbern-1)1 - choosingi FALSE
- for( j0 jltn j )
- while( choosingj )
- while( numberj ! 0 ( numberj,j)
lt (numberi,i) ) -
- critical_section()
- numberi 0
- noncritical_section()
-
- Where
- max(a0, ..., an-1) is a number, k, such that
kgtai for i0, ..., n-1, and - (a,b) lt (c,d) if altc or if ac and bltd.
26Choosing a ticket
27Correctness
Lemma
Mutual exclusion is immediate from this lemma It
is easy to show that Progress hold as well
28Hardware support - TSL
- ?? ?????? ??????? ?? ?????? Test and Set Lock
- ????? ?? ????? ??????
- ??? ????? ???? ?? ????? ?????? ??????? ???? ????
?????? ????? ????? ??????? ??? ????? ???. - ?????? ??? ????? ?????? ????? ?? ?? ???? ??????
?????? ??, ?????? ?????? ???? ?? ????? ??????? ??
????? ?????? ???? ???? ???????? ????? ???????
??????.
29??? ???? ??????
- Enter_region
- tsl register,flag
- cmp register,0 //was flag zero?
- jnz enter_region //if it was non zero, so loop
- ret
- Â
- Leave-region
- mov flag,0
- ret
30????? ????????, TSL ?-Bakery
- - BUSY WAITING ?????? ????? ????? ?????? ????
?????? ?????? ?? ????. - ???? ?? ???
- ????? CPU.
- ?? ?????? ????? ?????? ???? ?? ?????? ????, ??
??? ???????? ?????? ??? ??. - Â ???? ????? ????? ??? ?????? ???? ?????? ?????
???? ?? ??????? SLEEP ?- WAKEUP.
31Interprocess Communication (IPC)
- Mechanism for processes to communicate and to
synchronize their actions. - Message system processes communicate with each
other without resorting to shared variables. - IPC facility provides two operations
- send(message) message size fixed or variable
- receive(message)
- If P and Q wish to communicate, they need to
- establish a communication link between them
- exchange messages via send/receive
- Message passing may be either synchronous or
asynchronous.