









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Li; Class: PARALLEL PROGRAMMING; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 1 / 32
In a shared-memory system, a single address space exists, i.e. each memory location is given a unique address, and any memory location is accessible by any of the processors. A shared-memory system is typically used in two ways:
Threads are related to the concept of processes. Both are runtime entities created by a program, and can run concurrently with their parent and with their siblings. They differ in “weight” and in control structure location.
User Space Kernel Space
Stack User Code Data
Process Structure
Data
User Code
User Space Kernel Space Process Structure
Thread Structure
Stack Stack P1 P
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 3 / 32
Process — a self-sufficient, independent flow of control.
Thread — an independent flow of control (with its stack, pc, etc.)
Thread 1 Thread 2 stmt A stmt X stmt B stmt Y stmt C stmt Z
There are several possible orderings, including
stmt A, stmt B, stmt X, stmt C, stmt Y, stmt Z stmt X, stmt Y, stmt A, stmt B, stmt C, stmt Z
Any possible statement order resulted from interleaving defines a legal execution of the program!
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 7 / 32
Expressing Parallelism:
Synchronization Mechanism:
A non-intrusive approach for providing shared-memory programming power. In this case, the parallelism information is presented in the form of compiler directives inserted into the user program. The program can compile and run directly on a sequential computer by simply disregarding the directives.
Example: OpenMP — A specification for a set of compiler directives, library routines, and environment variables that can be used to specify shared-memory parallelism in Fortran and C/C++ programs. OpenMP is jointly defined by a group of major computer hardware and software vendors.
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 9 / 32
Thread libraries provide a less-intrusive alternative to language constructs for providing programmers with shared-memory programming power.
Examples:
This is the most aggressive approach. Designing a new language has the advantage that parallel features can truely be integrated into the language model. However, the challenge for wide-acceptance is huge.
So far, not many attempts have been made in this area.
Cilk/Cilk++ is a boarderline example, it is C/C++ based, with a small set of keywords added. When the Cilk/Cilk++ keywords are removed from Cilk/Cilk++ source code, the result is a valid C program.
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 13 / 32
Consider two threads each is to add one to a shared data item, x. To accomplish this, it is necessary for the contents of the x location to be read, x + 1 computed, and the result written back to the location. So we have,
Thread 1 Thread 2 read x read x compute x + 1 compute x + 1 write x back write x back
Due to the possibility of interleaved execution of the statements, we may get different results at the end.
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 15 / 32
The problem of accessing shared data can be generalized by considering shared resources. A mechanism for ensuring that only one thread accesses a particular resource at a time is to establish sections of code involving the resource as so-called critical sections and arrange that only one thread executes a critical section for the resource at a time.
This mechanism is known as mutual exclusion.
Deadlock can occur with two threads when each requires a resource held by the other. For example,
Thread 1 Thread 2 requests A requests B requests B requests A uses A and B uses A and B
Deadlock occurs when Thread 1 holds A, Thread 2 holds B, and each wants to get another resource.
Deadlock can also occur in a circular fashion with several threads.
Thread 1 Thread 2 Thread k holds A 1 holds A 2 · · · holds Ak requests A 2 requests A 3 requests A 1
Deadlock can be avoid if all threads request resources in the same order. Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 19 / 32
When multiple copies of a particular shared resource is available for multiple threads to use, using a lock to control the access would not be appropriate.
A semaphore, s, is a non-negative integer operated upon by two operations named P and V.
The P and V operations are performed atomically. A mechanism for activating waiting threads is also implicit in the operations.
Threads delayed by P(s) are kept in sleep until released by a V(s) on the same semaphore. Semaphore routines exist for Unix threads. They do not exist in Pthreads, though the Unix semaphore routines can be used in Pthreads programs.
Mutual exclusion of critical sections can be achieved with one semaphore having the value 0 or 1, which acts as a lock variable.
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 21 / 32
General semaphores can take on positive values other than 0 and 1. Such semaphores provide a means of recording the number of “resource units” available or used, and can be used to solve producer/consumer problems.
Example:
producer() { request_t *request; while(TRUE) { request = get_request(); add_request(request); V(queue˙length); } }
consumer() { request_t *request; while(TRUE) { P(queue˙length); request = remove_request(); thread_request(request); } }
wait
Lock
cond?
Unlock
Unlock
Sleep
Continue
signal
Lock
cond=TRUE
Unlock
Wakeup
Continue
n
y
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 25 / 32
Consider one or more threads designed to take action when a counter, x, is zero. Another thread is responsible for decrementing the counter.
action() { ... lock(); while (x != 0) wait(s); unlock(); take_action(); ... }
counter() { lock(); x--; if (x == 0) signal(s); unlock(); ... }
Monitors are introduced for providing onject-oriented style of access control — Essentially the protected data and the operations (monitor procedures) that can operate upon it are encapsulated inside a structure. Reading and writing to the data can only be done by using monitor procedures, and only one thread can use a monitor procedure at any instant.
A monitor procedure could be implemented using a semaphore to protect its entry; i.e.,
monitor_proc1() { P(monitor_semaphore);
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 27 / 32
The concept of monitor exists in Java. The keyword synchronized in Java makes a method thread safe, preventing more than one thread executing the method at the same time.
public class Adder { public int[] array; private int sum = 0, index = 0, numThreads = 10, threadQuit;
public Adder() { array = new int[1000]; threadsQuit = 0; initializeArray(); startThreads(); } private void initializeArray() { for (int i = 0; i<1000; i++) array[i] = i; }
System calls or library routines are called thread safe if they can be called from multiple threads simultaneously and always produce correct results.
Jingke Li (Portland State University) CS 415/515 Shared-Memory Programming 31 / 32
The above synchronization mechanisms provide basic access control on shared data. For many applications, stronger policies are required on top of them, such as