NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Chapter 4: Multithreaded Programming
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
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.
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Threads A thread is an alternative model of program execution
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Tutorial 4. In this tutorial session we’ll see Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
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.
Realizing Concurrency using the thread model
Introduction to threads
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
Day 12 Threads.
Threads in C Caryl Rahn.
Multithreading Tutorial
Operating System Concepts
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Multithreading Tutorial
Realizing Concurrency using the thread model
Lab 5 Process Control Operating System Lab.
Operating System Concepts
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

NCHU System & Network Lab Lab #6 Thread Management Operating System Lab

NCHU System & Network Lab Why We Need Threads? A traditional process –Needs a fair amount of "overhead" –Has a single thread of control If the process has multiple threads of control, it can do more than one task at a time. Threads –Run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code

NCHU System & Network Lab What Is a Thread?

NCHU System & Network Lab What Is a Thread? (cont.) A thread is a flow of control within a process. A multithreaded process contains several different flows of control within the same address space. A thread has a thread ID, a program counter, a register set, and a stack, and is similar to a process has. However, a thread shares with other threads in the same process its code section, data section, and other OS resources. –Thus, thread is lightweight

NCHU System & Network Lab The Benefits of Multithreaded Programming Responsiveness Resource sharing –Share resources of the process to which they belong Economy –Allocating memory and resources for process creation is costly –Threads that share resources of the process to which they belong is more economical to create and context switch threads. Utilization of multiprocessor architectures –Be greatly increased in a multiprocessor architecture

NCHU System & Network Lab Challenges When Using Thread Because threads within the same process share resources: –Changes made by one thread to shared system resources will be seen by all other threads. –Reading and writing to the same memory locations at the same time by two different thread is possible Race condition Therefore, thread needs explicit synchronization by the programmer.

NCHU System & Network Lab 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.

NCHU System & Network Lab What Are Pthreads? Historically, hardware vendors have implemented their own proprietary versions of threads. –Difficult for programmers to develop portable threaded applications. For UNIX systems, a standardized programming interface has been specified by the IEEE POSIX c standard (1995). Implementations which adhere to this standard are referred to as POSIX threads, or Pthreads. Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library.

The Pthreads API There is a whole set of library calls associated with threads, most of whose names start with pthread_. To use these library calls, we must include the file pthread.h, and link with the threads library using - lpthread. Table 1. POSIX thread management functions.

NCHU System & Network Lab Referencing Threads by ID pthread_self : A thread can find out its ID. pthread_equal : To compare thread IDs for equality.

Creating a Thread pthread_create : creates a thread. thread : Points to the ID of the newly created thread. attr : An attribute object that encapsulates the attributes of a thread. NULL for the default values. start_routine : The C routine that the thread calls when it begins execution arg : The argument that is to be passed to start_routine. NULL may be used if no argument is to be passed.

NCHU System & Network Lab Exiting If the main thread has no work to do after creating other threads, it should either block until all threads have completed or call pthread_exit(NULL).

NCHU System & Network Lab Joining pthread_join : causes the caller to wait for the specified thread to exit. thread : The ID of the terminating thread. value_ptr : Provides a location for a pointer to the return status that the target thread passes to pthread_exit. The caller does not retrieve the target thread return status if NULL.

NCHU System & Network Lab Example: Example: Creation and Termination

NCHU System & Network Lab Example: (cont.) Example: Creation and Termination (cont.)

Lab A program starts and immediately creates four identical threads. –Also declare a global variable count Each thread increments the same global variable 250,000 times. So count is incremented a total of 1,000,000 times. –The main program passes a value containing the number of iterations to the threads.

Lab (cont.) The main program waits for the four threads to complete. Print out the global count variable. Then, run your program 15 times, observe all of the results you get, and think about what problem will be have?

NCHU System & Network Lab References Avi Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating System Concepts,” John Wiley & Sons, 6th Edition, 2001 POSIX thread programming “Unix Systems Programming: Communication, Concurrency, and Threads” by Kay A. Robbins, Steven Robbins Neil Matthew and Richard Stones, “Beginning Linux Programming,” Wiley publishing, 3rd Edition, 2004 W. Richard Stevens, “Advanced Programming in the UNIX Environment,” Addison-Wesley, 1992

References (cont.) Marshall Brain, “Win32 System Services: The Heart of Windows 95 and Windows NT,” Prentice Hall PTR, 2nd Edition, 1996 IBM ?topic=/rzahw/rzahwsemco.htm NCHU System & Network Lab