Threads A thread is an alternative model of program execution

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

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 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Chapter 5 Processes and Threads Copyright © 2008.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Process Concept An operating system executes a variety of programs
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
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.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Department of Computer Science and Software Engineering
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.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
1 OS Review Processes and Threads Chi Zhang
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
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:
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Day 12 Threads.
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Threads.
Chapter 4: Threads.
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Operating System Concepts
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Presentation transcript:

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 1

Threads (continued) TCB contains: 1) thread id, priority, and state; 2) CPU state; 3) pointer to PCB of parent process; 4) TCB pointer. 2

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 4

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 If selected process belongs to a different process, switch between processes 5

User-Level Threads Thread library maintains thread state, 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 Kernel not aware of user-level threads in a process Threads must not make system calls that can lead to blocking, a user thread performing a blocking system call will cause entire process to block Concurrency is seriously impaired if a thread makes a system call that leads to blocking 6

Scheduling of User-Level Threads 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 8

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

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” or it calls “pthread_exit” Return 0 On failure Return non-zero error code

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;

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 pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks.

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; } #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 * p1 = new int Parent creating threads, Threads created, Thread 3, Thread 2, Thread 1 Threads are done

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 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 14

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 15