Lecture 7 : Multithread programming

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

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.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
The University of Adelaide, School of Computer Science
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
1 Confidential Enterprise Solutions Group Process and Threads.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
Lecture Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management.
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.
Chapter 4 – Thread Concepts
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
OPERATING SYSTEM CONCEPT AND PRACTISE
Realizing Concurrency using the thread model
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Multithreading Tutorial
Threads and Cooperation
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Realizing Concurrency using the thread model
CSCE 513 Computer Architecture
CS510 Operating System Foundations
Chapter 05. Multithread.
Pthread Prof. Ikjun Yeom TA – Mugyo
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Concurrency, Processes and Threads
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Lecture 7 : Multithread programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011학년도 2학기

1. Concept Parallelism: Multiple computations are done simultaneously. Instruction level (pipelining) Data parallelism Task parallelism (embarrassingly parallel) Concurrency: Multiple computations that may be done in parallel.

1. Concept A process with two threads of execution on a single processor. On a single processor, multithreading generally occurs by time-division multiplexing (as in multitasking): the processor switches between different threads.

Process vs. Threads Process: An instance of a program that is being executed in its own address space. In POSIX systems, each process maintains its own heap, stack, registers, file descriptors etc. Communication: Shared memory Network Pipes, Queues Thread: A light weight process that shares its address space with others. In POSIX systems, each thread maintains the bare essentials: registers, stack, signals. shared address space. Note: POSIX (Portable Operating System Interface) The application programming interface (API) for the Unix operating system, which is specified by the IEEE.

Process vs. Threads Threads differ from traditional multitasking operating system processes in that: processes are typically independent, while threads exist as subsets of a process processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources processes have separate address spaces, whereas threads share their address space processes interact only through system-provided inter-process communication mechanisms. Context switching between threads in the same process is typically faster than context switching between processes.

Multithreaded Concurrency Serial execution: All our programs so far has had a single thread of execution: main thread. Program exits when the main thread exits. Multithreaded: Program is organized as multiple and concurrent threads of execution. The main thread spawns multiple threads. The thread may communicate with one another. Advantages: Improves performance Improves responsiveness Improves utilization less overhead compared to multiple processes

Designing Threaded Programs Parallel Programming 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 ...

Designing Threaded Programs Several common modes for threaded programs exist: 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 suboperations, 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.

Designing Threaded Programs 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.

Designing Threaded Programs 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. For example, suppose that your application creates several threads, each of which makes a call to the same library routine: This library routine accesses/modifies a global structure or 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 constructs to prevent data corruption, then it is not thread-safe.

Designing Threaded Programs

2. Windows Multithreaded Programming In C or C++ the program entry point is main or wmain (Unicode version). In windows application the program starts in WinMain or wWinMain. When the program starts, the operating system creates the first thread. Because, Windows is a multitasking operating system.

Thread function Thread function is like an ordinary function with long void pointer parameter. We can pass any data through void pointer data type. A simple thread function looks like void ThreadFunction(void *param) { // do somthing }

2.1 C Run Time Library Multithreading All the above c run time library functions are in the process.h header file. Make sure the Microsoft Visual Studio project setting is as multithreaded DLL. Note: Microsoft recommend that If you use _beginthread functions for C run Time Library you can not use Win32 API like ExitThread or CreateThread. Because, if use that method it might result in deadlocks. Function Description create threads _beginthread use _endthread _beginthreadex some addition parameters like security and thread address. end the thread _endthread It closes thread handle automatically. _endthreadex We closes the thread handle using CloseHandle Win32 API function.

2.1 C Run Time Library Multithreading unsigned long _beginthread( void( __cdecl *start_address )( void * ), unsigned stack_size, void *arglist ); start_address: Start address of routine that begins execution of new thread stack_size: Stack size for new thread or 0 arglist: Argument list to be passed to new thread or NULL If successful, each of these functions returns a handle to the newly created thread. _beginthread returns –1 on an error void _endthread( void );

2.1 C Run Time Library Multithreading Project Settings: Make sure the Microsoft Visual Studio project setting is as multithreaded DLL.

Example #include <stdio.h> #include <windows.h> #include <process.h> /* _beginthread, _endthread */ void ThreadProc(void *param) { int h=*((int*)param); printf("%d Thread is Running!\n", h); _endthread(); } int main() int n, i; int val = 0; HANDLE handle; printf("Enter the number of threads : "); scanf("%d",&n); for(i=0; i<n; i++) val = i; handle = (HANDLE) _beginthread(ThreadProc, 0, &val); WaitForSingleObject(handle, INFINITE); return 0;

2.2 MFC Multithreaded The CWinThread is the base class for all thread operations. The MFC class hierarchy CObject::CCmdTarget::CWinThread::CWinApp Data members m_hThread The current thread handle m_bAutoDelete Whether the thread has set to auto delete or not m_nThreadID The current thread ID Data Functions: CreateThread Start the exec execution of thread SuspendThread Increment thread suspend count. When the resume thread occur only the thread resume. ResumeThread Resume the thread. Decrements the thread count stack SetThreadPriority Set thread priority ( LOW, BELOW LOW or HIGH) GetThreadPriority Get the thread Priority

2.2 MFC Multithreaded Not all the member functions in MFC as class members. We can access some functions globally also. These functions begin with Afx. CWinThread* AfxBeginThread( AFX_THREADPROC ThreadProc, LPVOID Param, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); void AfxEndThread( UINT nExitCode );

Example CwinThread *pThread = AfxBeginThread( ThreadFunction, &data); UINT ThreadFunction(LPVOID param) { DWORD result =0 ; // do somethig AfxEndThread(exitCode); return result; }

2.3 Win32 Multithreaded Win32 Thread created by CreateThread function. The syntax in CreateThread is following We terminate thread using the following methods. 1.use TerminateThread function 2.use ExitThread function 3.use return But, Advanced Windows by Jeffery Ritcher recommends us to use the return method. HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);

3. POSIX Multithreaded Programming The original Pthreads API was defined in the ANSI/IEEE POSIX 1003.1 - 1995 standard. Pthreads API can be informally grouped into four major groups: Thread management: Routines that work directly on threads - creating, detaching, joining, etc. Mutexes (mutual exclusion): Routines that deal with synchronization. Mutex functions provide for creating, destroying, locking and unlocking mutexes. Condition variables: Routines that address communications between threads that share a mutex. 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.

3. POSIX Multithreaded Programming API All identifiers in the threads library begin with pthread_. Some examples are shown below. Compile #include <pthread.h> gcc −Wall −O0 −o <output> file.c −pthread (no −l prefix) Routine Prefix Thread management pthread_ Mutexes pthread_mutex_ Condition variables pthread_cond_ Synchronization pthread_rwlock_, pthread_barrier_

3. POSIX Multithreaded Programming int pthread_create ( pthread_t thread, const pthread _attr _t attr, void ∗( ∗ start _routine )( void ∗), void arg ); creates a new thread with the attributes specified by attr. Default attributes are used if attr is NULL. On success, stores the thread it into thread calls function start_routine(arg) on a separate thread of execution. returns zero on success, non-zero on error. void pthread_exit( void∗ value_ptr); called implicitly when thread function exits. analogous to exit().

Example #include <pthread.h> #include <stdio.h> void *ThreadProc(void *param) { int val; val = (int)param; printf(“%d Thread is Running!\n", val); pthread_exit(NULL); } int main() pthread_t threads[5]; int i, reault; for(i=0; i<5; i++){ result = pthread_create(&threads[i], NULL, ThreadProc, (void *)i); if (result){ printf(“error code = %d\n", result); exit(-1);

Example: Mutex Witout Mutex Routine Prefix int counter=0; void *ThreadProc() { counter++; } pthread_mutex_t mutex1 = THREAD_MUTEX_INITIALIZER; void functionC() pthread_mutex_lock( &mutex1 ); pthread_mutex_unlock( &mutex1 );

Deadlock A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does. This condition occurs when a mutex is applied but then not "unlocked". The order of applying the mutex is also important. The following code segment illustrates a potential for deadlock:

Deadlock void *function1() { ... pthread_mutex_lock(&lock1); // Execution step 1 pthread_mutex_lock(&lock2); // Execution step 3 DEADLOCK!!! } void *function2() pthread_mutex_lock(&lock2); // Execution step 2 pthread_mutex_lock(&lock1); int main() pthread_create(&thread1, NULL, function1, NULL); pthread_create(&thread2, NULL, function2, NULL);

Deadlock The threads may wait indefinitely for the resource to become free causing a deadlock. It is best to test and if failure occurs, free the resources and stall before retrying. ... pthread_mutex_lock(&mutex_1); /* Test if already locked */ while ( pthread_mutex_trylock(&mutex_2) ) { /* Free resource to avoid deadlock */ pthread_mutex_unlock(&mutex_1); /* stall here */ } count++; pthread_mutex_unlock(&mutex_2);

Summary Designing Threaded Programs Windows Multithreaded Programming POSIX Multithreaded Programming Deadlock