MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrent Programming Abstraction & Java Threads
Real-Time Library: RTX
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
SMP threads an Introduction to Posix Threads. Technical Definition 1.Independent stream of instructions that can be scheduled to run by an operating system.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Java Threads CS Introduction to Operating Systems.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Win32 Programming Lesson 9: Jobs & Thread Basics.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Confidential Enterprise Solutions Group Process and Threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Threading and Concurrency COM379T John Murray –
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Windows CE Portable Modular Real-time Small footprint Embedded market.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Chapter 3 RTOS Concepts And Definitions Department of Computer Science Hsu Hao Chen Professor Hsung-Pin Chang.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Window Threads Chapter 7 Windows Thread Management.
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Multithreading.
Multithreading.
Java Based Techhnology
Multithreading.
Chapter 05. Multithread.
CSCI1600: Embedded and Real Time Software
Java Thread.
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
Threads and Multithreading
Representation and Management of Data on the Internet
CSCI1600: Embedded and Real Time Software
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

MultiThreaded Applications

What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of execution –Threads live in 1 process –Increases parallelization (blocking I/O) –Simplifies design –Better use of CPU –Bad if thread work is small (context switch) Necessary for building servers

Thread Basics A re-hash of 2601 Thread scheduling –Round-robin –Time slicing (each process gets a turn) –Context-switch –Thread states Running Ready Blocked

Thread Basics Thread Priorities –REALTIME –HIGH –NORMAL –IDLE The CPU will try and run the higher priorities Can lead to starvation

Multiprogramming Concepts Atomic operations –Needed for data integrity –Having multiple ASM statements as one –Can't be interrupted by CPU switch Mutual Exclusion –Allows programmer to perform a series of instructions in an atomic manner. Race Conditions –Having two or more processes compete for the same resources

Multiprogramming Concepts Starvation –When one thread never gets service (I.e. low priority) –Usually because another process is a hog

Multiprogramming Concepts Semaphores –Used when there are multiple resources that are the same. –When thread wants resource, takes it and decrements semaphore. –When finished, returns resource and increments semaphore –If at 0, fall asleep. Is woken up when a resource is free THEN decrements back to 0

Deadlock When threads can't make progress "I have the bat and want the ball; you have the ball and want the bat" Most operating systems simply ignore deadlock

Priority Inversion Assume A higher priority than B –B has mutex that A needs Assume A has highest, B middle and C lowest. –C acquires mutex that is shared with A –Thread B (higher than C) becomes ready to run –Thread B begins to run –A becomes ready…

Win32 Multithreading To create a thread in Win32: –Determine security attributes (NULL for default) –Determine it's stack size (0 for default) –Have a function(s) that can be used by a thread –The option to pass 1 parameter to that function –Special flags if you want it suspended (or 0 for default) –A pointer to a DWORD for the thread ID

Threaded Function Definition Must look similar to below: DWORD WINAPI threadFunc (LPVOID par);

Useful functions DWORD tID = GetCurrentThreadId ( ); –Determines the thread ID for the current thread you are in –Good for inside the threaded function –Good for outside the threaded function to determine primary thread's ID

Suspend and Resume To pause or resume a thread, you only need its handle: DWORD SuspendThread (HANDLE hThread); DWORD ResumeThread (HANDLE hThread);

#include DWORD WINAPI WorkerThreadProc (LPVOID lpThreadParameter); int main (void) { HANDLE h1, h2, h3; DWORD id1, id2, id3; h1 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id1); h2 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id2); h3 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id3); if (h1 != NULL) { Sleep (6000); printf ("Done"); CloseHandle(h1); CloseHandle (h2); CloseHandle (h3); } else { printf ("Failed to create worker thread: error 0x%x\n", GetLastError()); } return 0; } DWORD WINAPI WorkerThreadProc (LPVOID lpThreadParameter, LPVOID par2) { DWORD dwNumSeconds = (DWORD)lpThreadParameter; DWORD dwThreadId = GetCurrentThreadId(); for (int i = 0; i < (int)dwNumSeconds; i++) { printf ("[%08x] Hello, multithreaded world! = %d\n", dwThreadId, i); } return (dwThreadId); }

Now, in Java Java is pure OOP Class has a run( ) method Instances are the threads 2 ways to create Threaded class –extend Thread –implement Runnable

class MyThread extends Thread { int threadID; MyThread(int x) { threadID = x; this.start( );// Start calls run } public void run ( ) { for (int i = 0; i < 5000; i++) { System.out.println (threadID+":"+i); } // First to 5000 kills the program System.exit(1); } public static void main (String args[ ]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); }

class MyThread implements Runnable { int threadID; Thread t; MyThread(int x) { threadID = x; t = new Thread(this); t.start( ); } public void run ( ) { for (int i = 0; i < 5000; i++) { System.out.println (threadID+":"+i); } System.exit(1); } public static void main (String args[ ]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); }