Multi-threaded Programming
|
|
- Augustus Briggs
- 6 years ago
- Views:
Transcription
1 Multi-threaded Programming Trifon Ruskov Technical University of Varna - Bulgaria 1
2 Threads A thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. We can think of a thread as basically a lightweight process: A thread has an execution state (running, ready, etc.). The thread contexts saved when not running. Threads are able to be scheduled by the operating system and run as independent entities within a process. A thread has an execution stack and some per-thread static storage for local variables. A process can have multiple threads. A thread has access to the memory address space and resources of its process. Trifon Ruskov Technical University of Varna 2
3 Benefits of Threads vs. Processes Less time to create a new thread than a process, because the newly created thread uses the current process address space. Less time to terminate a thread than a process. Less time to switch between two threads within the same process, partly because the newly created thread uses the current process address space. Less communication overheads - communicating between the threads of one process is simple because the threads share everything: address space, in particular. Trifon Ruskov Technical University of Varna 3
4 Multi-threading vs. single-threading Single threading When the OS does not support the concept of threads. Multi-threading When the OS supports multiple threads of execution within a single process. single process, single thread single process, multiple threads Examples of OS: MSDOS - supports a single user process and a single thread UNIX - supports multiple user processes but only supports one thread per process Solaris - supports multiple threads multiple processes, single thread multiple processes, multiple threads Trifon Ruskov Technical University of Varna 4
5 Benefits of Multi-threaded Programming Improve application responsiveness. Any program in which many activities are not dependent upon each other can be redesigned so that each activity is defined as a thread. Use multiprocessors more efficiently. Typically, applications that express concurrency requirements with threads need not take into account the number of available processors. Applications with a high degree of parallelism, such as numerical algorithms and matrix multiplications, can run much faster when implemented with threads on a multiprocessor. Improve program structure. Multi-threaded programs can be more adaptive to variations in user demands than single threaded programs. Use fewer system resources. Programs with two or more processes that access common data through shared memory are applying more than one thread of control. The cost of creating and maintaining a large amount of process state information makes each process much more expensive than a thread in both time and address space. Trifon Ruskov Technical University of Varna 5
6 Models of Single and Multi-threaded Applications Single-Threaded Process Model Multithreaded Process Model Thread Thread Control Block Thread Thread Control Block Thread Thread Control Block Process Control Block User Stack Process Control Block User Stack User Stack User Stack User Address Space Kernel Stack User Address Space Kernel Stack Kernel Stack Kernel Stack Trifon Ruskov Technical University of Varna 6
7 Thread Levels There are two broad categories of thread implementation: User-level threads (ULT). The kernel is not aware of the existence of threads. All thread management is done by the application by using a thread library. Thread switching does not require kernel mode privileges and scheduling is application specific. Kernel-level threads (KLT). All thread management is done by the kernel. No thread library but an API (system calls) to the kernel thread facility exists. The kernel maintains context information for the process and the threads, switching between threads requires the kernel. Scheduling is performed on a thread basis. Trifon Ruskov Technical University of Varna 7
8 Advantages and Disadvantages of User Level Threads Advantages: Thread switching does not involve the kernel - no mode switching. Scheduling can be application specific - choose the best algorithm. User level threads can run on any OS - only needs a thread library. Disadvantages: Most system calls are blocking and the kernel blocks processes - so all threads within the process will be blocked. The kernel can only assign processes to processors two. Two threads within the same process cannot run simultaneously on two processors. Trifon Ruskov Technical University of Varna 8
9 Advantages and Disadvantages of Kernel Level Threads (cont.) Advantages: The kernel can simultaneously schedule many threads of the same process on many processors. Blocking is done on a thread level. Kernel routines can be multi-threaded. Disadvantages: Thread switching within the same process involves the kernel. This results in a significant slowdown. Trifon Ruskov Technical University of Varna 9
10 Combined User and Kernel Level Approaches Thread creation done in the user space. Bulk of scheduling and synchronization of threads done in the user space. The programmer may adjust the number of kernel level threads. Process includes the user's address space, stack, and process control block. User-level threads (threads library) invisible to the OS are the interface for application parallelism. Kernel threads are the unit that can be dispatched on a processor. Each lightweight process supports one or more ULTs and maps to exactly one KLT. Trifon Ruskov Technical University of Varna 10
11 Example of a Combined Approach Solaris thread implementation User L L L L L L L Kernel Hardware P P P P User-level thread Kernel-level thread L P Leight-weight Process Processor Trifon Ruskov Technical University of Varna 11
12 Thread libraries The interface to multi-threading support is through a subroutine library The library must contain code for: creating and destroying threads passing messages and data between threads scheduling thread execution saving and restoring thread contexts Examples of thread libraries: libpthread for POSIX threads libthread for Solaris threads Trifon Ruskov Technical University of Varna 12
13 The POSIX Thread library (Pthreads) What are Pthreads? A standardized programming interface for UNIX systems. The interface has been specified by the IEEE POSIX c standard (1995). Pthreads are defined as a set of C language programming types and procedure calls, specified with a pthread.h. There are several drafts of the POSIX threads standard. The primary motivation for using Pthreads is to realize potential program performance gains. Trifon Ruskov Technical University of Varna 13
14 Designing Threaded Programs A program must be able to be organized into discrete, independent tasks which can execute concurrently. For example, if routine1 and routine2 can be interchanged, interleaved and/or overlapped in real time, they are candidates for threading. routine1 routine2 final routine routine2 routine1 final routine r1 r2 r1 r2 r1 r2 final routine routine1 routine1 time routine2 routine2 final final routine routine Trifon Ruskov Technical University of Varna 14
15 Designing Threaded Programs (cont.) Tasks that may be suitable for threading include tasks that: Block for potentially long waits Use many CPU cycles Must respond to asynchronous events Are of lesser or greater importance than other tasks Are able to be performed in parallel with other tasks An application must not use libraries or other objects that don t explicitly guarantee thread-safety! Trifon Ruskov Technical University of Varna 15
16 Common Models for Threaded Programs Manager/Workers. A single thread, the manager, assigns work to other threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks. Two forms of the manager/worker model are common: static worker pool and dynamic worker pool. Pipeline. A task is broken into a series of suboperations, each of which is handled in series, but concurrently, by a different thread. For example - an automobile assembly line. Peer. Similar to the manager/worker model, but after the main thread creates other threads, it participates in the work. Trifon Ruskov Technical University of Varna 16
17 The Pthreads API Three major classes of Pthreads subroutines: Thread management. This class of functions work directly on threads - creating, detaching, joining, etc. They include functions to set/query thread attributes (joinable, scheduling etc.). Mutexes. This class deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. Condition variables. This class of functions address communications between threads that share a mutex. They are based upon programmer specified conditions. All identifiers in the threads library begin with pthread_ Trifon Ruskov Technical University of Varna 17
18 Thread Management. Creating Threads int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void*(*start_routine)(void *), void *arg); Parameters: tid - Returned new thread ID. tattr - Specify a thread attributes object, or NULL for the default values. This defines the thread as joinable (non detached) - its ID and other resources can not be reused as soon as the thread terminates. start_routine - The C routine that the thread will execute once it is created. arg - A single argument may be passed to start_routine. This routine creates a new thread and makes it executable. Typically, threads are first created from within main() inside a single process. Once created, threads are peers, and may create other threads. Initially, main() comprises a single, default thread. All other threads must be explicitly created by the programmer. Trifon Ruskov Technical University of Varna 18
19 Thread Management. Terminating Thread Execution Ways in which a thread may be terminated: The thread returns from its first (outermost) procedure. The thread makes a call to the pthread_exit() subroutine. The thread is canceled by another thread via the pthread_cancel() routine (not covered here). The entire process is terminated due to a call to either the exec or exit subroutines. void pthread_exit(void *status); The pthread_exit() function terminates the calling thread. All thread-specific data bindings are released. If the calling thread is not detached, then the thread's ID and the exit status specified by status are retained until the thread is waited for (blocked). Otherwise, status is ignored and the thread's ID can be reclaimed immediately. Use pthread_exit() to exit from all threads, especially main(). Trifon Ruskov Technical University of Varna 19
20 Example: Thread Creation and Termination #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid){ printf("\n%d: Hello World!\n", threadid); pthread_exit(null); int main (int argc, char* argv[]){ pthread_t threads[num_threads]; int rc, t; for(t=0; t < NUM_THREADS; t++){ printf("creating thread %d\n", t); // Create a thread and pass its number as argument rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("error: pthread_create %d\n", rc); exit(1); pthread_exit(null); Trifon Ruskov Technical University of Varna 20
21 Thread Management. Joining Threads "Joining" is one way to accomplish synchronization between threads. int pthread_join(thread_t tid, void **status); Parameters: tid ID of a thread, which the current thread is waiting to end. status Returned value of thread tid, which it gave as argument to pthread_exit(). The pthread_join() function blocks the calling thread until the specified thread tid terminates. The specified thread must be in the current process. It is impossible to join a detached thread. Multiple threads cannot wait for the same thread to terminate. If they try to, one thread returns successfully and the others fail. Trifon Ruskov Technical University of Varna 21
22 Example: Threads Joining #include <pthread.h> #include <stdio.h> #define NUM_THREADS 3 // A working thread void *BusyWork(void *null){ int i; double result=0.0; // Simulate working for (i=0; i < ; i++){ result = result + (double)random(); printf("result = %e\n",result); pthread_exit(null); Trifon Ruskov Technical University of Varna 22
23 Example: Threads Joining (cont.) int main (int argc, char* argv[]){ pthread_t thread[num_threads]; int rc, t, status; for(t=0; t < NUM_THREADS; t++){ printf("creating thread %d\n", t); // Create a thread as JOINABLE rc = pthread_create(&thread[t], NULL, BusyWork, NULL); if (rc){ printf("error: pthread_create %d\n", rc); exit(1); for(t=0;t < NUM_THREADS;t++){ // Wait for other treads rc = pthread_join(thread[t], (void **)&status); if (rc){ printf("error: pthread_join %d\n", rc); exit(1); printf("main: Completed join with thread %d status= %d\n",t, status); pthread_exit(null); Trifon Ruskov Technical University of Varna 23
24 Threads Attributes Attributes are a way to specify behavior that is different from the default. When a thread is created with pthread_create() or when a synchronization variable is initialized, an attribute object can be specified. An attribute object is opaque, and cannot be directly modified by assignments. A set of functions is provided to initialize, configure, and destroy each object type. Once an attribute is initialized and configured, it has process-wide scope. Attributes are specified only at thread creation time; they cannot be altered while the thread is being used. Trifon Ruskov Technical University of Varna 24
25 Threads Attributes (cont.) Attribute Default value Result scope detachstate stackaddr stacksize inheritsched schedpolicy PTHREAD_SCOPE_PROCESS PTHREAD_CREATE_JOINABLE NULL 1 megabyte PTHREAD_INHERIT_SCHED SCHED_OTHER New thread is not permanently attached to LWP Exit status and thread are preserved after the thread terminates New thread has system allocated stack address New thread has system allocated stack size New thread inherits parent thread scheduling policy Non-real-time preemptive scheduling policy Trifon Ruskov Technical University of Varna 25
26 Example: Using Thread Attributes #include <pthread.h> #include <stdio.h> #define NUM_THREADS 3 // A working thread void *BusyWork(void *null){ int i; double result=0.0; // Simulating a work for (i=0; i < ; i++){ result = result + (double)random(); printf("result = %e\n",result); pthread_exit(null); Trifon Ruskov Technical University of Varna 26
27 Example: Using Thread Attributes (cont.) int main (int argc, char* argv[]){ pthread_t thread[num_threads]; pthread_attr_t attr; // Attribute object int rc, t, status; // Initialize and set thread detached attribute as joinable pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,pthread_create_joinable); for(t=0;t < NUM_THREADS;t++){ printf("creating thread %d\n", t); rc = pthread_create(&thread[t], &attr, BusyWork, NULL); if (rc){ printf("error: pthread_create()is %d\n", rc); exit(1); Trifon Ruskov Technical University of Varna 27
28 Example: Using Thread Attributes (cont.) // Free attribute and wait for other threads pthread_attr_destroy(&attr); for(t=0; t < NUM_THREADS; t++){ rc = pthread_join(thread[t], (void **)&status); if (rc){ printf("error: pthread_join()is %d\n", rc); exit(1); printf("main: Completed join with thread %d status= %d\n",t, status); pthread_exit(null); Trifon Ruskov Technical University of Varna 28
29 Threads Synchronization Methods of synchronizing threads: Mutual Exclusion (Mutex) locks Condition variables Semaphores. Synchronizing facility - synchronization objects: Variables in memory that are accessed just like data. Threads in different processes can communicate with each other. through synchronization objects. The objects are placed in threads-controlled shared memory. They can have lifetimes beyond that of the creating process. Trifon Ruskov Technical University of Varna 29
30 Threads Synchronization. Mutual Exclusion Locks Mutual exclusion locks (mutexes) are a common method of serializing thread execution. Mutual exclusion locks synchronize threads, usually by ensuring that only one thread at a time executes a critical section of code. A typical sequence in the use of a mutex is as follows: Create and initialize a mutex variable. Several threads attempt to lock the mutex. Only one succeeds and that thread owns the mutex. The owner thread performs some set of actions. The owner unlocks the mutex. Another thread acquires the mutex and repeats the process. Finally the mutex is destroyed. Trifon Ruskov Technical University of Varna 30
31 Mutual Exclusion Locks. Initializing/Destroying a Mutex Two ways of initializing a mutex: Statically, when it is declared. For example: pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; Dynamically. This method permits setting mutex object attributes. int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t *mattr); Parameters: mp pointer to mutex variable. mattr attribute object, used to establish properties for the mutex. Specified as NULL to accept defaults. The mutex is initially unlocked. Destorying a mutex: int pthread_mutex_destroy(pthread_mutex_t *mp); Trifon Ruskov Technical University of Varna 31
32 Mutual Exclusion Locks. Locking/Unlocking a Mutex int pthread_mutex_lock(pthread_mutex_t *mp); The function locks the mutex pointed to by mp. When the mutex is already locked, the calling thread blocks and waits on a prioritized queue. When pthread_mutex_lock() returns, the mutex is locked and the calling thread is the owner. The function returns zero after completing successfully. Any other returned value indicates that an error occurred. int pthread_mutex_unlock(pthread_mutex_t *mp); This function unlocks the mutex pointed to by mp. The mutex must be locked and the calling thread must be the one that last locked the mutex (i.e. the owner). When any other threads are waiting for the mutex to become available, the thread at the head of the queue is unblocked. The function returns zero after completing successfully. Any other returned value indicates that an error occurred. Trifon Ruskov Technical University of Varna 32
33 Example: Mutual Exclusion Locks #include <stdio.h> #include <pthread.h> int count=0; pthread_mutex_t mu; // The mutex variable void *inc_count(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&mu); count++; pthread_mutex_unlock(&mu); if (i%100000==0) printf("+\n"); pthread_exit(null); void *dec_count(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&mu); count--; pthread_mutex_unlock(&mu); if (i%100000==0) printf("-\n"); pthread_exit(null); inc_count mutex count +1-1 dec_count Trifon Ruskov Technical University of Varna 33
34 Example: Mutual Exclusion Locks (cont.) int main(int argc, char* argv[]) { pthread_t t1, t2; // Initialize the mutex pthread_mutex_init(&mu, NULL); pthread_create(&t1, NULL, inc_count, NULL); pthread_create(&t2, NULL, dec_count, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); // Free the mutex resources pthread_mutex_destroy(&mu); printf("main: Done.\n"); pthread_exit(null); Trifon Ruskov Technical University of Varna 34
35 Deadlock A problem: There is a need occasionally to access two resources at once. For instance, one of the resources is in use, and then we discover that the other resource is needed as well. If two threads attempt to claim both resources but lock the associated mutexes in different orders, deadlock happens. Trifon Ruskov Technical University of Varna 35
36 Example: Deadlock void *thread1(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); void *thread2(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&m2); pthread_mutex_lock(&m1); resource1++; if (resource1% ==0) printf("thread1: resource2 %d\n", resource2); pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); pthread_exit(null); resource2++; if (resource2% ==0) printf("thread2: resource1 %d\n", resource1); pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); pthread_exit(null); Trifon Ruskov Technical University of Varna 36
37 Avoiding Deadlock: Using Locking Hierarchies The best way to avoid such deadlock problems is to use lock hierarchies: whenever threads lock multiple mutexes, they do so in the same order. When locks are always taken in a prescribed order, deadlock should not occur. void *thread1(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); void *thread2(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); resource1++; if (resource1% ==0) printf("thread1: resource2 %d\n", resource2); pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); pthread_exit(null); resource2++; if (resource2% ==0) printf("thread2: resource1 %d\n", resource1); pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); pthread_exit(null); Trifon Ruskov Technical University of Varna 37
38 Avoiding Deadlock: Using conditional locking Limitation of using locking hierarchies: Sometimes must take the mutexes in an order other than prescribed. Solution: To prevent deadlock in such a situation, use conditional locking. One thread must release its mutexes when it discovers that deadlock would otherwise be inevitable. int pthread_mutex_trylock(pthread_mutex_t *mp); The function will attempt to lock a mutex mp. However, if the mutex is already locked, the routine will return immediately with a "busy" error code. This routine may be useful in preventing deadlock conditions, as in a priorityinversion situation. Trifon Ruskov Technical University of Varna 38
39 Example: Using conditional locking void *thread1(void* arg) { int i=0; for (i=0;i< ;i++) { pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); resource1++; if (resource1% ==0) printf("thread1: resource2 %d\n", resource2); pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); pthread_exit(null); void *thread2(void* arg) { int i=0; for (i=0;i< ;i++) { // When there is a possibility of // deadlock, unlock the mutex (m2) for (;;) { pthread_mutex_lock(&m2); if (pthread_mutex_trylock(&m1)==0) break; pthread_mutex_unlock(&m2); resource2++; if (resource2% ==0) { printf("thread2: resource1 %d\n", resource1); pthread_mutex_unlock(&m1); pthread_mutex_unlock(&m2); pthread_exit(null); Trifon Ruskov Technical University of Varna 39
40 Threads Synchronization. Condition Variables Condition variables can be used to atomically block threads until a particular condition is true. Condition variables are always used in conjunction with mutex locks: With a condition variable, a thread can atomically block until a condition is satisfied. The condition is tested under the protection of a mutual exclusion lock (mutex). When the condition is false, a thread usually blocks on a condition variable and atomically releases the mutex waiting for the condition to change. When another thread changes the condition, it can signal the associated condition variable to cause one or more waiting threads to wake up, acquire the mutex again, and re-evaluate the condition. Trifon Ruskov Technical University of Varna 40
41 Condition Variables. Initializing/Destroying a Condition Two ways of initializing a condition variable: Statically, when it is declared. For example: pthread_condition_t mycond = PTHREAD_COND_INITIALIZER; Dynamically. This method permits setting condition object attributes. int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *cattr); Parameters: cv pointer to condition variable. cattr - attribute object, used to establish properties for the condition. Specified as NULL to accept defaults. Destorying a condition: int pthread_cond_destroy(pthread_cond_t *cv); Trifon Ruskov Technical University of Varna 41
42 Waiting / Signalling on Condition Variables int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex); The function blocks the calling thread until the specified condition cv is signalled. This routine should be called while mutex mutex is locked, and it will automatically release the mutex while it waits. Should also unlock mutex after a signal has been received. int pthread_cond_signal(pthread_cond_t *cv); The function routine is used to signal (or wake up) another thread which is waiting on condition variable cv. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() routine to complete. int pthread_cond_broadcast(pthread_cond_t *cv); The pthread_cond_broadcast() routine should be used instead of pthread_cond_signal() if more than one thread is in a blocking wait state. Trifon Ruskov Technical University of Varna 42
43 Example: Using Condition Variables // Two threads update a shared resource count. // The third thread waits until count variable reaches a // COUNT_LIMIT value. This thread is waken up by other treads. #include <pthread.h> #include <stdio.h> #define NUM_THREADS 3 #define TCOUNT 10 #define COUNT_LIMIT 12 int count = 0; int thread_ids[3] = {0,1,2; pthread_mutex_t count_mutex; pthread_cond_t count_limit_cv; // The condition variable // Working thread, updating count void *inc_count(void *idp){ int j,i; double result=0.0; int *my_id = idp; Trifon Ruskov Technical University of Varna 43
44 Example: Using Condition Variables (cont.) for (i=0; i < TCOUNT; i++) { pthread_mutex_lock(&count_mutex); count++; // Check the value of count and signal waiting thread // when condition is reached. if (count == COUNT_LIMIT) { pthread_cond_signal(&count_limit_cv); printf("inc_count(): thread %d, count = %d Limit reached.\n", *my_id, count); printf("inc_count(): thread %d, count = %d, unlocking mutex\n", *my_id, count); pthread_mutex_unlock(&count_mutex); // Do some work so threads can work for (j=0; j < ; j++) result = result + (double)random(); pthread_exit(null); // inc_count Trifon Ruskov Technical University of Varna 44
45 Example: Using Condition Variables (cont.) // The waiting thread void *watch_count(void *idp){ int *my_id = idp; printf("starting watch_count(): thread %d\n", *my_id); // Lock mutex and wait for signal. // If COUNT_LIMIT is reached before this routine is run, // the loop will be skipped to prevent pthread_cond_wait // from never returning. pthread_mutex_lock(&count_mutex); while (count < COUNT_LIMIT) { pthread_cond_wait(&count_limit_cv, &count_mutex); printf("watch_count(): thread %d Condition signal received.\n", *my_id); pthread_mutex_unlock(&count_mutex); pthread_exit(null); Trifon Ruskov Technical University of Varna 45
46 Example: Using Condition Variables (cont.) int main (int argc, char *argv[]){ int i, rc; pthread_t threads[3]; // Initialize mutex and condition variable objects pthread_mutex_init(&count_mutex, NULL); pthread_cond_init(&count_limit_cv, NULL); // Create threads in a joinable state pthread_create(&threads[0],null,inc_count,(void*)&thread_ids[0]); pthread_create(&threads[1],null,inc_count,(void*)&thread_ids[1]); pthread_create(&threads[2],null,watch_count,(void*)&thread_ids[2]); for (i = 0; i < NUM_THREADS; i++) //Wait for all threads to complete pthread_join(threads[i], NULL); printf ("Main: Waited on %d threads. Done.\n", NUM_THREADS); // Clean up and exit pthread_mutex_destroy(&count_mutex); pthread_cond_destroy(&count_limit_cv); pthread_exit(null); Trifon Ruskov Technical University of Varna 46
47 Threads Synchronization. Semaphores Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the count atomically, and wait until the counter is non null and decrement it atomically. Initializing the semaphore: int sem_init(sem_t *sem, int pshared, unsigned int value); Parameters: sem pointer to a semaphore object pshared indicates whether the semaphore is local to the current process (zero) or is to be shared between several processes (non-zero). returns: 0 on success -1 on error (errno is set) This function initialized the semaphore object pointed by sem. The count associated with the semaphore is set initially to value. Trifon Ruskov Technical University of Varna 47
48 int sem_wait(sem_t *sem); Semaphores Operations The function suspends the calling thread until semaphore pointed to by sem has non-zero count. It then atomically decreases the semaphore count. int sem_trywait(sem_t *sem); The function is non-blocking variant of sem_wait(). If semaphore pointed to by sem has non-zero count, the count is atomically decreased and sem_trywait() immediately returns 0. If semaphore count is zero, the function immediately returns with error. int sem_post(sem_t *sem); The function atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be used in asynchronous signal handlers. int sem_getvalue(sem_t *sem, int *sval); The function stores in the location pointed to by sval the current count of the semaphore sem. Trifon Ruskov Technical University of Varna 48
49 Example: Using Semaphores #include <sys/types.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <signal.h> #define NUMTHREADS 5 sem_t semaphore; // The semaphore object // Thread routine to wait on a semaphore. void *sem_waiter (void *arg){ int *id = (long)arg; printf("thread %d waiting\n", *id); sem_wait(&semaphore); printf("thread %d resuming\n", *id); pthread_exit(null); Trifon Ruskov Technical University of Varna 49
50 Example: Using Semaphores (cont.) int main (int argc, char *argv[]){ int thread_count; pthread_t sem_waiters[numthreads]; int status; // Initialize the semaphores if (sem_init(&semaphore, 0, 0) == -1) { perror ("Init semaphore"); exit(1); // Create 5 threads to wait concurrently on the semaphore. for (thread_count=0; thread_count<numthreads; thread_count++) { status = pthread_create(&sem_waiters[thread_count], NULL, sem_waiter, (void*)thread_count); if (status!= 0) { perror("create thread"); exit(1); Trifon Ruskov Technical University of Varna 50
51 Example: Using Semaphores (cont.) //"Broadcast" the semaphore by repeatedly posting until // the count of waiters goes to 0. while (1) { int sem_value; if (sem_getvalue(&semaphore, &sem_value) == -1) { perror("get semaphore value"); exit(1); if (sem_value > 0) break; printf ("Posting from main: %d\n", sem_value); if (sem_post(&semaphore) == -1) { perror("post semaphore"); exit(1); //Wait for all threads to complete. for (thread_count=0; thread_count<numthreads; thread_count++) { status = pthread_join(sem_waiters[thread_count], NULL); if (status!= 0) { perror("join thread"); exit(1); return 0; Trifon Ruskov Technical University of Varna 51
Process Synchronization
Process Synchronization Part III, Modified by M.Rebaudengo - 2013 Silberschatz, Galvin and Gagne 2009 POSIX Synchronization POSIX.1b standard was adopted in 1993 Pthreads API is OS-independent It provides:
More informationPOSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff
POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system
More informationCS333 Intro to Operating Systems. Jonathan Walpole
CS333 Intro to Operating Systems Jonathan Walpole Threads & Concurrency 2 Threads Processes have the following components: - an address space - a collection of operating system state - a CPU context or
More informationPthreads: POSIX Threads
Shared Memory Programming Using Pthreads (POSIX Threads) Lecturer: Arash Tavakkol arasht@ipm.ir Some slides come from Professor Henri Casanova @ http://navet.ics.hawaii.edu/~casanova/ and Professor Saman
More informationCS510 Operating System Foundations. Jonathan Walpole
CS510 Operating System Foundations Jonathan Walpole Threads & Concurrency 2 Why Use Threads? Utilize multiple CPU s concurrently Low cost communication via shared memory Overlap computation and blocking
More informationPOSIX Threads. HUJI Spring 2011
POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time
More informationCS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University
CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),
More informationCS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University
CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution
More informationUnix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology
Unix Threads & Concurrency Erick Fredj Computer Science The Jerusalem College of Technology 1 Threads Processes have the following components: an address space a collection of operating system state a
More informationAgenda. Process vs Thread. ! POSIX Threads Programming. Picture source:
Agenda POSIX Threads Programming 1 Process vs Thread process thread Picture source: https://computing.llnl.gov/tutorials/pthreads/ 2 Shared Memory Model Picture source: https://computing.llnl.gov/tutorials/pthreads/
More informationPOSIX Threads. Paolo Burgio
POSIX Threads Paolo Burgio paolo.burgio@unimore.it The POSIX IEEE standard Specifies an operating system interface similar to most UNIX systems It extends the C language with primitives that allows the
More informationreal time operating systems course
real time operating systems course 4 introduction to POSIX pthread programming introduction thread creation, join, end thread scheduling thread cancellation semaphores thread mutexes and condition variables
More informationCS510 Operating System Foundations. Jonathan Walpole
CS510 Operating System Foundations Jonathan Walpole The Process Concept 2 The Process Concept Process a program in execution Program - description of how to perform an activity instructions and static
More informationThread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono
Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo References The material in this set of slide is taken from two tutorials by Blaise Barney from the Lawrence Livermore National
More informationSynchronization Primitives
Synchronization Primitives Locks Synchronization Mechanisms Very primitive constructs with minimal semantics Semaphores A generalization of locks Easy to understand, hard to program with Condition Variables
More informationThreads. lightweight processes
Threads lightweight processes 1 Motivation Processes are expensive to create. It takes quite a bit of time to switch between processes Communication between processes must be done through an external kernel
More informationCS533 Concepts of Operating Systems. Jonathan Walpole
CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?
More informationCS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization
CS-345 Operating Systems Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization Threads A thread is a lightweight process A thread exists within a process and uses the process resources. It
More informationProcesses, Threads, SMP, and Microkernels
Processes, Threads, SMP, and Microkernels Slides are mainly taken from «Operating Systems: Internals and Design Principles, 6/E William Stallings (Chapter 4). Some materials and figures are obtained from
More informationSynchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1
Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary
More informationLecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005
Threads vs. Processes Lecture 4 Thread Programming January 21, 2005 fork() is expensive (time, memory) Interprocess communication is hard. Threads are lightweight processes: one process can contain several
More informationThreads. Jo, Heeseung
Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU
More informationANSI/IEEE POSIX Standard Thread management
Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The
More informationSynchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1
Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary
More informationThreads. Threads (continued)
Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state
More informationPthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Pthreads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Pthreads API ANSI/IEEE POSIX1003.1-1995 Standard Thread management Work directly on
More informationThreads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University
Threads Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Concurrency
More informationPreview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017
Preview What are Pthreads? What is Pthreads The Thread ID The Thread Creation The thread Termination The pthread_join() function Mutex The pthread_cancel function The pthread_cleanup_push() function The
More informationDefinition Multithreading Models Threading Issues Pthreads (Unix)
Chapter 4: Threads Definition Multithreading Models Threading Issues Pthreads (Unix) Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads 1 Thread A Unix process (heavy-weight process HWP)
More informationMultithreading Programming II
Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure
More informationChapter 4 Concurrent Programming
Chapter 4 Concurrent Programming 4.1. Introduction to Parallel Computing In the early days, most computers have only one processing element, known as the Central Processing Unit (CPU). Due to this hardware
More informationThread and Synchronization
Thread and Synchronization pthread Programming (Module 19) Yann-Hang Lee Arizona State University yhlee@asu.edu (480) 727-7507 Summer 2014 Real-time Systems Lab, Computer Science and Engineering, ASU Pthread
More informationLecture 19: Shared Memory & Synchronization
Lecture 19: Shared Memory & Synchronization COMP 524 Programming Language Concepts Stephen Olivier April 16, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Forking int pid;
More informationCSE 306/506 Operating Systems Threads. YoungMin Kwon
CSE 306/506 Operating Systems Threads YoungMin Kwon Processes and Threads Two characteristics of a process Resource ownership Virtual address space (program, data, stack, PCB ) Main memory, I/O devices,
More informationReal Time Operating Systems and Middleware
Real Time Operating Systems and Middleware POSIX Threads Synchronization Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Threads Synchronisation All the threads running in
More informationIntroduction to PThreads and Basic Synchronization
Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization
More informationThreads need to synchronize their activities to effectively interact. This includes:
KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 8 Threads Synchronization ( Mutex & Condition Variables ) Objective: When multiple
More informationCS240: Programming in C. Lecture 18: PThreads
CS240: Programming in C Lecture 18: PThreads Thread Creation Initially, your main() program comprises a single, default thread. pthread_create creates a new thread and makes it executable. This routine
More informationConcurrent Server Design Multiple- vs. Single-Thread
Concurrent Server Design Multiple- vs. Single-Thread Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN NTUT, TAIWAN 1 Examples Using
More informationPOSIX PTHREADS PROGRAMMING
POSIX PTHREADS PROGRAMMING Download the exercise code at http://www-micrel.deis.unibo.it/~capotondi/pthreads.zip Alessandro Capotondi alessandro.capotondi(@)unibo.it Hardware Software Design of Embedded
More informationThreads. Jo, Heeseung
Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU
More informationLocks and semaphores. Johan Montelius KTH
Locks and semaphores Johan Montelius KTH 2018 1 / 40 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;
More informationTHREADS. Jo, Heeseung
THREADS Jo, Heeseung TODAY'S TOPICS Why threads? Threading issues 2 PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files)
More informationThreads. studykorner.org
Threads Thread Subpart of a process Basic unit of CPU utilization Smallest set of programmed instructions, can be managed independently by OS No independent existence (process dependent) Light Weight Process
More informationCS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2
CS345 Opera,ng Systems Φροντιστήριο Άσκησης 2 Inter- process communica0on Exchange data among processes Methods Signals Pipes Sockets Shared Memory Sockets Endpoint of communica,on link between two programs
More informationNetwork Programming TDC 561
Network Programming TDC 561 Lecture # 6: Multithreaded Servers Dr. Ehab S. Al-Shaer School of Computer Science & Telecommunication DePaul University Chicago, IL 1 Introduction to Multithreaded Network
More informationCSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017
CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming
More informationCS 3305 Intro to Threads. Lecture 6
CS 3305 Intro to Threads Lecture 6 Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer Introduction Applications often need to perform
More informationParallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai
Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai School of Computer Science and Technology Huazhong University of Science and Technology Thread Basics! All memory in the
More informationPOSIX Threads Programming
Tutorials Exercises Abstracts LC Workshops Comments Search Privacy & Legal Notice POSIX Threads Programming Table of Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. Pthreads Overview 1. What is a Thread? 2. What are
More informationpthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)
pthreads 1 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) 2 1 Thread Packages Kernel thread packages Implemented and supported at kernel
More informationPthread (9A) Pthread
Pthread (9A) Pthread Copyright (c) 2012 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later
More informationLocks and semaphores. Johan Montelius KTH
Locks and semaphores Johan Montelius KTH 2017 1 / 41 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;
More informationComputer Systems Laboratory Sungkyunkwan University
Threads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Today s Topics Why threads? Threading issues 2 Processes Heavy-weight A process includes
More informationPre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012
Pre-lab #2 tutorial ECE 254 Operating Systems and Systems Programming May 24, 2012 Content Concurrency Concurrent Programming Thread vs. Process POSIX Threads Synchronization and Critical Sections Mutexes
More informationrecap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a
recap, what s the problem Locks and semaphores Johan Montelius KTH 2017 # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++; int main
More informationCS Operating Systems: Threads (SGG 4)
When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate
More informationCS 5523 Operating Systems: Thread and Implementation
When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate
More informationCPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu
CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting
More informationCSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010
CSC 4304 - Systems Programming Fall 2010 Lecture - XIV Concurrent Programming Tevfik Ko!ar Louisiana State University November 2nd, 2010 1 Concurrency Issues 2 Concurrency Issues 3 Synchronization Mechanism
More informationCS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives
CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look
More informationMultithreaded Programming
Multithreaded Programming The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. September 4, 2014 Topics Overview
More informationCS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5
CS 153 Lab4 and 5 Kishore Kumar Pusukuri Outline Introduction A thread is a straightforward concept : a single sequential flow of control. In traditional operating systems, each process has an address
More informationRicardo Rocha. Department of Computer Science Faculty of Sciences University of Porto
Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto For more information please consult Advanced Programming in the UNIX Environment, 3rd Edition, W. Richard Stevens and
More informationCSE 333 SECTION 9. Threads
CSE 333 SECTION 9 Threads HW4 How s HW4 going? Any Questions? Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts
More informationMultithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4
Multithreading Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4 Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003. EEL 602 1 Outline Process and Threads Multithreading
More informationPOSIX Semaphores. Operations on semaphores (taken from the Linux man page)
POSIX Semaphores A variable of type sem_t Example Declaration of a semaphore sem_t sem; Operations on semaphores (taken from the Linux man page) int sem_init(sem_t *sem, int pshared, unsigned int value);
More informationConcurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018
Concurrency and Synchronization ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Concurrency Multiprogramming Supported by most all current operating systems More than one unit of
More informationSynchronization Mechanisms
Synchronization Mechanisms CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra Mutex Locks Enforce protection and mutual exclusion Condition variables Allow atomic checking of conditions
More informationCOSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References
COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h
More informationThread. Disclaimer: some slides are adopted from the book authors slides with permission 1
Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast
More informationOutline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.
Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter
More informationHPCSE - I. «Introduction to multithreading» Panos Hadjidoukas
HPCSE - I «Introduction to multithreading» Panos Hadjidoukas 1 Processes and Threads POSIX Threads API Outline Thread management Synchronization with mutexes Deadlock and thread safety 2 Terminology -
More informationThreads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits
CS307 What is a thread? Threads A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process
More informationOct 2 and 4, 2006 Lecture 8: Threads, Contd
Oct 2 and 4, 2006 Lecture 8: Threads, Contd October 4, 2006 1 ADMIN I have posted lectures 3-7 on the web. Please use them in conjunction with the Notes you take in class. Some of the material in these
More informationTCSS 422: OPERATING SYSTEMS
TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread
More informationPosix Threads (Pthreads)
Posix Threads (Pthreads) Reference: Programming with POSIX Threads by David R. Butenhof, Addison Wesley, 1997 Threads: Introduction main: startthread( funk1 ) startthread( funk1 ) startthread( funk2 )
More informationOperating System Labs. Yuanbin Wu
Operating System Labs Yuanbin Wu CS@ECNU Operating System Labs Project 4 Due: 10 Dec. Oral tests of Project 3 Date: Nov. 27 How 5min presentation 2min Q&A Operating System Labs Oral tests of Lab 3 Who
More informationCSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks
CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data
More informationpthreads CS449 Fall 2017
pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate
More informationAbout me Now that you know the pthread API
pthread Examples About me Now that you know the pthread API How do you create threads? How do you pass different values to them? How do you return values from threads? What are some common mistakes? Friday:
More informationCSci 4061 Introduction to Operating Systems. (Threads-POSIX)
CSci 4061 Introduction to Operating Systems (Threads-POSIX) How do I program them? General Thread Operations Create/Fork Allocate memory for stack, perform bookkeeping Parent thread creates child threads
More informationExercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads
Exercise (could be a quiz) 1 2 Solution CSE 421/521 - Operating Systems Fall 2013 Lecture - IV Threads Tevfik Koşar 3 University at Buffalo September 12 th, 2013 4 Roadmap Threads Why do we need them?
More informationHigh Performance Computing Course Notes Shared Memory Parallel Programming
High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed
More informationShared Memory Programming Models III
Shared Memory Programming Models III Stefan Lang Interdisciplinary Center for Scientific Computing (IWR) University of Heidelberg INF 368, Room 532 D-69120 Heidelberg phone: 06221/54-8264 email: Stefan.Lang@iwr.uni-heidelberg.de
More informationParallel Programming with Threads
Thread Programming with Shared Memory Parallel Programming with Threads Program is a collection of threads of control. Can be created dynamically, mid-execution, in some languages Each thread has a set
More informationCSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...
CSE 421/521 - Operating Systems Fall 2011 Recitations Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar Presented by... University at Buffalo September..., 2011 1 Network Programming
More informationIntroduc)on to pthreads. Shared memory Parallel Programming
Introduc)on to pthreads Shared memory Parallel Programming pthreads Hello world Compute pi pthread func)ons Prac)ce Problem (OpenMP to pthreads) Sum and array Thread-safe Bounded FIFO Queue pthread Hello
More informationIntroduction to parallel computing
Introduction to parallel computing Shared Memory Programming with Pthreads (3) Zhiao Shi (modifications by Will French) Advanced Computing Center for Education & Research Vanderbilt University Last time
More informationCS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization
CS 345 Operating Systems Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization Assignment 2 We have a treasure room, Team A and Team B. Treasure room has N coins inside. Each team
More informationProgramming with Shared Memory. Nguyễn Quang Hùng
Programming with Shared Memory Nguyễn Quang Hùng Outline Introduction Shared memory multiprocessors Constructs for specifying parallelism Creating concurrent processes Threads Sharing data Creating shared
More informationWarm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?
Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD
More informationCarnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition
Synchronization: Basics 53: Introduction to Computer Systems 4 th Lecture, November 6, 7 Instructor: Randy Bryant Today Threads review Sharing Mutual exclusion Semaphores 3 Traditional View of a Process
More informationChapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:
SFWR ENG 3BB4 Software Design 3 Concurrent System Design 2 SFWR ENG 3BB4 Software Design 3 Concurrent System Design 10.11 13 OS Processes: Control and Resources Chapter 10 Threads A process as managed
More informationThreads Chapter 5 1 Chapter 5
Threads Chapter 5 1 Chapter 5 Process Characteristics Concept of Process has two facets. A Process is: A Unit of resource ownership: a virtual address space for the process image control of some resources
More informationThreads. CS3026 Operating Systems Lecture 06
Threads CS3026 Operating Systems Lecture 06 Multithreading Multithreading is the ability of an operating system to support multiple threads of execution within a single process Processes have at least
More informationOperating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam
95.300 Operating Systems Threads and Signals Amir Ghavam Winter 2002 1 Traditional Process Child processes created from a parent process using fork Drawbacks Fork is expensive: Memory is copied from a
More informationSynchronization: Basics
Synchronization: Basics CS 485G6: Systems Programming Lecture 34: 5 Apr 6 Shared Variables in Threaded C Programs Question: Which variables in a threaded C program are shared? The answer is not as simple
More informationResource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14
Resource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14 Lecture Outline Resources access control (cont d): Enhancing the priority ceiling protocol Stack-based priority ceiling protocol
More informationECE 598 Advanced Operating Systems Lecture 23
ECE 598 Advanced Operating Systems Lecture 23 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 April 2016 Don t forget HW#9 Midterm next Thursday Announcements 1 Process States
More information