P4: Multithreaded Programming Zhenxiao Luo CS537 Spring 2010.

Slides:



Advertisements
Similar presentations
Indian Institute of Science Bangalore, India भारतीय विज्ञान संस्थान बंगलौर, भारत Supercomputer Education and Research Centre (SERC) SE 292: High Performance.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Threads. Readings r Silberschatz et al : Chapter 4.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Thread Synchronization with Semaphores
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition chapter This material does not exist.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Lecture 10 Locks.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.
POSIX Threads HUJI Spring 2011.
Pthreads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Threads A thread is an alternative model of program execution
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Programming with Threads. Threads  Sometimes called a lightweight process  smaller execution unit than a process  Consists of:  program counter 
pThread synchronization
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Thread Programming.
Linux Processes & Threads
Multithreading Tutorial
Thread synchronization
Realizing Concurrency using Posix Threads (pthreads)
Thread Programming.
Unix System Calls and Posix Threads
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Pthread Prof. Ikjun Yeom TA – Mugyo
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
POSIX Threads(pthreads)
Presentation transcript:

P4: Multithreaded Programming Zhenxiao Luo CS537 Spring 2010

Outline Motivation Thread Basics Thread Operations Thread Synchronizations Pthread: counter, HashTable SpinLock: SpinLock, SpinCounter, SpinList, SpinHash

Setting Environment Variable setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:. As done in P3 Or, you may have: – error while loading shared libraries: libcounter.so: cannot open shared object file: No such file or directory

Motivation: Why Use Threads? To Execute Software Faster Improve performance through parallel or distributed computing Processes are too expensive Threads are effective on both multiprocessor systems and uniprocessor systems

Thread Basics All threads within a process share the same address space Threads in the same process share: –Process instructions –open files (descriptors) –current working directory Each thread has a unique: –Thread ID –set of registers, stack pointer –priority

Thread Operations Thread Creation int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); Thread Termination void pthread_exit(void *retval);

Thread Synchronization mutexes - Mutual exclusion lock joins - Make a thread wait till others are complete condition variables – wait() and signal() ‏

Pthread Counter pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; static int global_counter = 0; void Counter_Init(){ – global_counter = 0; } void Counter_Increment(){ –pthread_mutex_lock( &mutex1 ); –global_counter++; –pthread_mutex_unlock( &mutex1 ); –}

Pthread HashTable struct hash_element{ int number; struct hash_element *next; }; struct hash_bucket{ int elemCount; struct hash_element *head; }; static struct hash_bucket *hash_buckets; static int g_numOfBuckets; pthread_mutex_t *mutexArray;

Hash Table - Init void Hash_Init(int buckets){ int i = 0; hash_buckets = malloc(buckets * sizeof(struct hash_bucket)); g_numOfBuckets = buckets; mutexArray = (pthread_mutex_t*)malloc(buckets * sizeof(pthread_mutex_t)); for (i = 0; i < buckets; i++) { hash_buckets[i].head = NULL; hash_buckets[i].elemCount = 0; pthread_mutex_init(&(mutexArray[i]), NULL); }

Hash Table - Insert Get BucketNumber: –int bucketNum = x % g_numOfBuckets; Traverse the list of elements: –If x is found, return -1 –If x is not found, create new element, insert into the end of the list –Remember to allocate memory for new element –Remember to add mutex lock when inserting to the list

Hash Table - Remove Get BucketNumber: –int bucketNum = x % g_numOfBuckets; Traverse the list of elements: –If x is not found, return -1 –If x is found, remove it from the list, update pointers –Remember to add mutex lock when removing element from the list

SpinLock TestAndSet: –xchg(volatile unsigned int *addr, unsigned int newval) ‏ –Typedef struct spinlock_t { –int flag; –} spinlock_t; –void spinlock_acquire(spinlock_t *lock) { –while (xchg(&lock->flag, 1) == 1); –// spin wait}

SpinLock Cond void spinlock_release(spinlock_t *lock) { lock->flag = 0; } void spinlock_init(spinlock_t *lock) { lock->flag = 0; }

SpinCounter –typedef struct __counter_t { –int counter; –spinlock_t lock; –} counter_t; –void Counter_Increment(counter_t *c) ‏ –{ – spinlock_acquire(&c->lock); – c->counter++; – spinlock_release(&c->lock); –}

SpinList typedef struct __listnode_t { unsigned int key; void *value; struct __listnode_t *next; } node; typedef struct __list_t { node *head; spinlock_t lock; } list_t;

SpinList Cond void List_Insert(list_t *l, void *element, unsigned int key) { node* tmp = malloc(sizeof(node)); tmp->key = key; tmp->value = element; spinlock_acquire(&l->lock); tmp->next = l->head; l->head = tmp; spinlock_release(&l->lock); }

SpinHash typedef struct __hash_t { int numbuckets; // # of buckets list_t *hash_buckets; } hash_t; void Hash_Insert(hash_t *h, void *element, unsigned int key) { // compute hash; int index = key % h->numbuckets; List_Insert(&h->hash_buckets[index], element, key); } void Hash_Remove(hash_t *h, unsigned int key) { // compute hash; int index = key % h->numbuckets; List_Remove(&h->hash_buckets[index], key); }