Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.

Slides:



Advertisements
Similar presentations
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Advertisements

Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecture 2 Foundations and Definitions Processes/Threads.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4 – Thread Concepts
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
Process Management Presented By Aditya Gupta Assistant Professor
Threads & multithreading
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Foundations and Definitions
Chapter 4: Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Lecture 4 Thread Concepts

Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process. Each thread keeps its own state parameters and register values so that it may run concurrently with other threads. Threads may be managed by the OS (kernel-level threads) or by a user application (user-level threads) Deitel & Associates, Inc.

Threads vs. Processes Processes each have their own address space, text space and resources, while threads spawned within a user application all share the memory and resources of the parent thread or process. A thread defines a single sequential execution stream within a process. Each thread maintains its own PC, SP and CPU register values.

Thread States 2004 Deitel & Associates, Inc.

Thread Operations Threads have some operations in common with processes CreateExit (terminate) SuspendResume SleepWake Other thread operations do not correspond to process operations Cancel - indicates that a thread should be terminated, but does not guarantee that the thread will be terminated. Threads can mask the cancellation signal. Join - a primary thread can wait for all other threads to exit by joining them. The joining thread blocks until the thread it joined exits Deitel & Associates, Inc.

User-Level Threads A user-level thread is one that is created and managed by a user application. Usually the OS does not know of the existence of user-level threads so additional resources are not provided by the OS for user-level threads. Each user-level thread must share the resources already allocated to the user application Deitel & Associates, Inc.

Kernel-Level Threads Kernel-level threads are created and managed by the OS. They attempt to address the limitations of user-level threads by mapping each thread to its own execution context. Kernel- level threads offer increased scalability, interactivity, and throughput, but have a higher overhead due to context switching and reduced portability because of OS-specific APIs 2004 Deitel & Associates, Inc.

to utilize a multiprocessor for parallel operations to do useful work while waiting for a slow device to satisfy human users by working on several actions at once to provide network service to multiple clients simultaneously to defer work until a less busy time to implement a pipeline process Multi-Threading Applications An Introduction to Programming with C# Threads - Andrew D. Birrell

using System; using System.Threading; namespace first_thread_demo { public class Version_1 { public static bool done = false; public static void T1() { System.Random rnd = new Random(); while (!done) { Console.WriteLine("T1 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T1 is leaving critical section"); } static void Main(string[] args) { Thread p = new Thread(new ThreadStart(T1)); p.Start(); Thread q = new Thread(new ThreadStart(T2)); q.Start(); Thread.Sleep(5000); done = true; Console.WriteLine("Run Finished"); Console.ReadKey(); } public static void T2() { System.Random rnd = new Random(); while (!done) { Console.WriteLine("T2 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T2 is leaving critical section"); } A Simple Multi-Threading Demo

T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section BothT1 & T2 can be in Critical Sections at the Same Time

public static int threadnumber = 1; : public static void T1() { System.Random rnd = new Random(); while (!done) { while (threadnumber == 2); Console.WriteLine("T1 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T1 is leaving critical section"); threadnumber = 2; } A "Fix" for the Problem The inclusion of a globally accessible variable threadnumber that can be set to the number of the preferred thread to execute can prevent two threads from entering their critical sections at the same time, but at what cost? a very important semicolon

Worker Threads and the BackgroundWorker Class A common application for multithreading is performing time-consuming tasks in the background. The main thread keeps running, while the worker thread does its background job. A C# application can become multi-threaded in two ways: (1) by explicitly creating and running additional threads, or (2) using a feature of the.NET framework that implicitly creates threads such as BackgroundWorker thread pooling, a threading timer a Remoting server or Web Services an ASP.NET application. Threading in C# Joseph Albahari

BackgroundWorker is a helper class in the System.ComponentModel namespace for managing a worker thread. It provides the following features: · A "cancel" flag for signaling a worker to end without using Abort · A standard protocol for reporting progress, completion and cancellation · An implementation of IComponent allowing it be sited in the Visual Studio Designer · Exception handling on the worker thread · The ability to update Windows Forms controls in response to worker progress or completion. The last two features are particularly useful – it means you don't have to include a try/catch block in your worker method, and can update Windows Forms controls without needing to call Control.Invoke. BackgroundWorker Class Threading in C# Joseph Albahari

1. Double-click on BackgroundWorker (see link appear below form). 2. Highlight backgroundWorker1 and look at Properties panel. 3. Select events panel (lightning bolt) 4. Double-click on DoWork to create a shell method. 5. Add worker code to this method: Thread.Sleep(1000); This statement will cause the worker to wait 1000 milliseconds (to simulate a time-consuming task). A Simple BackgroundWorker Demo Program

class Program { public static void Main() { Thread[] threads = new Thread[10]; Account acc = new Account(1000); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(acc.DoTransactions)); threads[i] = t; } for (int i = 0; i < 10; i++) threads[i].Start(); Console.ReadKey(); } When Threads Collide Independently executing threads are relatively easy to implement. Issues of complexity and program correctness arise when threads communicate and/or access shared memory. In this example, ten threads are running concurrently, all accessing the same memory space. Open a new "account" acc with 1000 "foobucks". Launch 10 concurrent threads of execution t that make repeated withdrawals from this account ensure that the account is not overdrawn

using System; using System.Threading; using System.Text; namespace lock_demo { class Account { static object locker = new object(); int balance; Random r = new Random(); public Account(int initial) { balance = initial; } private int Withdraw(int amount) { if (balance < 0) throw new Exception("Negative Balance"); lock (locker) { if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; } else return 0; // transaction rejected } public void DoTransactions() { for (int i = 0; i < 100; i++) Withdraw(r.Next(1, 100)); } lock( ) Demo

Balance before Withdrawal : 1000 Amount to Withdraw : -87 Balance after Withdrawal : 913 Balance before Withdrawal : 913 Amount to Withdraw : -62 Balance after Withdrawal : 851 Balance before Withdrawal : 851 Amount to Withdraw : -28 Balance after Withdrawal : 823 Balance before Withdrawal : 823 Amount to Withdraw : -3 Balance after Withdrawal : 820 Balance before Withdrawal : 820 Amount to Withdraw : -8 Balance after Withdrawal : 812 Balance before Withdrawal : 812 Amount to Withdraw : -32 Balance after Withdrawal : 780 Balance before Withdrawal : 780 Amount to Withdraw : -16 Balance after Withdrawal : 764 Balance before Withdrawal : 764 Amount to Withdraw : -69 Balance after Withdrawal : 695 Balance before Withdrawal : 695 Amount to Withdraw : -29 Balance after Withdrawal : 666 Balance before Withdrawal : 666 Amount to Withdraw : -42 Balance after Withdrawal : 624 Balance before Withdrawal : 624 Amount to Withdraw : -44 Balance after Withdrawal : 580 Balance before Withdrawal : 580 : Amount to Withdraw : -22 Balance after Withdrawal : 166 Balance before Withdrawal : 166 Amount to Withdraw : -26 Balance after Withdrawal : 140 Balance before Withdrawal : 140 Amount to Withdraw : -5 Balance after Withdrawal : 135 Balance before Withdrawal : 135 Amount to Withdraw : -57 Balance after Withdrawal : 78 Balance before Withdrawal : 78 Amount to Withdraw : -14 Balance after Withdrawal : 64 Balance before Withdrawal : 64 Amount to Withdraw : -11 Balance after Withdrawal : 53 Balance before Withdrawal : 53 Amount to Withdraw : -16 Balance after Withdrawal : 37 Balance before Withdrawal : 37 Amount to Withdraw : -20 Balance after Withdrawal : 17 Balance before Withdrawal : 17 Amount to Withdraw : -15 Balance after Withdrawal : 2 Balance before Withdrawal : 2 Amount to Withdraw : -1 Balance after Withdrawal : 1 Balance before Withdrawal : 1 Amount to Withdraw : -1 Balance after Withdrawal : 0 Results with lock( )

Balance before Withdrawal : 1000 Amount to Withdraw : -82 Balance after Withdrawal : 918 Balance before Withdrawal : 918 Amount to Withdraw : -33 Balance after Withdrawal : 885 Balance before Withdrawal : 885 Amount to Withdraw : -53 Balance after Withdrawal : 832 Balance before Withdrawal : 832 Amount to Withdraw : -18 Balance after Withdrawal : 814 Balance before Withdrawal : 814 Balance before Withdrawal : 1000 Amount to Withdraw : -94 Balance after Withdrawal : 720 Balance before Withdrawal : 720 Amount to Withdraw : -68 Balance after Withdrawal : 652 Balance before Withdrawal : 652 Amount to Withdraw : -50 Balance after Withdrawal : 602 Balance before Withdrawal : 602 Amount to Withdraw : -16 Balance after Withdrawal : 586 Balance before Withdrawal : 586 Amount to Withdraw : -81 Balance before Withdrawal : 720 Amount to Withdraw : -35 Balance after Withdrawal : 470 Balance before Withdrawal : 470 Amount to Withdraw : -97 Balance after Withdrawal : 373 Amount to Withdraw : -98 Balance after Withdrawal : 275 : Balance before Withdrawal : 152 Amount to Withdraw : -26 Balance before Withdrawal : 152 Balance before Withdrawal : 275 Amount to Withdraw : -3 Balance after Withdrawal : 123 Balance before Withdrawal : 126 Amount to Withdraw : -49 Balance after Withdrawal : 74 Balance before Withdrawal : 126 Amount to Withdraw : -71 Balance after Withdrawal : 3 Amount to Withdraw : -2 Amount to Withdraw : -55 Balance after Withdrawal : 126 Balance after Withdrawal : 1 Balance before Withdrawal : 1 Balance before Withdrawal : 74 Amount to Withdraw : -63 Balance before Withdrawal : 123 Balance before Withdrawal : 74 Balance after Withdrawal : -54 Amount to Withdraw : -59 Balance after Withdrawal : -176 Balance after Withdrawal : -117 Amount to Withdraw : -1 Amount to Withdraw : -71 Balance after Withdrawal : -248 Amount to Withdraw : -54 Balance after Withdrawal : -177 Balance after Withdrawal : -302 Results without lock( )

Summary A thread is a lightweight process Two or more threads spawned within an application share memory space User-level threads are controlled by the parent process Kernel-level threads are controlled by the OS There are many reasons to build a multithreaded application Multiple threads running independently are easy to implement Effective (and error-free) thread communication and/or interaction is difficult Concurrent (i.e. asynchronous) operation means "order of execution between threads is indeterminate"