Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.

Slides:



Advertisements
Similar presentations
Operating Systems Components of OS
Advertisements

Chap 2 System Structures.
CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2.
Chapter 5 Threads os5.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Multithreading The objectives of this chapter are:
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/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.
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
What is Concurrent Programming? Maram Bani Younes.
CS 153 Design of Operating Systems Spring 2015
1 From Processes to Threads. 2 Processes, Threads and Processors Hardware can interpret N instruction streams at once  Uniprocessor, N==1  Dual-core,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Threads, Thread management & Resource Management.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
OPERATING SYSTEM SUPPORT DISTRIBUTED SYSTEMS CHAPTER 6 Lawrence Heyman July 8, 2002.
Processes Introduction to Operating Systems: Module 3.
11/13/20151 Processes ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original.
CS333 Intro to Operating Systems Jonathan Walpole.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
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.
Department of Computer Science and Software Engineering
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Processes and threads.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Operating Systems (CS 340 D)
Process Management Presented By Aditya Gupta Assistant Professor
Lecture 21 Concurrency Introduction
Operating System Concepts
Chapter 15, Exploring the Digital Domain
Chapter 2: System Structures
What is Concurrent Programming?
Threads Chapter 4.
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
Chapter 3: Processes.
Chapter 4: Threads.
Multithreading The objectives of this chapter are:
Threads.
Presentation transcript:

Thread basics

A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory related –Global address space –File-handle table –Heap data

A computer process When swapping processes for execution: –All things in that d.s. have to be brought to disk –Large chunks of memory contents Process lots of memory Swapping processes is expensive, lots of memory have to be moved around. Swapping (context swapping) is measured in seconds

Thread Sequence of byte code instructions executed by the JVM; thus a thread consists of two things: –A sequence of code –Its execution This sequence forms a path of code through a program sequence No notion of objects or methods here Sequences of instructions (threads) can overlap, also they can execute simultaneously on multiple threads

Thread data structure It contains data to keep track of this sequence and its execution –Contents of the registers –Position of execution in the instruction sequence –Run-time stack (LIFO) made up of activation records (created for methods to contain local variables, parameters, return pointer) –Life-time: threads are created and terminated.

Swapping threads OS typically swaps a thread –Push registers on the thread’s stack –Add thread d.s. to an OS list –Pull a different thread’s d.s off that list –Pop thread’s local stack into register set. Thread swapping is measured in milliseconds. –Thus: threads are lightweight processes A process is equivalent to Java’s virtual machine A thread is process state, ie a virtual machine state.

Threads and Processes Important: threads share the memory allocated to the process. Thus, given two threads: –May share instruction sequence –May share memory As a consequence: –Two threads may share program’s data and methods to modify it This is what makes threads hard to program. You need to carefully design for: –The commands executed by possibly multiple threads –The queries executed by possibly multiple threads –The time when that execution must occur. How to design threaded programming: –By synchronizing (locking) the actions caused by each thread that shares data with other threads.

Managing threads If a thread manipulates data that is not shared by any other thread, no need to manage this sequence of execution, no need for synchronization. Example of such case. Given two threads, each has its own stack: –Two threads do not share methods local variables and parameters. If a method does not access any heap data ( thus no access to objects and their fields), it can be executed simultaneously by multiple threads without explicit synchronization.

Multi-threaded programming Modern OS allow multiple threads to be running concurrently within a process. When JVM is started –A new process is created –Many threads can be created (spawned) in it. –One thread spawned is the thread having as execution sequence given by the “main”.

Default threads in JVM The “main” thread: –Spawned by the JVM when the process is created. –Beginning sequence is given by the main(). –Executes all statements in main() –Dies when main() completes execution.

Default threads in JVM The “garbage collection” thread: –Spawned by the JVM when the process is created. –The sequence of code it executes is the code written to discard objects and reclaim memory. Thus any Java program has at least two threads executing: we say that a Java program runs in a multi-threaded environment.

Default threads in JVM If the program contains a GUI interface a new thread is created: The “event-handling” thread: The sequence of instructions it executes is –the code associated with listeners. –Events coming from the OS. This means that if this thread is executing code of a listener, any mouse click or key press is put on hold until this thread terminates what is doing. As a consequence we have an unresponsive user interface: one that appears to hang.

Thread safety The phrase “thread safe” is used to describe a method that can run safely in a multi-threaded environment –Accesses process-level data (shared memory by several processes) in a safe and efficient way. The goal in designing multi-threaded programs is to achieve thread safety among other properties. This is usually a hard goal to achieve, one you must design up-front about, and one whose bugs are not only hard to pin-point but difficult to remove.

Why multi-threaded programming? Better interaction with user Simulation of simultaneous activities in one processor machine Exploitation of multi-processor machines Execution of computation code while doing I/o. Specially useful in network programming. Allow multiple user to execution same code simultaneously. Specially useful in network programming. Better Object oriented design.

Why understanding and using threads All nontrivial applications for the Java platform are multithreaded, whether you like it or not. It's not ok to have an unresponsive UI. It’s not ok for a server to reject requests.

When multi-threaded programming might not be good Threads carry overhead both in space and time. Thread management. All this cost must be considered when considering designing for threads.

Thread behavior is platform dependent! You need to use the OS threading system to get parallelism (vs. concurrency) –Different operating systems use different threading models (more in a moment). –Behavior often based on timing. –Multi-threaded apps can be slower than single-threaded apps (but be better organized)

Priorities The Java™ programming language has 10 levels The Solaris™ OS has 2 31 levels NT™ offers 5 (sliding) levels within 5 "priority classes." NT priorities change by magic. –After certain (unspecified) I/O operations priority is boosted(by an indeterminate amount) for some (unspecified) time. –Stick to Thread.MAX_PRIORITY, –Thread.NORM_PRIORITY,Thread.MIN_PRIORITY)

Threading models Cooperative (Windows 3.1) –A Thread must voluntarily relinquish control of the CPU. –Fast context swap, but hard to program and can’t leveragemultiple processors. Preemptive (NT) –Control is taken away from the thread at effectively random times. –Slower context swap, but easier to program and multiple threads can run on multiple processors. Hybrid (Solaris™ OS, Posix, HPUX, Etc.) –Simultaneous cooperative and preemptive models are supported.

Do not assume a particular environment Assume both of these rules, all the time: –A thread can prevent other threads from running if it doesn't occasionally yield by calling yield(), performing a blocking I/O operation, etc. –A thread can be preempted at any time by another thread even by one that appears to be lower priority than the current one.

Conclusion Easy to start using threads, VERY tough to master. Designing classes that contain methods that are thread safe is a challenge. Read: javadoc for the class java.lang.Thread