Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.

Slides:



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

Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
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: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
ThreadsCS-502 Fall Threads CS-502 Operating Systems (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, &
ThreadsCS-3013 C-term Threads CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz,
Threads CSCI 444/544 Operating Systems Fall 2008.
ThreadsCS-3013 A-term Threads CS-3013, Operating Systems A-term 2009 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
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.
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
CS 153 Design of Operating Systems Spring 2015
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
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.
Threads, Thread management & Resource Management.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
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.
ThreadsCS-3013 C-term Threads (continued) CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed.,
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
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.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
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.
Threads. Readings r Silberschatz et al : Chapter 4.
Threads A thread is an alternative model of program execution
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Operating System Concepts
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Threads Overview Benefits, User and Kernel Threads.
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.
CS 6560: Operating Systems Design
Day 12 Threads.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4 Threads.
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
CS-502, Operating Systems Fall 2007
Threads & multithreading
Realizing Concurrency using Posix Threads (pthreads)
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 153 Design of Operating Systems Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
Threads Chapter 4.
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
CS-3013 Operating Systems Hugh C. Lauer
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Realizing Concurrency using Posix Threads (pthreads)
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Realizing Concurrency using Posix Threads (pthreads)
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Presentation transcript:

Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006

Threads 2 CS502 Spring 2006 Problem – Unix/Windows Processes are Heavyweight Lots of data in process context Even more when we study memory management More than that when we study file systems, etc. Processor caches a lot of information Memory Management information Caches of active pages Costly context switches and traps 100’s of microseconds

Threads 3 CS502 Spring 2006 Problem – Unix/Windows Processes are Heavyweight (continued) Separate processes have separate address spaces Shared memory is limited or nonexistent Applications with internal concurrency are difficult Isolation between independent processes vs. cooperating activities Fundamentally different goals

Threads 4 CS502 Spring 2006 Example Web Server – want to handle multiple concurrent requests – How? One solution: –create several processes that execute in parallel –Use shared memory ( shmget() ) to map to the same address space in the processes –have the OS schedule them in parallel Not efficient –space: PCB, page tables, etc. –time: creating OS structures ( fork() ) and context switch

Threads 5 CS502 Spring 2006 Example 2 Transaction processing systems – e.g, airline reservations 1000’s of transactions per second Separate processes per transaction are too costly Other techniques (e.g., message passing) are much more complex

Threads 6 CS502 Spring 2006 This problem … … is partly an artifact of Unix, Linux, and Windows Big, powerful processors (e.g., Pentium 4) … tends to occur in most large systems … is infrequent in small-scale systems PDAs, cell phones, hand-held games Closed systems (i.e., controlled applications)

Threads 7 CS502 Spring 2006 Solution:– Threads A thread is the execution of a program or procedure within the context of a Unix or Windows process I.e., a specialization of the concept of process A thread has its own Program counter, registers, PSW Stack A thread shares Address space, heap, static data All other resources with other threads in the same process

Threads 8 CS502 Spring 2006 Threads 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC

Threads 9 CS502 Spring 2006 Thread Interface This is taken from the POSIX pthreads API: –int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void), void *arg) ; creates a new thread of control new thread begins executing at start_routine –pthread_exit(void * value_ptr) terminates the calling thread –pthread_join(pthread_t thread, void **value_ptr); blocks the calling thread until the thread specified terminates –pthread_t pthread_self() Returns the calling thread's identifier

Threads 10 CS502 Spring 2006 Threads Linux, Windows, and various versions of Unix have their own thread interfaces Similar, not standardized Some issues E.g., ERRNO in Unix — a static variable set by system calls

Threads 11 CS502 Spring 2006 Unix Processes vs. Threads On a 700 Mhz Pentium running Linux –Processes: fork()/exit () microsec –Kernel threads: pthread_create()/pthread_join(): 90 microsec –User-level threads: pthread_create()/pthread_join(): 5 microsec

Threads 12 CS502 Spring 2006 Threads – Management Who/what creates and manages threads –Kernel level – new system calls and new entity to manage (Linux (called lightweight process), Win/NT (threads)) –User level done with function library (Posix) Runtime system – similar to process management except in user space Win/NT - fibers

Threads 13 CS502 Spring 2006 Threads – User Space Can be implemented without kernel support … or knowledge! Program links with a runtime system that does thread management Operation are efficient (procedure calls) Space efficient and all in user space (TCB) Task switching is very fast Since kernel not aware of threads, there can be scheduling inefficiencies E.g., blocking I/O calls

Threads 14 CS502 Spring 2006 Threads – User Space Thread Scheduler –Queues to keep track of threads’ state –Scheduler – non-preemptive Assume threads are well-behaved Thread gives up CPU by calling yield() – does context switch to another thread –Scheduler – preemptive Assumes threads may not be well-behaved Scheduler sets timer to create a signal that invokes scheduler Scheduler can force thread context switch Increased overhead

Threads 15 CS502 Spring 2006 Threads – Kernel Space OS schedules threads instead of processes Benefits –Overlap I/O and computing in a process –Creation is cheaper than processes –Context switch can be faster than processes Negatives –System calls (high overhead) for operations –Additional OS data space for each thread

Threads 16 CS502 Spring 2006 Threads – supported by processor E.g., Pentium 4 with Hyperthreading™ Multiple CPU’s on a single chip True concurrent execution within a single process Requires kernel support Re-opens old issues Deadlock detection

Threads 17 CS502 Spring 2006 Mutual Exclusion within Threads extern void thread_yield(); extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_critical_region(int &lock) { while (TestAndSet(lock) == 1) thread_yield(); /* give up processor */ }; void leave_critical_region(int &lock) { lock = 0; };

Threads 18 CS502 Spring 2006 Threads inside the OS kernel Kernels have become big, multi-threaded programs. Lots of concurrent activity Multiple devices operating at one time Multiple user activities at one time A useful tool Special kernel thread packages, synchronization primitives, etc. Useful for complex OS environments

Threads 19 CS502 Spring 2006 Threads – Summary Processes are very heavyweight in Unix, Linux, Windows, etc. Need for isolation between processes at odds with desire for concurrency within processes Threads provide an efficient alternative Thread implementation and management strategies depend upon expected usage Kernel support or not Processor support or not

Threads 20 CS502 Spring 2006 IA-32 Intel® Architecture Software Developer’s Manual (Figure 2-1)