Sie sind auf Seite 1von 14

Chapter 2

Processes

OPERATING SYSTEMS
Design and Implementation

Instructor:
Hadi Salimi
Computer Engineering Department
IRAN University of Science and Technology
hsalimi@iust.ac.ir

IPC
 Processes frequently need to
communicate with other processes
 For example, in a shell pipeline, the output
of the first process must be passed to the
second process.
 This is called Inter-Process
Communication or IPC.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 2


Race Conditions

Two processes want to access shared memory at same time. What


happens if they try to access it simultaneously?

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 3

Race Conditions
 Situations like this are called race
conditions.
 What will happen if two processes execute
the following code?
X=0;

Read(x);
X++;
Write(x);

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 4


Critical Sections
 We should prohibit more than one process
from reading and writing the shared data
at the same time.
 In other words, what we need is mutual
exclusion.
 The part of the program where the shared
memory is accessed is called the critical
section or critical region.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 5

Solution Criteria
 Conditions to hold to have a good solution:
 No two processes may be simultaneously
inside their critical regions.
 No assumptions may be made about speeds
or the number of CPUs.
 No process running outside its critical section
may block other processes.
 No process should have to wait forever to
enter its critical region.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 6


Mutual Exclusion
 Busy Waiting
 Disabling interrupts
 Strict alternation
 Peterson’s solution
 TSL instruction
 Sleep and Wakeup
 Semaphores
 Monitors
 Message passing

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 7

Disabling Interrupts
 Let each process to disable all interrupts
just after entering its critical section and
re-enable them just before leaving.
 Isit wise to let the processes do such a job?
 What will happen if one process never turn
the interrupts on?
 What if the program is running on a
multiprocessor?

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 8


Lock Variables
 Does the following code solve the
problem?

If (lock ==0) then


lock = 1;
Begin Critical Section

lock = 0;
End Critical Section

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 9

Strict Alternation

 Which condition is violated?


5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 10
Peterson’s Algorithm

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 11

TSL Instruction
 It is another proposal that requires a little
help from hardware.
 Many computers have a TEST AND LOCK
instruction that works as follows:
 Reads the memory into a register and stores
a non-zero value into it.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 12


TSL (cont.)

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 13

Sleep and Wakeup


 The described solutions requiring busy
waiting.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 14


Sleep and Wakeup
 Semaphores
 Monitors
 Message Passing

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 15

Sleep and Wakeup


 Despite busy waiting methods which
waste CPU cycles, in this method
processes may sleep or wakeup using
system calls.
 Let’s clarify this approach using an
example, namely, producers and
consumers.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 16


Sleep and Wakeup
 Consider two
processes Consumer
Process
Producer
Process
which produce
and consume
items from/to
a buffer with
size N.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 17

Producer/Consumer

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 18


Problems
 This solution is also wrong.
 Consider the situations in which both the
producer and the consumer access the
shared variable count simultaneously.
 This may cause both of the processes go
sleep.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 19

Semaphores
 In many problems there is a need to count
an event, like producing an item or
consuming it.
 Accessing to this counter should be
protected against concurrent processes.
 Such a protected counter is called a
semaphore which has more features.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 20


Semaphores (cont.)
 Two operators are defined on a
semaphore: Down and Up (generalizations
of sleep and wakeup)
Down(int& x) { Up(int& x) {
If (x > 0) If (there is any waiting process)
Pick a process from queue and make it
x--; ready;
else else
Sleep() }; x++ };
5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 21

Semaphores (cont.)
 How to protect a critical section using
semaphores?
int s = 1;
Down(s);

Critical Section

Up(s);

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 22


Semaphores (cont.)
 Consider a resource which can be shared by 3
processes. How accessing this device can be
protected using semaphores?
int x = 3;
Down(x);

Accessing the shared resource.

Up(x);

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 23

Producer/Consumer

What happens if the


down operator first
applied on mutex?

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 24


Monitors
 The unfortunate situation which causes no
process to proceed is called deadlock.
 We study it in detail in the next chapter.
 The problem was pointed out to show how
careful one must be when using
semaphores.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 25

Monitors (cont.)
 To make it easier to write correct
programs, a higher level primitive called
monitor is introduced.
 It is a collection of procedures, variables
and data structures that are all grouped in
a package.
 An important property:
 Only one process can be active in a monitor
at any time.
5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 26
Monitors (cont.)
monitor example
int x;
procedure producer(x)

end;
procedure consumer(x)

end;
5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 27

Monitors (cont.)
 Monitors are a programming language
construct, so the compiler should handle
calls to procedures.
 When a process calls a monitor
procedure, the first few instructions of the
procedure will check to see if any other
process is currently active or not.

5/19/2009 Operating Systems - By: Hadi Salimi - IUST-CE 28

Das könnte Ihnen auch gefallen