Lecture6 - CSCE 313: Introduction to Computer Systems...

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

View Full Document Right Arrow Icon
CSCE 313: Introduction to Computer Systems Spring 2011 Dr. Ronnie Ward
Background image of page 1

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

View Full DocumentRight Arrow Icon
POSIX Thread Synchronization Chapter 13 in textbook Slides adopted and modified from Dr. Bettati.
Background image of page 2
POSIX Thread Synchronization Unnamed semaphores for threads and processes Type: sem_t Functions: sem_init(), sem_wait(), sem_post() Header: #include <semaphore.h> Mutex Locks Condition Variables 2/14/12 3
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 Thread Programming Standard Thread Library for POSIX- compliant systems Supports thread creation and management Synchronization using mutex variables condition variables read/write locks At the time of creation, different attributes can be assigned to Threads 2/14/12 4
Background image of page 4
Using Posix Thread Library To use this library, #include <pthread.h> in your program. To compile, link with the pthread library: gcc myThreadedApp.c -o myThreadedApp –lpthread 5 2/14/12
Background image of page 5

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

View Full DocumentRight Arrow Icon
Data Types in POSIX mutex variables for mutual exclusion (pthread_mutex_t) mutex variables are like binary semaphores a mutex variable can be in either locked or unlocked state condition variables : a thread can sleep until some other thread signals the condition (pthread_cond_t) various kind of attribute types used when initializing: threads (pthread_attr_t) 6 2/14/12
Background image of page 6
Problem 1: What’s the problem? 7 2/14/12 Process  thread1  { foo = 1; bar = 2; } Process  thread2  { foo = 3; bar = 4; } What are the possible results?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Race condition Two threads racing to perform the same task Interleaving of operations can cause incorrect behavior Example: Therac-25 Code must be deterministic 8 2/14/12
Background image of page 8
Atomic Updates Perform multiple operations logically as a single unit When completed, exactly (A or B) is true not (A and B) not part of A and part of B Indivisible operations 9 Solution: Thread Synchronization 2/14/12
Background image of page 9

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

View Full DocumentRight Arrow Icon
POSIX Thread Synchronization Mutex Locks Condition Variables Read-Write Locks 2/14/12 10
Background image of page 10
Mutex 11 2/14/12 Enter and Exit critical  section #include <pthread.h> pthread_mutex_t mutex_var = …; int pthread_mutex_lock(mutex); int pthread_mutex_unlock(mutex); mutex_var Thread A Thread B lock block var access - A mutex is usually declared to have static  storage class.  - A mutex must be initialized before it is  used. -  locked or unlocked w/queuing - Synchronization of threads in the same or  different processes
Background image of page 11

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

View Full DocumentRight Arrow Icon
Mutex Locks 12 2/14/12 #include <pthread.h> int pthread_mutex_init ( pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restrict attr); static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; EAGAIN: System lacks non-memory resources to initialize *mutex ENOMEM: System lacks memory resources to initialize *mutex EPERM: Caller does not have appropriate privileges int pthread_mutex_destroy (pthread_mutex_t *mutex); int pthread_mutex_lock (pthread_mutex_t *mutex); int pthread_mutex_trylock (pthread_mutex_t *mutex); int pthread_mutex_unlock (pthread_mutex_t *mutex);
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course CSCE 313 taught by Professor Gu during the Spring '11 term at Texas A&M.

Page1 / 31

Lecture6 - CSCE 313: Introduction to Computer Systems...

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

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