operating systems - iran university of science and...

14
OPERATING SYSTEMS Design and Implementation Chapter 2 Processes Instructor: Hadi Salimi Computer Engineering Department IRAN University of Science and Technology [email protected] 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 Operating Systems - By: Hadi Salimi - IUST-CE 2 5/19/2009 of the first process must be passed to the second process. This is called Inter-Process Communication or IPC.

Upload: hangoc

Post on 06-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

OPERATING SYSTEMSDesign and Implementation

Chapter 2Processes

Instructor:Hadi Salimi

Computer Engineering Department

IRAN University of Science and Technology

[email protected]

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

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

of the first process must be passed to the

second process.

� This is called Inter-Process

Communication or IPC.

Race Conditions

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

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

happens if they try to access it simultaneously?

Race Conditions

� Situations like this are called race

conditions.

� What will happen if two processes execute

the following code?

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

the following code?

X=0;

Read(x);

X++;

Write(x);

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

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

� 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.

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

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

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.

Mutual Exclusion

� Busy Waiting

�Disabling interrupts

�Strict alternation

�Peterson’s solution

TSL instruction

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

�TSL instruction

� Sleep and Wakeup

�Semaphores

�Monitors

�Message passing

Disabling Interrupts

� Let each process to disable all interrupts

just after entering its critical section and

re-enable them just before leaving.

� Is it wise to let the processes do such a job?

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

� Is it 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?

Lock Variables

� Does the following code solve the

problem?

If (lock ==0) then

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

If (lock ==0) then

lock = 1;

Begin Critical Section

lock = 0;

End Critical Section

Strict Alternation

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

� Which condition is violated?

Peterson’s Algorithm

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

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:

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

instruction that works as follows:

�Reads the memory into a register and stores

a non-zero value into it.

TSL (cont.)

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

Sleep and Wakeup

� The described solutions requiring busy

waiting.

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

Sleep and Wakeup

� Semaphores

� Monitors

� Message Passing

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

Sleep and Wakeup

� Despite busy waiting methods which

waste CPU cycles, in this method

processes may sleep or wakeup using

system calls.

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

system calls.

� Let’s clarify this approach using an

example, namely, producers and

consumers.

Sleep and Wakeup

� Consider two

processes

which produce

and consume

Consumer

Process

Producer

Process

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

and consume

items from/to

a buffer with

size N.

Producer/Consumer

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

Problems

� This solution is also wrong.

� Consider the situations in which both the

producer and the consumer access the

shared variable count simultaneously.

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

shared variable count simultaneously.

� This may cause both of the processes go

sleep.

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

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

� Accessing to this counter should be

protected against concurrent processes.

� Such a protected counter is called a

semaphore which has more features.

Semaphores (cont.)

� Two operators are defined on a

semaphore: Down and Up (generalizations

of sleep and wakeup)

Down(int& x) { Up(int& x) {

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

Down(int& x) {

If (x > 0)

x--;

else

Sleep() };

Up(int& x) {

If (there is any waiting process) Pick a process from queue and make it

ready;

else

x++ };

Semaphores (cont.)

� How to protect a critical section using semaphores?

int s = 1;

Down(s);

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

Down(s);

Critical Section

Up(s);

Semaphores (cont.)

� Consider a resource which can be shared by 3

processes. How accessing this device can be

protected using semaphores?

int x = 3;

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

int x = 3;

Down(x);

Accessing the shared resource.

Up(x);

Producer/Consumer

What happens if the

down operator first

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

applied on mutex?

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

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

� The problem was pointed out to show how

careful one must be when using

semaphores.

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

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

� 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.

Monitors (cont.)

monitor example

int x;

procedure producer(x)

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

end;

procedure consumer(x)

end;

Monitors (cont.)

� Monitors are a programming language

construct, so the compiler should handle

calls to procedures.

� When a process calls a monitor

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

� 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.