lect20 - CMSC 216 Introduction to Computer Systems Lecture...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC 216 Introduction to Computer Systems Lecture 20 Concurrency Jan Plane & Pete Keleher {jplane, [email protected] Administrivia • Project 5 • Read Reek, Section 16.3 on time measurement • Read Reek, Section 13.3 on function pointers • Read Chapter 12 on Concurrent Programming 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C ONCURRENT P ROGRAMMING Chapter 12, Bryant and O'Hallaron 3 Concurrency • We have seen concurrency in our programs before, with processes, as well as ways for processes to communicate with each other (signals, pipes) • Since processes have separate virtual address spaces, working with common data requires considerable communication and synchronization overhead • Concurrency can provide speedups, however, if implemented properly on a multicore system 4
Background image of page 2
Threads The use of threads allows all the threads to access common memory inside a process Each thread has a separate thread context, including: – thread ID – stack – stack pointer – program counter – registers – condition codes Threads share: – heap memory – global/static memory – open files – shared libraries – virtual address space 5 Thread model • Threads are scheduled similarly to processes; a thread that performs an I/O operation may be scheduled out of the processor while another thread is scheduled in • No parent-child relationship; the main (first) thread creates a peer thread, which are both then in the thread pool • Context switches between threads are much less expensive than those between processes 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Posix threads • The standard interface for C threads • Example of a Pthreads program: #include <pthread.h> #include <stdio.h> struct point { int x, y; }; static void *print_point(void *pointp); int main() { pthread_t tid; struct point pt = {3, 5}; pthread_create(&tid, NULL, print_point, &pt); pthread_join(tid, NULL); return 0; } static void *print_point(void *pointp) { struct point arg = * (struct point *) pointp; printf("Point: (%d, %d)\n", arg.x, arg.y); return NULL; } 7 Compiling Pthreads code • To compile the example program, you need to tell gcc to use the pthread library when linking your executable • To do this, use the -l switch to gcc : gcc -o threadex threadex.c -lpthread – This same switch is needed to use many other libraries (math functions, for example) – If you forget this, your program will not compile, and you will get an error like this: /tmp/cc3VuzAe.o(.text+0x3a): In function `main': : undefined reference to `pthread_create' – You can add this flag to the LDFLAGS variable in your Makefile to have it work correctly • All of the functions we'll talk about that begin with " pthread_ " require including <pthread.h> 8
Background image of page 4
Creating a thread • Use: int pthread_create(pthread_t *tid,
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 13

lect20 - CMSC 216 Introduction to Computer Systems Lecture...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online