CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.

Slides:



Advertisements
Similar presentations
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
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.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
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  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Threads CSCI 444/544 Operating Systems Fall 2008.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 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.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Chapter 4: Threads.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
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.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4: Threads.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
Chapter 4: Multithreaded Programming
Principles of Operating Systems Lecture 8
Realizing Concurrency using Posix Threads (pthreads)
Mid Term review CSC345.
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4: Threads.
Presentation transcript:

CSC 360, Instructor: Kui Wu Thread & PThread

CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues 5.Pthread library 6.An example

CSC 360, Instructor: Kui Wu 2 1. What is thread (1): Program, process, thread In one process easy to share Btw processes multitasking Best of both thread –one process –multitasking Q: browsers to use multi-process?

CSC 360, Instructor: Kui Wu 3 1. What is thread (2): Threads Thread a basic unit of CPU utilization –thread state, program counter, register set, stack share with other threads in the same process –code, data, opened files, signals, etc Benefits responsiveness: multithreading resource sharing, efficiency, MP architectures Q: potential problems?

CSC 360, Instructor: Kui Wu 4 1. What is thread (3): Single- threaded Web server Web server with cache and disk wait for a request process the request –check cache; if hit, break –otherwise, retrieve from disk (relatively slow) respond the request One request at a time or create a new process on each request –expensive!

CSC 360, Instructor: Kui Wu 5 1. What is thread (4): Multi- threaded Web server Dispatcher thread wait for a request handoff the request Worker threads process the request –disk I/O respond the request “Many” requests at a time

CSC 360, Instructor: Kui Wu 6 2. User vs kernel threads User threads: e.g., pthread library each process schedules its own threads no context switch between these threads a blocking call blocks the entire process Kernel threads: in almost all modern OS kernel manages all threads can pickup another thread if one blocks Hybrid approaches

CSC 360, Instructor: Kui Wu CSc 360Overview 7 3. Thread models (1) User-kernel mapping many-to-one: low cost, (lower) parallelism one-to-one: high parallelism, (higher) cost many-to-many: limited kernel threads

CSC 360, Instructor: Kui Wu 8 3. Thread models (2) Two-level model

CSC 360, Instructor: Kui Wu 9 4. Threading issues When a new process is created fork(), and usually then exec() –duplicate all threads or just the calling thread? When a signal to be delivered signal: event notification to be handled –to all, some, or a specific thread? Thread pool keep a pool of threads to be used –and reuse

CSC 360, Instructor: Kui Wu Pthread library (1) Create a thread –int pthread_create (thread, attributes, start_routine, arguments); PC: start_routine(arguments); default attributes: joinable and non-realtime Exit from a (created) thread –void pthread_exit (return_value); cleanup handlers by pthread_cleanup_push (); –stack-like “reverse” execution order

CSC 360, Instructor: Kui Wu Pthread library (2) Wait a target thread to exit: synchronize –int pthread_join (thread, return_value); release resource allocated to the target thread Put a target thread in detached state –int pthread_detach (thread); no other threads can “join” this one –no “pthread_attach” resource released once the thread exits –thread can be created in detached state

CSC 360, Instructor: Kui Wu Pthread (3) Cancel another thread –int pthread_cancel (thread); calling thread: send a request target thread: pthread_setcancelstate (); –ignore the request –terminate immediately ·asynchronous cancellation –check whether it should be cancelled periodically ·deferred cancellation

CSC 360, Instructor: Kui Wu Example (1): producer-consumer Multi-process shared memory solution message passing solution Single-process, multi-thread #include... void *producer (void *args); void *consumer (void *args); typedef struct {...} queue;

CSC 360, Instructor: Kui Wu 14 queue *queueInit (void); void queueDelete (queue *q); void queueAdd (queue *q, int in); void queueDel (queue *q, int *out); int main () { queue *fifo; pthread_t pro, con; fifo = queueInit (); if (fifo == NULL) { fprintf (stderr, "main: Queue Init failed.\n"); exit (1); } pthread_create (&pro, NULL, producer, fifo); pthread_create (&con, NULL, consumer, fifo); pthread_join (pro, NULL); pthread_join (con, NULL); queueDelete (fifo); return 0; } 6. Example (2): Main thread

CSC 360, Instructor: Kui Wu Example (3): Producer thread void *producer (void *q) { queue *fifo; int i; fifo = (queue *)q; for (i = 0; i < LOOP; i++) { /* produce LOOP items, inserting them into * the “fifo” queue. */... } return (NULL); }

CSC 360, Instructor: Kui Wu Example (4) Consumer thread void *consumer (void *q) { queue *fifo; int i, d; fifo = (queue *)q; for (i = 0; i < LOOP; i++) { /* Consumer LOOP items from the * “fifo” queue. */... } return (NULL); }