Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.

Similar presentations


Presentation on theme: "Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process."— Presentation transcript:

1 Unix Internals Concurrent Programming

2 Unix Processes Processes contain information about program resources and program execution state, including: Process ID, process group ID, user ID, and group ID Environment Working directory. Program instructions Registers Stack Heap File descriptors Signal actions Shared libraries Inter-process communication tools (such as message queues, pipes, semaphores, threads, or shared memory).

3 THREADS WITHIN A UNIX PROCESS UNIX PROCESS

4 Where the Thread Reside Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code. This independent flow of control is accomplished because a thread maintains its own: – Stack pointer – Registers – Scheduling properties (such as policy or priority) – Set of pending and blocked signals – Thread specific data.

5 That is in UNIX environment a thread: Exists within a process and uses the process resources Has its own independent flow of control as long as its parent process exists and the OS supports it Duplicates only the essential resources it needs to be independently schedulable May share the process resources with other threads that act equally independently (and dependently) Dies if the parent process dies - or something similar Is "lightweight" because most of the overhead has already been accomplished through the creation of its process.

6 Threads within the same process Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data. Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer.

7 Designing Threaded Programs On multi-core machines, pthreads (POSIX Threads) are ideally suited for parallel programming, and whatever applies to parallel programming in general, applies to parallel pthreads programs. There are many considerations for designing parallel programs, such as: – What type of parallel programming model to use? – Problem partitioning – Load balancing – Communications – Data dependencies – Synchronization and race conditions – Memory issues – I/O issues – Program complexity – Programmer effort/costs/time – etc

8 Concurrent Code Organization Threads are used to organize a program into discrete, independent tasks which can execute concurrently.

9 What type of Programs may be suited for pthreads Work that can be executed, or data that can be operated on, by multiple tasks simultaneously: Block for potentially long I/O waits Use many CPU cycles in some places but not others Must respond to asynchronous events priority interrupts: Some work is more important than other work.

10 Models for threaded programs Manager/worker: 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. At least 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 sub-operations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model. Peer: similar to the manager/worker model, but after the main thread creates other threads, it participates in the work.

11 Shared Memory Model All threads have access to the same global, shared memory Threads also have their own private data Programmers are responsible for synchronizing access (protecting) globally shared data.

12 Thread-safeness Thread-safeness: in a nutshell, refers an application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions. Suppose that your application creates several threads, each making call to the same library routine: – This library routine accesses and modifies a global structure or a certain location in memory. – As each thread calls this routine it is possible that they may try to modify this global structure/memory location at the same time. – If the routine does not employ some sort of synchronization mechanisms to prevent data corruption, then it is not thread-safe.

13 Accessing Common Resources Be careful if your application uses libraries or other objects that don't explicitly guarantee thread-safeness. When in doubt, assume that they are not thread-safe until proven otherwise. This can be done by "serializing" the calls to the uncertain routine, etc.

14 Thread Limitations Although the Pthreads API is an ANSI/IEEE standard, implementations can, and usually do, vary in ways not specified by the standard. Because of this, a program that runs fine on one platform, may fail or produce wrong results on another platform. For example, the maximum number of threads permitted, and the default thread stack size are two important parameters to consider when designing a program. See ulimit utility of linux.

15 The Pthreads API The subroutines which comprise the Pthreads API can be informally grouped into four major groups: Thread management: Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.) Mutexes: Routines that deal with synchronization, called a mutex ("mutual exclusion"). Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes. Condition variables: Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included. Synchronization: Routines that manage read/write locks and barriers.

16 Naming conventions All identifiers in the threads library begin with pthread_. Routine PrefixFunctional Group pthread_ Threads themselves and miscellaneous subroutines pthread_attr_Thread attributes objects pthread_mutex_Mutexes pthread_mutexattr_Mutex attributes objects. pthread_cond_Condition variables pthread_condattr_Condition attributes objects pthread_key_Thread-specific data keys pthread_rwlock_Read/write locks pthread_barrier_Synchronization barriers

17 Thread Management Deals with creating and terminating threads Library routines: – pthread_create (thread,attr,start_routine,arg) pthread_exit (status) pthread_create pthread_exit – pthread_cancel (thread) pthread_cancel – pthread_attr_init (attr) pthread_attr_init – pthread_attr_destroy (attr) pthread_attr_destroy

18 Creating Threads Initially, your main() program comprises a single, default thread. All other threads must be explicitly created by the programmer. pthread_create creates a new thread and makes it executable. This routine can be called any number of times from anywhere within your code. pthread_create arguments: – thread: An opaque, unique identifier for the new thread returned by the subroutine. – attr: An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values. – start_routine: the C routine that the thread will execute once it is created. – arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.

19 Threads may create other threads.

20 Thread Attributes pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the thread attribute object. Other routines are then used to query/set specific attributes in the thread attribute object. Attributes include: – Detached or joinable state – Scheduling inheritance – Scheduling policy – Scheduling parameters – Scheduling contention scope – Stack size – Stack address – Stack guard (overflow) size

21 Thread Binding and Scheduling a) when a thread will be scheduled to run by the operating system, and b) which processor/core it will run on? – threads can be scheduled to run FIFO (first-in first-out), – RR (round-robin), or – OTHER (operating system determines). It also provides the ability to set a thread's scheduling priority value. – See sched_setscheduler man page on Linuxsched_setscheduler

22 Terminating Threads & pthread_exit() When its task is done the thread returns normally from its starting routine. The thread makes a call to the pthread_exit subroutine - whether its work is done or not. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to making a call to either the exec() or exit() If main() finishes first, without calling pthread_exit explicitly itself The pthread_exit() routine allows the programmer to specify an optional termination status parameter. This optional parameter is typically returned to threads "joining" the terminated thread. See int pthread_join(pthread_t tid, void **status)

23 Example Code

24 Result of the example codee

25 Passing Arguments to Threads The pthread_create() allows the programmer to pass one argument to the thread start routine. Passing multiple arguments can be done by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create(). All arguments must be passed by reference and cast to (void *).

26 Example: Argument passing See thread_arg_pass_2.c

27 Code Output

28 Joining and Detaching Threads pthread_join (threadid,status) pthread_detach (threadid) pthread_join pthread_detach pthread_attr_setdetachstate (attr,detachstate) pthread_attr_setdetachstate pthread_attr_getdetachstate (attr,detachstate) pthread_attr_getdetachstate

29 Joining "Joining" is one way to accomplish synchronization between threads. For example: The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit(). A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread.

30 Joinable or Not? When a thread is created, one of its attributes defines whether it is joinable or detached. Only threads that are created as joinable can be joined. To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() is used. The required steps are: – Declare a pthread attribute variable of the pthread_attr_t data type – Initialize the attribute variable with pthread_attr_init() – Set the attribute detached status with pthread_attr_setdetachstate() – When done, free library resources used by the attribute with pthread_attr_destroy()

31 Miscellaneous Routines pthread_self () : returns the unique, system assigned thread ID of the calling thread. pthread_self pthread_equal (thread1,thread2) : compares two thread IDs. If the two IDs are different 0 is returned, otherwise a non-zero value is returned. pthread_equal pthread_once (once_control, init_routine): executes the init_routine exactly once in a process pthread_once – The first call to this routine by any thread in the process executes the given init_routine, without parameters. Any subsequent call will have no effect. – The once_control parameter is a synchronization control structure that requires initialization prior to calling pthread_once, e.g., pthread_once_t once_control = PTHREAD_ONCE_INIT;

32 Thread Synchronization Mutex ( (“mutual exclusion”)

33 Mutex Variables Mutex variables are one of the primary means of implementing thread synchronization and for protecting shared data when multiple writes occur. A mutex variable acts like a "lock" protecting access to a shared data resource. even if several threads try to lock a mutex only one thread will be successful. Mutexes can be used to prevent "race" conditions when accessing critical sections.

34 A sequence in the use of a mutex 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

35 Creating and Destroying Mutexes pthread_mutex_init (mutex,attr) pthread_mutex_init pthread_mutex_destroy (mutex) pthread_mutex_destroy pthread_mutexattr_init (attr) pthread_mutexattr_init pthread_mutexattr_destroy (attr) pthread_mutexattr_destroy Mutex variables must be declared with type pthread_mutex_t, and must be initialized before they can be used. There are two ways to initialize a mutex variable: 1.Statically, when it is declared, e.g., pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; 2.Dynamically, with the pthread_mutex_init(), this method permits setting mutex object attributes, attr.

36 mutex attributes The attr object is used to establish properties for the mutex object, and must be of type pthread_mutexattr_t if used (may be specified as NULL to accept defaults). The Pthreads standard defines three optional mutex attributes: 1. Protocol: Specifies the protocol used to prevent priority inversions for a mutex. 2.Prioceiling: Specifies the priority ceiling of a mutex. 3.Process-shared: Specifies the process sharing of a mutex. The pthread_mutexattr_init() and pthread_mutexattr_destroy() routines are used to create and destroy mutex attribute objects respectively. pthread_mutex_destroy() should be used to free a mutex object which is no longer needed.

37 Locking and Unlocking Mutexes pthread_mutex_lock (mutex): is used by a thread to acquire a lock on the specified mutex variable. If the mutex is already locked by another thread, this call will block the calling thread until the mutex is unlocked. pthread_mutex_lock pthread_mutex_trylock (mutex): will attempt to lock a mutex. 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 priority-inversion situation. pthread_mutex_trylock pthread_mutex_unlock (mutex): will unlock a mutex if called by the owning thread. Calling this routine is required after a thread has completed its use of protected data if other threads are to acquire the mutex for their work with the protected data. An error will be returned if: pthread_mutex_unlock – If the mutex was already unlocked – If the mutex is owned by another thread

38 Example: Dot product serial version This code illustrates the use of mutex variables in a multi-threaded program that performs a dot product. The main data is made available to all threads through a globally accessible structure. Each thread works on a different part of the data. The main thread waits for all the threads to complete their computations, and then it prints the resulting sum. (dotprod_serial.c, dotprod_mutex.c, )

39 Condition Variables Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling threads access to data, condition variables allow threads to synchronize based upon the actual value of data. Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling. A condition variable is always used in conjunction with a mutex lock.

40 Using condition variables Main Thread: 1. Declare and initialize global data/variables which require synchronization (such as "count") 2.Declare and initialize a condition variable object 3.Declare and initialize an associated mutex 4.Create threads A and B to perform the task.

41 Condition variables with two tasks Thread A Do work up to the point where a certain condition must occur (such as "count" must reach a specified value) Lock associated mutex and check value of a global variable Call pthread_cond_wait() to perform a blocking wait for signal from Thread-B. Note that a call to pthread_cond_wait() automatically and atomically unlocks the associated mutex variable so that it can be used by Thread-B. When signalled, wake up. Mutex is automatically and atomically locked. Explicitly unlock mutex Continue Thread B Do work Lock associated mutex Change the value of the global variable that Thread-A is waiting upon. Check value of the global Thread-A wait variable. If it fulfills the desired condition, signal Thread-A. Unlock mutex. Continue

42 Creating and Destroying Condition Variables pthread_cond_init (condition,attr) initialize a condition var and attribute object pthread_cond_init pthread_cond_destroy (condition) used to free a condition variable that is no longer neededpthread_cond_destroy pthread_condattr_init (attr) is used to create condition variable attribute objects pthread_condattr_init pthread_condattr_destroy (attr) is used to destroy condition variable attribute objects pthread_condattr_destroy

43 Waiting and Signaling on Condition Variables pthread_cond_wait(condition, mutex) blocks the calling thread until the specified condition is signalled. This routine should be called while mutex is locked, and it will automatically release the mutex while it waits. After signal is received and thread is awakened, mutex will be automatically locked for use by the thread. The programmer is then responsible for unlocking mutex when the thread is finished with it. pthread_cond_signal(condition) is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() to complete. The pthread_cond_broadcast(condition) should be used instead of pthread_cond_signal(condition) if more than one thread is in a blocking wait state.

44 Using Condition Variables This simple example code demonstrates the use of several Pthread condition variable routines. The main routine creates three threads. Two of the threads perform work and update a "count" variable. The third thread waits until the count variable reaches a specified value. See, condvar.c


Download ppt "Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process."

Similar presentations


Ads by Google