Docsity

HPC Lecture 19: Exceptions, IPC, and Synchronization, Slides of Computer Science

Partial preview of the text

Download HPC Lecture 19: Exceptions, IPC, and Synchronization and more Slides Computer Science in PDF only on Docsity!

High Performance Computing

Lecture 19

2

Exceptions

 Certain exceptional events that occur during

program execution, handled by the

processor HW

 There are two kinds of exceptions

1. Traps

 Page fault, Divide by zero, System call

2. Interrupts

 Timer, keyboard, disk

: Synchronous, software generated

: Asynchronous, hardware generated

4

Re-look at Process Lifetime

 Which process has the exception handling

time accounted against it?

 The process running at the time of the exception

 All interrupt handling time while process is in

running state is accounted against it

 As part of `running in system mode’

5

Concurrent Programming

 Until now: Program execution involved one

flow of control through the program

 Concurrent programming is about programs

with multiple flows of control

 For example: a program that runs as multiple

processes cooperating to achieve a common

goal

 To cooperate, processes must somehow

communicate

7

Inter Process Communication (IPC)

1. Processes can communicate using files

2. OS supports something called a pipe

 corresponds to 2 file descriptors (int fd[2])

 Read from fd[0] accesses data written to fd[1] in

FIFO (First In First Out) order and vice versa

8

Other IPC Mechanisms

1. Processes can communicate using files

2. OS supports something called a pipe

3. Processes could communicate through

variables that are shared between them

 Shared variables, shared memory; other

variables are private to a process

 Special OS support for program to specify

objects that are to be in shared regions of

address space

10

Other IPC Mechanisms

1. Processes can communicate using files

2. OS supports something called a pipe

3. Processes could communicate through

variables that are shared between them

4. Processes could communicate by sending

and receiving messages to each other

 Special OS support for these messages

11

More Ideas on IPC Mechanisms

5. Sometimes processes don’t need to

communicate explicit values to cooperate

 They might just have to synchronize their

activities

 Example: Process 1 reads 2 matrices, Process 2

multiplies them, Process 3 writes the result

matrix

 Process 2 should not start work until Process 1

finishes reading, etc.

 Called process synchronization

 Synchronization primitives

 Examples: mutex lock, semaphore, barrier

13

shared int X = 0;

X

Process P1 (^) Process P

time

LW R1, X

LW R1, X

ADD R1, R1, 1

SW X, R

ADD R1, R1, 1

SW X, R

R1 R

Context Switch

Context Switch

X++ X++

LW R1, X

ADD R1, R1, 1

SW X, R

FINAL

VALUE

OF X

COULD

BE 1

14

Problem with using shared variables

 Final value of X could be 1!

P1 loads X into R1, increments R

P2 loads X into register before P1 stores new value into X

Net result: P1 stores 1, P2 stores 1

 Moral of example: Necessary to synchronize

processes that are interacting using shared variables

 Problem arises when 2 or more processes try to

update shared variable

 Critical Section: part of program where shared

variable is accessed like this

16

Implementing a Lock

int L=0; /* 0: lock available */

AcquireLock(L):

while (L==1);

L = 1;

ReleaseLock(L):

L = 0;

/* `BUSY WAITING’ */

17

Why this implementation fails

while ( L == 1) ;

L = 1;

wait: LW R1, Addr(L)

BNEZ R1, wait

ADDI R1, R0, 1

SW R1, Addr(L)