1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
Synchronizing Threads with Semaphores
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
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.
Introduction to threads
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Synchronization: Basics
Realizing Concurrency using the thread model
Concurrency, Processes and Threads
Threads in C Caryl Rahn.
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Multithreading Tutorial
Concurrency I: Threads April 10, 2001
Realizing Concurrency using Posix Threads (pthreads)
CS703 – Advanced Operating Systems
Concurrent Programming
Realizing Concurrency using the thread model
Multithreading Tutorial
CS510 Operating System Foundations
Operating System Concepts
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Multithreading Tutorial
Concurrency, Processes and Threads
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Concurrency, Processes and Threads
Lecture 20: Synchronization
Chapter 4: Threads.
Lecture 19: Threads CS April 3, 2019.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

1 Introduction to Threads Race Conditions

2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads Heap Code Data OS Stack Heap Stack

3 What are Threads? Thread -Independent stream of instructions -Basic unit of CPU utilization A thread contains -A thread ID -A register set (including the Program Counter PC) -An execution stack A thread shares with its sibling threads -The code, data and heap section -Other OS resources, such as open files and signals

4 Single and Multi-Threaded Processes

5 Multi-Threaded Processes (2) Each thread has a private stack But threads share the process address space! There’s no memory protection! Threads could potentially write into each other’s stack

6 Why use Threads? A specific example, a Web server: do {get web page request from client check if page exists and client has permissions transmit web page back to client } while(1); If transmission takes very long time, server is unable to answer other client’s requests. Solution: do {get web page request from client check if page exists and client has permissions create a thread to transmit web page back to client } while(1);

7 pthreads (5.4) Refers to the POSIX standard (IEEE c) API for thread creation and synchronization Common in UNIX operating systems

8 Java Threads (5.8) Java threads may be created by: -Extending Thread class -Implementing the Runnable interface JVM manages Java threads -Creation -Execution -Etc.

9 Race Conditions

10 Concurrent Threads Concurrent threads come into conflict with each other when they come to use shared resources Atomic actions are indivisible. In hardware, loads and stores are indivisible. On a processor, a thread switch can occur between any two atomic actions; thus the atomic actions of concurrent threads may be interleaved in any possible order. Result of concurrent execution should not depend on the order in which atomic instructions are interleaved.

11 badcnt.c : An Incorrect Program unsigned int cnt = 0; /* shared */ int main() { pthread_t tid1, tid2; pthread_create(&tid1, NULL, count, NULL); pthread_create(&tid2, NULL, count, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); if (cnt != (unsigned)NITERS*2) printf("BOOM! cnt=%d\n", cnt); else printf("OK cnt=%d\n", cnt); } /* thread routine */ void * count(void *arg) { int i; for (i=0; i<NITERS; i++) cnt++; return NULL; } linux>./badcnt BOOM! cnt= linux>./badcnt BOOM! cnt= linux>./badcnt BOOM! cnt= cnt should be equal to 200,000,000. What went wrong?! #define NITERS

12 Critical sections are blocks of code that access shared data. The objective is to make critical sections behave as if they are atomic operations: if one process uses a shared piece of data, other processes can’t access it. /* thread routine */ void * count(void *arg) { int i; for (i=0; i<NITERS; i++) cnt++; return NULL; } Critical Sections

13 Atomic Operations The statement cnt++;R = cnt R = R + 1 cnt = R must be performed atomically (cnt is shared) An atomic operation is an operation that completes in its entirety, without interruption machine level

14 Race Conditions One possible interleaving of statements is: R1 = cnt R1 = R1 + 1 R2 = cnt R2 = R2 + 1 in = R2 cnt = R1 Then cnt ends up incremented once only! Threads overwrite each other’s data. Race condition: The situation where several processes access shared data concurrently. The final value of the shared data may vary from one execution to the next.

15 Consider two threads sharing a global variable count, initially 10: What are the possible values for count after both threads finish executing: Practice Exercise count++; Thread A count--; Thread B

16 Practice Exercise Consider the following three concurrent threads that share a global variable g, initially 10: What are the possible values for g, after all three threads finish executing? g = g * 2; Thread A g = g + 1; Thread B g = g - 2; Thread C

17 Hands-on Session Complete the hands-on POSIX programming exercises posted on the class website. -Creating threads -Passing data to threads -pthread_create -pthread_exit -pthread_self -pthread_join -pthread_attach -pthread_detach