Threads. Threads (continued)

Similar documents
CSE 306/506 Operating Systems Threads. YoungMin Kwon

pthreads CS449 Fall 2017

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Threads. studykorner.org

CS 3305 Intro to Threads. Lecture 6

LSN 13 Linux Concurrency Mechanisms

CS333 Intro to Operating Systems. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

CSE 333 SECTION 9. Threads

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

Introduction to pthreads

CS510 Operating System Foundations. Jonathan Walpole

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

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

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

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

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

Introduction to PThreads and Basic Synchronization

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Concurrency. Johan Montelius KTH

Multithreaded Programming

ENCM 501 Winter 2019 Assignment 9

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

Thread and Synchronization

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru Department of Electronics and Communication Engineering

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

POSIX Threads. HUJI Spring 2011

CS 261 Fall Mike Lam, Professor. Threads

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

Threads. CS3026 Operating Systems Lecture 06

Threaded Programming. Lecture 9: Alternatives to OpenMP

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

4. Concurrency via Threads

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

Threads. Jo, Heeseung

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

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

CSE 333 Section 9 - pthreads

Operating Systems & Concurrency: Process Concepts

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

Operating systems fundamentals - B06

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

Multicore and Multiprocessor Systems: Part I

Pthread (9A) Pthread

User Space Multithreading. Computer Science, University of Warwick

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

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

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Processes and Threads

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

CS 326: Operating Systems. Process Execution. Lecture 5

Threads (light weight processes) Chester Rebeiro IIT Madras

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

THREADS. Jo, Heeseung

Concurrency, Thread. Dongkun Shin, SKKU

Creating Threads COMP755

TCSS 422: OPERATING SYSTEMS

CS533 Concepts of Operating Systems. Jonathan Walpole

Threads. lightweight processes

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

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

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

PThreads in a Nutshell

PROGRAMOVÁNÍ V C++ CVIČENÍ. Michal Brabec

Chapter 4 Concurrent Programming

POSIX PTHREADS PROGRAMMING

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

High Performance Computing Course Notes Shared Memory Parallel Programming

Lecture 4 Threads. (chapter 4)

Processes, Threads, SMP, and Microkernels

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

Threads. Jo, Heeseung

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

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

CISC2200 Threads Spring 2015

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

Lecture 19: Shared Memory & Synchronization

Computer Systems Laboratory Sungkyunkwan University

Chapter 3 Process Description and Control

Programming Assignment #4

Global variables and resources are shared between threads within the same process. Each thread has its own stack and program counter (see Fig 2-7).

Concurrent Programming. Concurrent Programming with Processes and Threads

Creating Threads. Programming Details. COMP750 Distributed Systems

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

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

ANSI/IEEE POSIX Standard Thread management

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

Concurrency and Threads

C Grundlagen - Threads

Intro to POSIX Threads with FLTK

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

Transcription:

Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state into two parts Resource state remains with process CPU state is associated with thread Switching between threads incurs less overhead than switching between processes 5.1 Threads (continued) TCB contains: 1) thread id, priority, and state; 2) CPU state; 3) pointer to PCB of parent process; 4) TCB pointer. 5.2 1

5.3 Kernel-Level, User-Level, and Hybrid Threads Kernel-Level Threads Threads are implemented by the kernel User-Level Threads Threads are implemented by a thread library Hybrid Threads Combination of kernel-level and user-level threads 5.4 2

Kernel-Level Threads A kernel-level thread is like a process except that it has a smaller amount of state information Advantages: Provide parallelism in a multiprocessor system If a thread performs a blocking system call, the kernel can schedule another thread in the application for execution Disadvantage: Switching between threads of same process incurs the overhead of event handling 5.5 User-Level Threads Thread library maintains i thread state, t performs scheduling and switching of threads Advantage: Fast thread switching because kernel is not involved Disadvantages: Blocking of a thread blocks all threads of the process No parallelism 5.6 3

Scheduling of User-Level Threads 5.7 Hybrid Thread Models Many-to-many association permits a user-level thread to be mapped into different kernel-level threads at different times Can provide a combination of parallelism and low overhead of switching 5.8 4

Pthreads The ANSI/IEEE Portable Operating System Interface (POSIX) standard defines pthreads API For use by C language programs Provides 60 routines that perform the following: Thread management Assistance for data sharing mutual exclusion Assistance for synchronization condition variables On Linux machines #include <pthread.h> Link with the pthread library g++ threads.cc -lpthread 5.9 Pthreads: Creating int pthread_create(pthread_t *thread, pthread_attr_t *attr, void * (* start_routine) (void *), void *arg); On success A new thread is created with attributes attr (NULL=default), id of new thread is placed in thread. New thread starts executing function start_routine with parameter arg New thread executes concurrently with the calling thread New thread runs until it returns from start_routine routine or it calls pthread_exit Return 0 On failure Return non-zero error code 5.10 5

pthread_create Example void* my_thread (void *arg) { char *msg = (char *) arg; cout << Thread says << msg << \n ; } int main() { pthread_t t; char *msg = Hello World ; pthread_create(&t, NULL, my_thread, msg); return 0; } 5.11 Pthreads: Waiting int pthread_join(pthread_t th, void **thread_return) wait for a thread to terminate th: the thread to wait for thread_return: NULL or address of buffer to store return value of th When a thread terminates, its memory resources (TCB and stack) are not deallocated until another thread performs pthread_join on it. At most one thread can wait for the termination of a given thread. Return 0 on success, non-zero on failure 5.12 6

#include <pthread.h> #include <iostream> #include <unistd.h> using namespace std; int N; void *thread(void *x) { int *id = (int *)x; while (N!= *id); cout << "Thread " << *id << endl; N--; delete id; pthread_exit(0); } int main() { N = 0; pthread_t t1, t2, t3; cout << "Parent creating threads\n"; pthread_create(&t1, NULL, thread, new int(1)); pthread_create(&t2, NULL, thread, new int(2)); pthread_create(&t3, NULL, thread, new int(3)); cout << "Threads created\n"; N = 3; pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); cout << "Threads are done\n"; return 0; } 5.13 Summary A process is a model of execution of a program Can create other processes by making requests to the OS through system calls Provides parallelism li or concurrency OS allocates resources to a process and stores information about them in the context of the process To control operation of the process, OS uses notion of a process state OS keeps information concerning each process in a process control block (PCB) Process state and CPU state associated with process Scheduler selects a ready process and dispatcher switches CPU to selected process through information found in its process context and the PCB 5.14 7

Summary (continued) A thread is an alternative model of execution of a program Overhead of switching between threads is much less than the overhead of switching between processes Three models of threads: Kernel-level threads User-level threads Hybrid threads Thread models have different implications for switching overhead, concurrency, and parallelism 5.15 8