CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
Advertisements

Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 3 Process Description and Control
CSC 322 Operating Systems Concepts Lecture - 8: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
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 5 Processes and Threads Copyright © 2008.
Threads Section 2.2. Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Threads CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
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.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Threads, Thread management & Resource Management.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
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 Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
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
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
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.
Processes & Threads Introduction to Operating Systems: Module 5.
Threads, Thread management & Resource Management.
CSC 322 Operating Systems Concepts Lecture - 10: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
OPERATING SYSTEM LESSON 5 THREADS. 2 The process model is based on two independent concepts: resource grouping and execution. A process is the way to.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
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:
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Processes and threads.
CS 6560: Operating Systems Design
Day 12 Threads.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Operating Systems Threads.
CSC 322 Operating Systems Concepts Lecture - 16: by
Threads & multithreading
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 10: Threads Implementation
Threads Chapter 4.
Process Description and Control
CSE 153 Design of Operating Systems Winter 2018
Threads and Concurrency
Threads Chapter 4.
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 10: Threads Implementation
Realizing Concurrency using Posix Threads (pthreads)
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. (Chapter-1) Ahmed Mumtaz Mustehsan, CIIT, Islamabad

Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad2 Process and Thread Process has an address space and a sin­gle thread of control. Processes may have multiple threads of control in the same address space running in quasi-parallel, like (almost) separate processes. Threads are Processes with in process Threads are lightweight processes share the same address space and resources allocated to process. Process share the resources offered by operating system among other processes

Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad3 Usage of Threads Example: Processing a large document, having threads for, Interactive users, background formatter and backup file on disk

. Thread Example-web server. Lecture 74Ahmed Mumtaz Mustehsan, CIIT, Islamabad This model allows the server to be written as a collection of sequential threads.

(a) Dispatcher thread. (b) Worker thread. Web server code Lecture 75Ahmed Mumtaz Mustehsan, CIIT, Islamabad

In the absence of threads, web process operates as a single thread. If page is not there, then thread blocks CPU does nothing while it waits for page Server can handle fewer requests of the clients as compared to multithreaded implementation. Wastage of CPU time if the server is a dedicated web server Web server with single thread of control Lecture 76Ahmed Mumtaz Mustehsan, CIIT, Islamabad

If page is not there, the server switches to another event/request (use a non-blocking call) The state of the computation must be saved and restored in the table every time the server switches from one request to another. The next event may be a request for new work or a reply received through Interrupt-Signal from the disk about a previous operation. If it is new work, it is started, otherwise the relevant information is fetched from table and reply is processed. A design where process information is saved and set of events change the state is called a Finite-State-Machine. Implementation of threads in a hard way. Web Server using Finite-State-Machine Lecture 77Ahmed Mumtaz Mustehsan, CIIT, Islamabad

Three ways to build the server Lecture 78Ahmed Mumtaz Mustehsan, CIIT, Islamabad

Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad9 The Classical Thread Model a). Three Processes each with one thread b). One process with three threads

Enables parallelism (web server) with blocking system calls Threads are faster to create and destroy then processes Natural for multiple cores (Multiprocessors) Easy programming model The process could be structured with an: The input thread; reads data into an input buffer. The processing thread; takes data out of the input buffer, processes them, and puts the results in an output buffer. The output thread; writes these re­sults back to disk. Reasons to use threads Lecture 710Ahmed Mumtaz Mustehsan, CIIT, Islamabad

Threads are lightweight Lecture 711Ahmed Mumtaz Mustehsan, CIIT, Islamabad threads are not as independent as processes. All threads have the same address space, and share global variables. one thread can read, write, or even wipe out an­ other thread's stack. There is no protection between threads because 1.it is im­possible ( why ?) 2.it should not be necessary. (Why ?)

Have same states; Running, Ready and Blocked Share the CPU time quantum allocated to their process Have their own stacks ; same as processes Stacks contain: frames for (unreturned) procedure calls Local variables Return address to use when procedure comes back, hence maintains the history of calling sequence. Example, if proce­dure X calls procedure Y and Y calls procedure Z, then while Z is executing, the frames for X and Y will all be on the stack. Threads are like processes Lecture 712Ahmed Mumtaz Mustehsan, CIIT, Islamabad

Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad13 Each Thread has its own stack Threads are like processes

Execution start with one thread in a process that creates new threads Thread contains (id, registers, attributes) Use library call to create, and manage new threads. Thread_create takes parameter indicating what procedure to run and returns thread identifier/name Thread_exit causes thread to exit and disappear (no longer schedulable) Thread_join causes thread to block until another thread finishes its work Thread_yield volun­tarily give up the CPU to let another thread run How do threads work? Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad14

Pthreads are IEEE UNIX standard library calls POSIX Threads (Pthreads) Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad15

. A Pthreads example “Hello, world”... Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad16

(a)Implementation of threads in user space. (b)Implementation of threads in kernel space. Choice is moderately Controversial Implementing Threads Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad17 (a)(b)

Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad18 Implementing Threads in user space Can be implemented on an operating system that does not support threads. The threads are implemented by a library procedures called Run Time Library. (RTL) Example: pthread_create, pthread_exit, pthread_join, and pthread_yield, Process needs its own private Thread-table, contains thread's program counter, stack pointer, registers, state, and so. Information used and managed by RTL Context switching takes places either a thread execute thread_exit or call thread_yield

RetainedIf thread blocks, and control is still Retained by RTL and not transferred to the kernel then RTL saves thread info in table and picks up new thread to run. State saving and scheduling are invoked faster then kernel call (no trap, no cache flush) change of state is managed by RTS: As soon as the stack pointer and program counter have been switched, the new thread comes to life again automat­ically. each process can have its own customized scheduling algorithm Advantages of implementing Threads in user space Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad19

Retained Retained : Can’t let a thread to execute system call which will trap to kernel who will block the process and hence all of the threads within the process. No elegant solution  Requires changes in the system library to avoid blocking calls ( e.g. keep reading keyboard until the data is typed in)  Could use select system calls. ( UNIX code placed around system call to do the checking weather the system call is safe or not! is called a jacket or wrapper. Difficulty of implementing Threads in user space Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad20

Threads don’t voluntarily give up CPU Could interrupt periodically to give control to run time system using clock timer. clock timer is very inefficient if repeated periodically and secondly it my clash with the clock timer requested by the process. Hence overhead of this solution is a problem….. Page fault by a thread blocks the process hence all threads. Disadvantages; implementing Threads in user space Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad21

In addition to process table, kernel maintains thread- table, to keep track of all the threads in the system. To create a new or destroy existing thread, it makes a kernel call, which does the creation or destruction by updating the kernel thread table. All calls that might block a thread are implemented as system calls. If thread blocks, kernel just picks another one Not necessarily from same process! To save time recycling of threads is possible. Expensive to manage the threads in the kernel and takes valuable kernel space Implementing threads in kernel space Lecture 7Ahmed Mumtaz Mustehsan, CIIT, Islamabad22