CS 3305 Intro to Threads. Lecture 6

Similar documents
CS333 Intro to Operating Systems. Jonathan Walpole

Threads. Threads (continued)

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

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

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

Multithreaded Programming

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

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Threads. studykorner.org

Introduction to pthreads

pthreads CS449 Fall 2017

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

LSN 13 Linux Concurrency Mechanisms

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

Threaded Programming. Lecture 9: Alternatives to OpenMP

Concurrency. Johan Montelius KTH

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

CS 261 Fall Mike Lam, Professor. Threads

Multicore and Multiprocessor Systems: Part I

Concurrent Server Design Multiple- vs. Single-Thread

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

CSE 333 SECTION 9. Threads

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Lecture 4 Threads. (chapter 4)

Operating systems fundamentals - B06

CS533 Concepts of Operating Systems. Jonathan Walpole

CS420: Operating Systems

Introduction to PThreads and Basic Synchronization

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

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

Processes and Threads

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

Chapter 4 Concurrent Programming

4.8 Summary. Practice Exercises

Concurrency, Thread. Dongkun Shin, SKKU

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CS 326: Operating Systems. Process Execution. Lecture 5

Thread Fundamentals. CSCI 315 Operating Systems Design 1

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CS370 Operating Systems

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

4. Concurrency via Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

Creating Threads. Programming Details. COMP750 Distributed Systems

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

Lecture 19: Shared Memory & Synchronization

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

518 Lecture Notes Week 3

High Performance Computing Course Notes Shared Memory Parallel Programming

CMPSC 311- Introduction to Systems Programming Module: Concurrency

POSIX PTHREADS PROGRAMMING

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

Processes. Johan Montelius KTH

CS240: Programming in C. Lecture 18: PThreads

Operating Systems & Concurrency: Process Concepts

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

Computer Systems Laboratory Sungkyunkwan University

Threads. CS3026 Operating Systems Lecture 06

A process. the stack

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Chapter 5: Threads. Single and Multithreaded Processes

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

TCSS 422: OPERATING SYSTEMS

POSIX Threads. HUJI Spring 2011

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

CS370 Operating Systems

PRACE Autumn School Basic Programming Models

Introduction to Threads

CSE 153 Design of Operating Systems Fall 2018

THREADS. Jo, Heeseung

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Operating Systems Prof. Ashok K Agrawala

CS370 Operating Systems

Thread and Synchronization

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

ECE 598 Advanced Operating Systems Lecture 23

CS370 Operating Systems

Pthreads: POSIX Threads

CSE 306/506 Operating Systems Threads. YoungMin Kwon

User Space Multithreading. Computer Science, University of Warwick

Concurrent Programming. Concurrent Programming with Processes and Threads

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

PThreads in a Nutshell

Definition Multithreading Models Threading Issues Pthreads (Unix)

CS 450 Operating System Week 4 Lecture Notes

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

Multiprocessors 2007/2008

Transcription:

CS 3305 Intro to Threads Lecture 6

Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer

Introduction Applications often need to perform many tasks at once This requires multiple threads of execution within a single process

Example Background printing GUI rendering Application core logic Example: Word processor Tasks include: Display graphics Respond to keystrokes from the user Perform spelling and grammar checking

Example Example: Web server It is desirable to service requests concurrently

Introduction Earlier we discussed the use of forking to create a process For example we could Word processor example: fork a process for each task Web server example: fork a process for each request Not very efficient since a fork copies everything

Threads A thread is a basic unit of CPU utilization Threads of a process share memory but can execute independently

Thread Usage Word Processor A word processor program with three threads.

Thread Usage Web Server Web/FTP server Process Request Client 2 Client 1 Process Request Client N Client 2 Client N

Why Not Fork? You certainly can fork a new process In fact, the first implementation of Apache web servers (Apache 1.0) forked N processes when the web server was started N was defined in a configuration file Each child process handled one connection at a time Problem: Process creation is time consuming and resource intensive Creating threads is not as expensive

Why Not Fork? Let s look at web servers Web servers have caches for read pages Forking means that these caches cannot be shared Using threads allows for these caches to be shared

Thread State Threads share Code Data (global variables) Open files, sockets Threads have their own CPU context Program counter(pc), Stack pointer (SP), register state

Single and Multithreaded Processes

Benefits of Threads Resource Sharing Example: Word processor The document is shared in memory Forking would require replication Allocating memory and resources for process creation is costly Responsiveness No replication - saves time Context-switching (CPU switching from one process to another) is faster

Thread Libraries A thread library provides the programmer with an API for creating and managing threads Three main libraries in use POSIX PThreads User or kernel level Win32 Kernel level library Used in Windows OS Java threads JVM is running on top of a host OS The Java thread API is implemented using a thread library available on the system

Pthreads: POSIX Threads Pthreads is a standard set of C library functions for multithreaded programming Pthread Library (60+ functions) Programs must include the file pthread.h

Thread Creation Thread identifiers Each thread has a unique identifier (ID), a thread can find out its ID by calling pthread_self(). Thread IDs are of type pthread_t which is usually an unsigned int.

Creates a new thread pthread_create() int pthread_create ( pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *), void *arg); Returns 0 to indicate success, otherwise returns error code thread: id of the new thread attr: argument that specifies the attributes of the thread to be created (NULL = default attributes) start_routine: function to use as the start of the new thread arg: argument to pass to the new thread routine If the thread routine requires multiple arguments, they must be passed bundled up in an array or a structure

pthread_create() example Let us say that you want to create a thread that simply prints hello world I am a thread int main(int argc, char *argv) { pthread_t worker_thread; } if (pthread_create(&worker_thread, NULL, do_work) { fprintf(stderr, Error while creating thread\n ); exit(1); }... void *do_work() { Printf ( \n hello world..i am a thread ); return NULL; }

Problem Sharing global variables is dangerous - two threads may attempt to modify the same variable at the same time. Use support for mutual exclusion primitives that can be used to protect against this problem. The general idea is to lock something before accessing global variables and to unlock as soon as you are done. More on this topic later in the course