Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

2.

Processes and
Interactions

Fundamentals of Modern Operating Systems


There is no reason anyone would want a computer
in their home.

Ken OlsenPresident, Chairman and founder of


Digital, 1977

Fundamentals of Modern Operating Systems


Overview 1
• 2.1 The Process Notion
• 2.2 Defining and Instantiating Processes
– Implicit Process Creation
– Precedence Relations
– Dynamic Creation With fork And join
– Explicit Process Declarations
• 2.3 Basic Process Interactions
– Competition: The Critical Problem
– Cooperation

Fundamentals of Modern Operating Systems 3


Overview 2

• 2.4 Semaphores
– Semaphore Operations and Data
– Mutual Exclusion
– Producer/Consumer Situations
• 2.5 Event Synchronization

Fundamentals of Modern Operating Systems 4


Processes 1

• A (sequential) process (also called a “task”) is


the activity of executing a program on a CPU
– instructions/program, data, thread of execution
– program is static implementation of an algorithm
• Conceptually, each process has its own CPU
– in practice processes are running concurrently
– kernel virtualizes the CPU

Fundamentals of Modern Operating Systems 5


Processes 2
• Physical concurrency = parallelism
– it requires multiple CPUs
• Logical concurrency = time-shared CPU
– multiple tasks share a common single resource
• Processes cooperate
– shared memory, messages, synchronization
• Processes compete
– CPU, memory, I/O resources

Fundamentals of Modern Operating Systems 6


Why Use Process Structure?

• Hardware-independent solutions
– processes cooperate and compete correctly,
regardless of the number of CPUs

• Structuring mechanism
– tasks are isolated with well-defined interfaces

Fundamentals of Modern Operating Systems 7


Defining/Instantiating Processes
Examples of Precedence Relationships

Figure 2-1
Fundamentals of Modern Operating Systems 8
Structuring Process Flow

• Serial execution is expressed as: S(p1, p2, …)


• Parallel execution is expressed as: P(p1, p2, …)

• Figure 2.1(c) represents the following:


S(p1, P(p2, S(p3, P(p4, p5)), p6), P(p7, p8))

Fundamentals of Modern Operating Systems 9


Process Flow Graphs 1
• (a + b) * (c + d) - (e / f) gives rise to

Fundamentals of Modern Operating Systems Figure 2-2 10


Process Flow Graphs 2

• Compare:
We have seen expression
for (c) using S/P,
(d) cannot be expressed
using S/P
– it is not properly nested

Fundamentals of Modern Operating Systems


Figure 2-1 11
Implicit Process Creation
• Processes are created dynamically using
language constructs
– no process declaration
• cobegin/coend
– syntax:
cobegin C1 // C2 // … // Cn coend
– meaning:
• all Ci may proceed concurrently
• when all terminate, the statement following
cobegin/coend continues
Fundamentals of Modern Operating Systems 12
cobegin/coend Expression

• cobegin/coend have the same expressive


power as S/P notation
– S(a,b) a; b (sequential execution by default)
– P(a,b) cobegin a // b coend

Fundamentals of Modern Operating Systems 13


cobegin/coend Example

Figure 2-4

cobegin Time_Date // Mail // { Edit ;


cobegin { Compile; Load; Execute } //
{ Edit; cobegin Print // Web coend }
coend }
coend;
Fundamentals of Modern Operating Systems 14
Data Parallelism

• Same code is applied to different data


– e.g. SIMD vector instructions
• The forall statement:
– syntax: forall (parameters) statements
– semantics (meaning):
• parameters specify set of data items
• statements are executed for each item concurrently

Fundamentals of Modern Operating Systems 15


The forall Statement

• Example: matrix multiply A = B × C


forall ( i:1..n, j:1..m )
A[i][j] = 0;
for ( k=1; k<=r; ++k )
A[i][j] = A[i][j] +
B[i][k]*C[k][j];
• Each inner product is computed sequentially
• All inner products are computed in parallel

Fundamentals of Modern Operating Systems 16


The fork and join Primitives 1

• cobegin/coend are limited to


properly nested graphs
• forall is limited to
data parallelism
• fork/join can express
arbitrary functional parallelism
– any process flow graph

Fundamentals of Modern Operating Systems 17


The fork and join Primitives 2
• Syntax: fork x
• Semantics: create new process that begins
executing at label x
• Syntax: join t,y
• Semantics:
t = t–1;
if (t==0) goto y;
• The operation must be indivisible (“atomic”)
– Why?
Fundamentals of Modern Operating Systems 18
The fork and join Primitives 3
• t1 = 2; t2 = 3;
p1; fork L2; fork L5;
fork L7; quit;
L2: p2; fork L3; fork L4; quit;
L5: p5; join t1,L6; quit;
L7: p7; join t2,L8; quit;
L4: p4; join t1,L6; quit;
L3: p3; join t2,L8; quit;
L6: p6; join t2,L8; quit;
L8: p8; quit;

Fundamentals of Modern Operating Systems Figure 2-1(d) 19


The Unix fork
• procid = fork()
• Replicates calling process
• Parent and child are identical except for
the value of procid
• Use procid to diverge parent and child:
if (procid == 0)
do_child_processing
else
do_parent_processing

Fundamentals of Modern Operating Systems 20


Explicit Process Declarations 1

• Designate piece of code as a unit of execution


– facilitates program structuring
• Instantiate:
– statically (like cobegin) or
– dynamically (like fork)

Fundamentals of Modern Operating Systems 21


Explicit Process Declarations 2
process p
process p1
declarations_for_p1
begin ... end
process type p2
declarations_for_p2
begin ... end
begin
...
q = new p2;
...
end
Fundamentals of Modern Operating Systems 22
Process Interactions
• Competition: The Critical Problem
x = 0;
cobegin
p1: …
x = x + 1;

//
p2: …
x = x + 1;

coend
• x should be 2 after both processes execute
Fundamentals of Modern Operating Systems 23
The Critical Section 1
• Interleaved execution due to parallel
processing or context switching:

p1: R1 = x; p2: …
R1 = R1+1; R2 = x;
x = R1 ; R2 = R2+1;
… x = R2;
• x has only been incremented once
• The first update (x=R1) is lost

Fundamentals of Modern Operating Systems 24


The Critical Section 2

• Problem statement:
cobegin
p1: while(1) {CS1; program1;}
//
p2: while(1) {CS2; program2;}
//
...
//
pn: while(1) {CSn; programn;}
coend

Fundamentals of Modern Operating Systems 25


The Critical Section 3
• Assume
– reading and writing of individual variables are each
atomic (indivisible)
– no priorities associated with critical sections
– relative speeds of processes are unknown
– process may halt only outside of its critical section
• Guarantee mutual exclusion
– at any time, only one process is executing within its
Critical Section (CSi)

Fundamentals of Modern Operating Systems 26


The Critical Section 4
• In addition to assuring mutual exclusion,
prevent mutual blocking:
1. Process outside of its CS must not prevent other
processes from entering its CS.
(No “dog in the manger”)
2. Process must not be able to repeatedly reenter its
CS and starve other processes (fairness)
3. Processes must not block each other forever
(deadlock)
4. Processes must not repeatedly yield to each other
(“after you”--“after you” = livelock)
Fundamentals of Modern Operating Systems 27
Software Solution

Figure 2-5

Fundamentals of Modern Operating Systems 28


Algorithm 1
• Use a single “turn” variable:
int turn = 1;
cobegin
p1: while (1) {
while (turn==2); /*wait*/
CS1; turn = 2; program1;
}
// ...

• Violates blocking requirement (1),


“dog in the manger”

Fundamentals of Modern Operating Systems 29


Algorithm 2
• Use two turn variables to indicate intent:
– c1 for process 1 and c2 for process 2
int c1 = 0, c2 = 0;
cobegin
p1: while (1) {
c1 = 1;
while (c2); /*wait*/
CS1; c1 = 0; program1;
}
// ...
• Violates blocking requirement (3), “deadlock”
– processes could wait forever
Fundamentals of Modern Operating Systems 30
Algorithm 3
• Like #2, but reset intent variable each time:
int c1 = 0, c2 = 0;
cobegin
p1: while (1) {
c1 = 1;
if (c2) c1 = 0;
else {CS1; c1 = 0; program1}
}
// ...

• Violates blocking requirements (2) and (4),


“fairness” and “livelock”
Fundamentals of Modern Operating Systems 31
Algorithm (Peterson, 1981)4
• Like #2, but use “WillWait” variable to break tie:
int c1 = 0, c2 = 0, WillWait;
cobegin
p1: while (1) {
c1 = 1;
WillWait = 1;
while(c2&&(WillWait==1));/*wait*/
CS1; c1 = 0; program1;
}
// ...
• Guarantees mutual exclusion and no blocking
Fundamentals of Modern Operating Systems 32
Cooperation
• Problems with software solutions:
– difficult to program and to verify
– processes loop while waiting (busy-wait)
– applicable to only to critical problem: Competition
for a resource
• Cooperating processes must also synchronize
• Classic generic scenario:
Producer → Buffer → Consumer

Fundamentals of Modern Operating Systems 33


Semaphores
• A semaphore s is a nonnegative integer
– P and V operate on s
• Semantics:
V(s): increment s by 1
P(s): if s>0, decrement s; else wait until s>0
• The waiting can be implemented by
– blocking the process, or
– busy-waiting (see Chapter 4)
• P and V are indivisible operations (“atomic”)
Fundamentals of Modern Operating Systems 34
Dijkstra’s Semaphores 1

• A Semaphore is a non-negative integer:


– s
– (how many tasks can proceed
simultaneously), and
• Two indivisible operations
– P(s) and V(s)

Fundamentals of Modern Operating Systems 35


Dijkstra’s Semaphores 2
– P(s), often written Wait(s); think “Pause”:
“P” from “passaren” (“pass” in Dutch) or from
“prolagan,” combining “proberen” (“try”) and
“verlagen” (“decrease”).
• while (s<1)/*wait*/; s=s-1
– V(s), often written Signal(s);
think of the “V for Victory” 2-finger salute:
“V” from “vrigeven” (“release”) or
“verhogen” (“increase”).
• s=s+1;
Fundamentals of Modern Operating Systems 36
Mutual Exclusion w/
Semaphores
semaphore mutex = 1;
cobegin
...
//
pi: while (1) {
P(mutex); CSi; V(mutex);
programi;
}
//
...
coend;

Fundamentals of Modern Operating Systems 37


Signal/Wait with
Semaphores
semaphore s = 0;
cobegin
p1: ...
P(s); /* wait for signal */
...
//
p2: ...
V(s); /* send signal */
...
...
coend;

Fundamentals of Modern Operating Systems 38


Bounded Buffer Problem
semaphore e = n, f = 0, b = 1;
cobegin
Producer: while (1) {
Produce_next_record;
P(e); P(b); Add_to_buf; V(b); V(f);
}
//
Consumer: while (1) {
P(f); P(b); Take_from_buf; V(b); V
(e);
Process_record;
}
coend
Fundamentals of Modern Operating Systems 39
Events

• Synchronous event (e.g. I/O completion)


– process waits for it explicitly
– constructs: E.wait, E.post

• Asynchronous event (e.g. arithmetic error)


– process provides event handler
– invoked whenever event is posted

Fundamentals of Modern Operating Systems 40


UNIX Event Synchronization

• UNIX signals
• kill(pid, sig)
send signal
(SIGHUP, SIGILL, SIGKILL, …)
• process may ignore signal
• process may catch signal

Fundamentals of Modern Operating Systems 41


Fundamentals of Modern Operating Systems 42

You might also like