CISC2200 Threads Spring 2015

Similar documents
Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Chapter 4: Threads. Chapter 4: Threads

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Multithreaded Programming

Lecture 4 Threads. (chapter 4)

Chapter 5: Threads. Single and Multithreaded Processes

Operating Systems Prof. Ashok K Agrawala

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

OPERATING SYSTEM. Chapter 4: Threads

CS307: Operating Systems

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

Definition Multithreading Models Threading Issues Pthreads (Unix)

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

CS370 Operating Systems

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Multi-Threaded Programming

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Chapter 4: Threads. Chapter 4: Threads

CS370 Operating Systems

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Chapter 4: Multithreaded

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

4.8 Summary. Practice Exercises

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Processes and Threads

CS370 Operating Systems

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

CS370 Operating Systems

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Chapter 4 Multithreaded Programming

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

Process Description and Control

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

CS420: Operating Systems

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 3: Processes. Operating System Concepts 8th Edition

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Chapter 4: Multithreaded Programming

CSE 4/521 Introduction to Operating Systems

Chapter 5: Threads. Outline

Processes and Threads

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

Threads. CS3026 Operating Systems Lecture 06

Threads. studykorner.org

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CS Lecture 3! Threads! George Mason University! Spring 2010!

Semantics of fork() and exec()

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Thread. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Concurrency, Thread. Dongkun Shin, SKKU

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

操作系统原理与设计. 第 4 章 Threads( 线程 ) 陈香兰. March 28, 2014 中国科学技术大学计算机学院. 陈香兰 ( 中国科学技术大学计算机学院 ) 操作系统原理与设计 March 28, /

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

THREADS. Jo, Heeseung

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

CSE 153 Design of Operating Systems Fall 2018

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Processes. Johan Montelius KTH

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

A process. the stack

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

COP 4610: Introduction to Operating Systems (Fall 2016) Chapter 4: Threads. Zhi Wang Florida State University

COMP/ELEC 429/556 Introduction to Computer Networks

Lecture 2 Process Management

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

ECE 598 Advanced Operating Systems Lecture 23

Computer Systems Laboratory Sungkyunkwan University

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Last class: Today: Thread Background. Thread Systems

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS333 Intro to Operating Systems. Jonathan Walpole

Threads. Threads (continued)

CSE Opera,ng System Principles

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

CS510 Operating System Foundations. Jonathan Walpole

Questions from last time

Transcription:

CISC2200 Threads Spring 2015

Process We learn the concept of process A program in execution A process owns some resources A process executes a program => execution state, PC, We learn that bash creates processes to run user s command We practice with process creation in assignment Create a child process Parent process 2

OS: Protection Protection: mechanism for controlling access of processes or users to resources (Files, memory segment, CPU and other) Systems generally first distinguish among users to determine who can do what User identities (user IDs): a unique name and number User ID then associated with all files, processes of that user to determine access control Group identifier (group ID) allows set of users to be defined, then also associated with each process, file 3

Hardware Support for protection Dual-mode allows OS to protect itself and other system components A mode bit provided by processor hardware Kernel mode for running kernel code User mode for running user code Privileged instructions only executable in kernel mode System call: changes mode to kernel, return from call resets it to user mode ps command: reports user time, system time 4

5 Single-threaded Processes

Multithreading Separate two characteristics of process Unit of dispatching => thread, lightweight process Unit of resource ownership => process, task Multi-threading: ability of OS to support multiple, concurrent paths of execution with a single process 6

Multithreading (cont d) Process: unit of resource allocation/protection An address space for its image Protected access to resources Inter-process communication Thread: A thread execution state Saved context, Execution stack Access to memory/resource of its process, shared with other threads in the process 7

Why Multithreading? Many applications require a set of related units of execution Web Server example: listening on port for client connection request serving a client: read client request, serve the file Can create multiple processes for them Mainly performance consideration. Threads are Faster to create a new thread, terminate a thread Faster to switch between two threads within a process More efficient communication between threads (without invoking kernel) 8

Thread Usage (1) Figure 2-7. A word processor with three threads. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Thread Usage (2) Figure 2-8. A multithreaded Web server. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Thread Usage (3) Figure 2-9. A rough outline of the code for Fig. 2-8. (a) Dispatcher thread. (b) Worker thread. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Thread Usage (4) Figure 2-10. Three ways to construct a server. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Benefits of Threads Responsiveness Resource Sharing Economy Scalability Increase parallelism, allow application to take advantage of multiprocessor architecture 10

Concurrent Execution Parallel Execution (Multicore System) 11

POSIX Threads (1) Figure 2-14. Some of the Pthreads function calls. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Blocking I/O vs non-blocking I/O What we usually do to read/write from I/O is blocking I/O: read (src_fd, &buf, 1) cin >> op1 >> opcode >> op2; The calling process/thread blocks (enters into BLOCKED state) until reading operation completes. Non-blocking I/O int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags O_NONBLOCK); read (fd, ) will return failure if there is nothing to read Making blocking I/O after select, poll system call monitor multiple fds until one of them become ready commonly used in server programs call read() on file descriptor that is ready to read (i.e., it won t block the calling process) 10

Blocking I/O int main() { char buf[100]; int ret; int count=0; } ret = read (0,buf,100); if (ret>0){ write (1, "Read this:",9); write (1, buf, ret); } else write (2, failed in read.,15); 0: file descriptor for standard input, by default, it s linked to keyboard 1: file descriptor for standard output, by default, it s linked to terminal 2: file descriptor for standard error, by default, it s linked to terminal Same read(), write() system calls can be used to read/write from I/O device, files, sockets, We usually use higher-level library calls, printf, scanf (in C), cin and cout (in C++) 17

Non-blocking I/O int main() { char buf[100]; int ret; int count=0; int flags = fcntl (0, F_GETFL,0); fcntl (0, F_SETFL, flags O_NONBLOCK); get the current mode of file descriptor 0 (standard input), and set it to non-blocking do { ret = read (0,buf,100); /* sleep (1); //put wait for 1 second before continue... count++; cout << Waited << count << seconds \n ; */ } while (ret<=0); } buf[ret]='\0'; cout <<"Input"<<buf<<"End of INput <<endl; 18

Solution: blocking I/O after select, poll select, pselect, synchronous I/O multiplexing int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); Timeout value fd_set: a set of file descriptors void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, fd_set *set); void FD_SET(int fd, fd_set *set); void FD_ZERO(fd_set *set); allows a program to monitor multiple file descriptors Calling process waits (is blocked) until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform corresponding I/O operation (e.g., read(2)) without blocking. Timer expires Ex: in connect-four game or other game program, implement a limited time play (make a move within 10 seconds) 19

User Threads vs Kernel Threads Support for threads: provided at user level or kernel level User thread: Thread management done by user-level threads library Kernel thread: threads supported and managed by the Kernel 12

Implementation of Threads: User-level Implemented by a user-level threads package Kernel only knows about processes Thread library runtime system schedule multiple threads within a process Pro: Lower overhead when switching between threads (no system call/trap involved) Con: blocking I/O made by one thread will block whole process Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Many-to-One, User-level threads 14 Multiple user-level threads mapped to a single kernel thread/process; user-level thread library implements all threading functionalities Advantage: Thread switching requires no kernel mode (save time) Application specific scheduling Run on any OS Disadvantage: One thread blocking blocks all threads in process A multithread app. cannot take advantage of multiprocessor Examples: Solaris Green Threads. GNU Portable Threads Thread library

Implementation of Threads: Kernel Implemented by a kernel Kernel knows about threads, schedule threads Con: Overhead when switching between threads (system call/trap involved) Pro: blocking I/O made by one thread will NOT block whole process Question: Is Linux thread implemented in OS kernel, or by user-level threads library? package kernel threads package Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

One-to-One Each user-level thread maps to kernel thread Advantage? Disadvantage? Examples Windows NT/XP/2000, Linux, Solaris 9 and later 15

The Classical Thread Model (2) Figure 2-12. The first column lists some items shared by all threads in a process. The second one lists some items private to each thread. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Hybrid Implementations Figure 2-17. Multiplexing user-level threads onto kernel-level threads. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Many-to-Many Model User-level thread library: thread creation, scheduling, synchronization Multiple user level threads mapped to some (smaller of equal) number of kernel threads Programmer can adjust # of kernel threads Ex: Solaris prior to version 9, Windows NT/2000 with the ThreadFiber package Thread library 16

Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementation User-level library: library entirely in user space Kernel-level library supported by the OS 18

Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to developer of the library May be provided either as user-level or kernel-level Common in UNIX operating systems (Solaris, Linux, Mac OS X) 19

#include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ if (argc!= 2) { fprintf(stderr,"usage: a.out <integer value>\n"); return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,"argument %d must be non-negative\n",atoi(argv[1])); return -1; } pthread_attr_init(&attr); /* get the default attributes */

/* create the thread */ pthread_create(&tid,&attr,runner,argv[1]); pthread_join(tid,null); /* now wait for the thread to exit */ printf("sum = %d\n",sum); } /** * The thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } } pthread_exit(0); To compile: gcc pthread main.c 21

Java Threads Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface 22

Threading Issues Complication due to shared resources: e.g., global variables, system info for the process Semantics of fork() and exec() system calls Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools 23

Is swap thread-safe? int t; void swap(int *x, int *y) { t = *x; *x = *y; } // hardware interrupt might invoke isr() here! *y = t; Can swap function be called by multiple threads within a program? 34

Making Single-Threaded Code Multithreaded Figure 2-19. Conflicts between threads over the use of a global variable. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? Some Unix provides two version of fork(), one duplicate all threads and another duplicates only the thread making fork() calls exec() replace the entire process with the new program, including all threads 24

Thread Cancellation Terminating a thread (referred to as target thread) before it finishes Two general approaches: Asynchronous cancellation terminates target thread immediately Problematic if target thread is updating shared data, or has allocated some resource Deferred cancellation allows target thread to periodically check (at cancellation points) if it should be cancelled 25

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred 1. Signal is generated by particular event: segmentation fault, division by error, ctrl-c, kill. 2. Signal is delivered to a process 3. Signal is handled by the process A signal handler is used to process signals Default signal handler, user-defined signal handler 26

Signal Handling & multi-threading Deliver signal to all threads within the process, or just one particular thread? Options: Deliver signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process and process the signals 27

Thread Pools Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool 28

Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) 29

Linux Threads Linux does not distinguish between processes and threads Linux refers to them as tasks rather than threads A flow of control within a program System call clone(): create a new task, with flags specify the level of sharing between parent and child task CLONE_FS: file system info is shared CLONE_VM: memory space is shared CLOSE_SIGHAND: signal handlers are shared CLONE_FILES: the set of open files is shared How to create a task with similar sharing of threads as described in this class? How to create a task with similar sharing as process? 30