Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Operating Systems Parallel Systems (Now basic OS knowledge)
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.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Threads CSCI 444/544 Operating Systems Fall 2008.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Process Concept An operating system executes a variety of programs
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
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 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
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
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.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
© 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,
1 Web Based Programming Section 8 James King 12 August 2003.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
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 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Cooperating Processes The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
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 2: Homework 1 and Project 1
Chapter 4 – Thread Concepts
Introduction to threads
Processes and threads.
Concurrency.
Concurrency, Processes and Threads
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Parallel Systems Chapter 6 9/17/2018 Crowley OS Chap. 6.
Operating System Concepts
Threads Chapter 4.
Concurrency, Processes and Threads
Gentle Introduction to Threads
Concurrency, Processes and Threads
CS703 – Advanced Operating Systems
Presentation transcript:

Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack space. –Remaining resources (memory space and operating system resources, such as open files) are shared with peer threads within the same process. –A thread is a lightweight process.

By sharing a single process’ memory space and O/S objects a program written with multiple threads requires less overhead at context switch time. This can lead to more efficient use of the CPU for multi-threaded applications. Figure 6.10: Example of two multi-threaded processes. Threads can execute the same code with different contexts (different register sets, stack & program counter). A process has at least one thread.

Extend SOS to support threads through three new calls: –int CreateThread(char *startAddr, char *startStack) - start a new thread; code begins at startAddr and the stack it will use is at startStack. Call returns a Thread ID (tid). –int ExitThread(int returnCode) - similar to a process Exit() call, except the last thread of a process, where it is exactly the process Exit() call. –int WaitThread(int tid) - block the calling thread until the thread specified by tid calls ExitThread(). Stack management is the responsibility of the thread creator (except for the first thread).

Advantages of Threads –Provide parallel (possibly pseudo-parallel) programming. –Cheaper to create and destroy. –Cheaper to context switch between. –By definition they share memory, meaning shared- memory based IPC is a natural and slow system calls are not needed to communicate. –Good for parallel algorithms that are tightly coupled and that use the same data structures. –Common term: multi-threaded server.

Uses of threads –Partition the activities of a single process (Figure 6.11). –Permit asynchronous I/O within a process -- have a thread wait for the I/O to complete. –Activity replication -- a server process can create threads on demand as services are requested. Great example - a multi-thread web server. Another example: disk block server (Figure 6.12). Thread implementation (6.7.5): not covered.

Process - has its own address space and is allocated CPU time. Process with threads - split out the concept of CPU allocation into the thread, leaving the process as a shell to hold everything else. Threads can be implemented in the operating system or they can be implemented totally within a user process (called user threads). A user process does not need to be in system mode and use special instructions to swap stack pointers and register sets. A timer helps, though. User threads are more efficient (no O/S switch).

A process running user threads may run more efficiently but can’t take advantage of any O/S thread calls, since the O/S isn’t aware of the user-level threads. Kernel threads are threads designed to run within the O/S itself. Examples of Threads: –O/S-level threads are built into Mach, Solaris, OS/2, Windows NT & most modern UNIXes. –User-level threads can be found all over. Of note -- the Java language runtime supports threads. By extension this means Java-aware browsers also implement user-level threads (Netscape, MSIE, etc.).

Java Threads (taken from chapter 15 of The Java Tutorial). –JavaSOS uses Java threads, so it’s worth covering their specifics. –Java thread definition: a single sequential flow of control within a program. –The Thread class is provided in the java.lang package. –You can either extend the Thread class or in the case of a class already being extended (like the Applet class) you can say the class implements Runnable. –Either way you can then override a number of Thread class methods that affect thread behavior.

Example using extends: class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + “ “ + getName()); try { sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println(“DONE! “ + getName()); }

Example creating two instances of SimpleClass and running them: class TwoThreadsTest { public static void main (String[] args) { new SimpleThread(“Jamacia”).start(); new SimpleThread(“Fiji”).start(); } –Notice use of overridden Thread method run().

Example using implements: Class Clock extends Applet implements Runnable { public void start() { if (clockThread == null) { // if applet just loaded clockThread = new Thread(this, “Clock”); clockThread.start(); } public void stop() { clockThread = null; } public void run { while (Thread.currentThread() == clockThread) { repaint(); // (sleep w/ exception handling) // } }

JavaSOS uses implements Runnable for the Java threads. Check these files: AppGUICounter.java, AppTests.java - the three user-level applications are all Java threads. HWSimulation.java:class HWSimulatedDisk HWSimulation.java:class HWTimer SOSStart.java:class SOSStart JavaSOS uses the suspend() and resume() methods of the Thread class to block one process and start another one running.

Implementation of Mutual Exclusion –Two prior solutions to the mutex problem: use of Message Queues & that pesky ExchangeWord() (more on this later). –1st solution - disable interrupts while the process is in a critical section (no interrupts == no timer interrupt == no context switch to some other process). Book mentions it works well on a single-processor machine, but neglects the downside of having a user program running with interrupts disabled! –2nd solution - Use ExchangeWord(): a special hardware instruction that allows one processor to read and modify a word in memory without any other processor getting access (atomic read & modify).

Implementation of Mutual Exclusion –Good for multiprocessor mutex solutions; exists in different forms, such as “Test and Set”. Software Solutions - no special hardware instructions exist to enforce the mutex. First solution by Dekker in 1965; complex. A simpler solution done by Peterson in Imagine two processes, each with the same structure: void Processn(void) { while (1) { DoSomeStuff(); EnterCriticalSection(0); DoCriticalSectionStuff(); LeaveCriticalSection(0); }

To work, the processes have to agree that: –A process will not wait forever in EnterCriticalSection(). –Only one process at a time can be in the critical section. –A process will spend a finite time in the critical section. –A process that wants to enter its critical section cannot be made to wait for the other process to enter its critical section. enum { False = 0; True = 1; }; int interested[2] = {False, False}; int turn = 0; void EnterCriticalSection(int this_process) { int other_process = 1 - this_process; interested[this_process] = True; turn = this_process; while (turn == this_process && interested[other_process]) { // do nothing (busy loop) }

void LeaveCriticalSection(int this_process) { interested[this_process] = False; } –This algorithm works with two processes contending for the critical section and it also works if there’s only one process. –It’s important you understand how this code works!

Disabling Interrupts –Fast! –No busy waiting! –Uniprocessor-only solution! –Best solution for a single processor (with the exception you don’t want the interrupts to be disable-able within user mode). Using ExchangeWord() –Busy waiting :( –Requires hardware assistance (memory subsystem) :( –Works with multiple processors! –Best solution for shared memory multiprocessors. Peterson’s algorithm (software-only solution) –Busy waiting :( –No hardware assistance required! –Works with multiple processors! –Best solution for distributed systems with no centralized control.