Dana Vrajitoru
B424 Parallel and Distributed Programming

Shared Memory Models

Programs and libraries designed for computers with several CPUs that have access to the same memory - multiprocessor machines.

The RAM memory is common, and each processor has its own cache.
The programs are called multi-threaded (multiple execution threads).

Two types of machines

Specific problems Applications for multi-threaded programs Background_play() {
  while (data) {
    Play_data(data);
    if (stop_signal) break;
    Get_data(data);
  }
}

class Stop_button {
  ...
  onClick: Generate(stop_signal);
  ...
};

Variables that are shared

Variables that are not shared

Threads Programming

A thread is an independent stream of instructions that can be scheduled to run as such by the operating system.

In the Unix/Linux environments a thread:

Process Thread
Process ID, process group ID, user ID, and group ID
Environment
Working directory.
Program instructions
Registers
Stack
Heap
File descriptors
Signal actions
Shared libraries
Inter-process communication tools
(such as message queues, pipes,
semaphores, or shared memory).
Stack pointer
Registers
Scheduling properties (such as policy or priority)
Set of pending and blocked signals
Thread specific data.

Types of Threads

Protection Boundary

A protection boundary protects one software subsystem on a computer from another, in such a way that only data that is explicitly shared across such a boundary is accessible to the entities on both sides.

All code within a protection boundary will have access to all data within that boundary.

Examples:

Transferring control across protection boundaries is expensive.

Critical Section Problem

Critical section (region): Part of the code and/or memory that must not be accessed in read or write mode by more than one process at a time.

process Critical_section() {
  while (true) {
    entry protocol;
    critical section;
    exit protocol;
    noncritical section;
  }
}

The entry and exit protocols for the critical region must satisfy the following requirements:

Atomic operation: a sequence of one or more instructions that appears to be executed as an indivisible action. Example: a statement that translates into a single machine language instruction.
The initialization, test, increase, and decrease operations must be atomic.

Example of entry and exit protocols. The while loop in the entry protocol is also known as a Busy Waiting procedure.

short control = 0;

Entry () {
  while (control < 0);
  control--;
}

Exit() {
  control++;
}