Lecture 19: Shared Memory & Synchronization

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

LSN 13 Linux Concurrency Mechanisms

Operating systems fundamentals - B06

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

POSIX Threads. HUJI Spring 2011

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

ANSI/IEEE POSIX Standard Thread management

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

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 3305 Intro to Threads. Lecture 6

CSE 333 SECTION 9. Threads

Threads. Jo, Heeseung

Introduction to PThreads and Basic Synchronization

Process Synchronization

THREADS. Jo, Heeseung

Threads. Threads (continued)

Concurrency. Johan Montelius KTH

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

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

Concurrent Server Design Multiple- vs. Single-Thread

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.

Threads. studykorner.org

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

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

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

Threads. Jo, Heeseung

Chapter 4 Concurrent Programming

pthreads CS449 Fall 2017

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Multithreaded Programming

Thread and Synchronization

518 Lecture Notes Week 5

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

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

Pthread (9A) Pthread

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

High Performance Computing Course Notes Shared Memory Parallel Programming

Introduction to Threads

CSE 333 Section 9 - pthreads

Creating Threads. Programming Details. COMP750 Distributed Systems

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

POSIX PTHREADS PROGRAMMING

Multi-threaded Programming

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Computer Systems Laboratory Sungkyunkwan University

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

PThreads in a Nutshell

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

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

Threads need to synchronize their activities to effectively interact. This includes:

CS240: Programming in C. Lecture 18: PThreads

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

POSIX Threads. Paolo Burgio

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threaded Programming. Lecture 9: Alternatives to OpenMP

Multicore and Multiprocessor Systems: Part I

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Multithreading Programming II

real time operating systems course

CS510 Operating System Foundations. Jonathan Walpole

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

Synchronization Primitives

Pthreads: POSIX Threads

In the example, since printf() was never declared, the compiler had to try to figure out what kind of function it is.

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

Programming with Shared Memory. Nguyễn Quang Hùng

Concurrency, Thread. Dongkun Shin, SKKU

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

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

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Parallel Programming with Threads

Operating System Labs. Yuanbin Wu

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

CSCE 313 Introduction to Computer Systems. Instructor: Dr. Dezhen Song

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

Shared Memory Parallel Programming

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

C Grundlagen - Threads

CS333 Intro to Operating Systems. Jonathan Walpole

CSCE 313: Intro to Computer Systems

CS 105, Spring 2007 Ring Buffer

Concurrent Programming with OpenMP

Processes and Threads

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Shared Memory Programming. Parallel Programming Overview

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

Quiz: Simple Sol Threading Model. Pthread: API and Examples Synchronization API of Pthread IPC. User, kernel and hardware. Pipe, mailbox,.

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

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

CS 6400 Lecture 11 Name:

Threads. lightweight processes

Concurrent Programming

Transcription:

Lecture 19: Shared Memory & Synchronization COMP 524 Programming Language Concepts Stephen Olivier April 16, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts

Forking int pid; pid = fork(); // Error occurred if (pid < 0) { cerr << "main: Fork failed!" << endl; exit(-1); else if (pid == 0) { cout << "Main Thread" << endl; else { cout << "Child start" << endl; cout << "Child complete" << endl; exit(0); 2

Synchronization One of the most fundamental issues in concurrent systems is how to ensure that different threads do not interfere with each other. 3

Atomic instructions One of the most important tools for implementing synchronizations protocols are atomic instructions. Atomic instructions are multiple instructions that are treated as one. For example, Test-and-set sets a boolean variable to true and returns the previous value. 4

Busy Waiting Under Busy Waiting a process continually attempts to access a critical section until it is free. Busy waiting is often implemented by a spin lock. 5

Barriers Barriers stop all threads (or a set of threads) until they reach a certain point. Busy waiting is one way implement these. There are some performance issues Tree-based barriers for O(log(n)) time 6

Semaphores Semaphore is the first synchronization method. A Semaphore has one of two states, up or down. If the semaphore is up, then a process can acquire the semaphore and change its state to down. If a semaphore is down, then no process can acquire the semaphore. There can exist semaphores that have multiple ups 7

Deadlock Deadlock occurs when two processes attempt to acquire nested resources. e.g., Task one requests A then B and task two requests B then A. Djikstra calls this the deadly embrace. 8

Monitors Monitors are similar to semaphores, except that they are directly associated with resources and a set of procedures. monitor account { int balance := 0 function withdraw(int amount) { if amount < 0 then error "Amount may not be negative" else if balance < amount then error "Insufficient funds" else balance := balance - amount function deposit(int amount) { if amount < 0 then error "Amount may not be negative" else balance := balance + amount 9

Conditional Critical Regions Conditional critical Regions are similar to monitors, except that they specify regions of code over which only one process may execute. region protected_variable when Boolean_condition do... end region. 10

Java Synchronization Before Java 5, only through use of the synchronized construct Controls access to an object class class_name { type method_name() { synchronized (object) { statement block 11

Java Synchronization Syntactic sugar lets us specify an entire method as synchronized at definition Implicit object is this rclass class_name { synchronized type method_name() { statement block 12

Java 5 Synchronization Now Java has locks: Lock l =...; l.lock(); try { // access the resource protected by this lock finally { l.unlock(); 13

Java 5 Synchronization Condition variables also built in now: Condition conditionvariable = l.newcondition();... boolean somecondition; //evaluate your wait criteria while(somecondition){ conditionvariable.await(); //re-evaluate somecondition 14

Pthreads POSIX threading library for unix-based systems Windows variants exist Used in conjunction with C/C++ Relatively low level of abstraction Supports explicit thread creation, management, synchronization, scheduling 15

#include <stdio.h> #include <stdlib.h> #include <pthread.h> Forking void *print_message_function( void *ptr ); main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf("thread 1 returns: %d\n",iret1); printf("thread 2 returns: %d\n",iret2); exit(0); void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s \n", message); 16

int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h) attr - Set to NULL if default thread attributes are used. (else define members of the struct pthread_attr_t defined in bits/pthreadtypes.h) Attributes include: detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED) scheduling policy (real-time? PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED, SCHED_OTHER) scheduling parameter inherit sched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED) scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not both.) guard size stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR) stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h), void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void. *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure. 17

void pthread_exit(void *retval); retval - Return value of thread. Pthreads don t return values, but if the thread isn t detached, then the thread ID and return value may be examined by another thread using pthread_join. *retval must not be local, otherwise it would cease to exist one the thread terminates 18

#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *functionc(); Forking pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; main() { int rc1, rc2; pthread_t thread1, thread2; if( (rc1=pthread_create( &thread1, NULL, &functionc, NULL)) ){ printf("thread creation failed: %d\n", rc1); if( (rc2=pthread_create( &thread2, NULL, &functionc, NULL)) ){ printf("thread creation failed: %d\n", rc2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); void *functionc() { pthread_mutex_lock( &mutex1 ); counter++; printf("counter value: %d\n",counter); pthread_mutex_unlock( &mutex1 ); 19

#include <stdio.h> #include <pthread.h> Forking #define NTHREADS 10 void *thread_function(void *); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; main() { pthread_t thread_id[nthreads]; int i, j; for(i=0; i < NTHREADS; i++) { pthread_create( &thread_id[i], NULL, thread_function, NULL ); for(j=0; j < NTHREADS; j++) { pthread_join( thread_id[j], NULL); printf("final counter value: %d\n", counter); void *thread_function(void *dummyptr) { printf("thread number %ld\n", pthread_self()); pthread_mutex_lock( &mutex1 ); counter++; pthread_mutex_unlock( &mutex1 ); 20

Forking #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER; void *functioncount1(); void *functioncount2(); int count = 0; #define COUNT_DONE 10 #define COUNT_HALT1 3 #define COUNT_HALT2 6 main() { pthread_t thread1, thread2; pthread_create( &thread1, NULL, &functioncount1, NULL); pthread_create( &thread2, NULL, &functioncount2, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); 21

void *functioncount1() { for(;;) { Forking pthread_mutex_lock( &condition_mutex ); while( count >= COUNT_HALT1 && count <= COUNT_HALT2 ){ pthread_cond_wait( &condition_cond, &condition_mutex ); pthread_mutex_unlock( &condition_mutex ); pthread_mutex_lock( &count_mutex ); count++; printf("counter value functioncount1: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(null); void *functioncount2(){ for(;;) { pthread_mutex_lock( &condition_mutex ); if( count < COUNT_HALT1 count > COUNT_HALT2 ){ pthread_cond_signal( &condition_cond ); pthread_mutex_unlock( &condition_mutex ); pthread_mutex_lock( &count_mutex ); count++; printf("counter value functioncount2: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(null); 22

Remote Procedure Call (RPC) Message passing (rather than shared memory) approach to communication Works across distributed systems Allows higher level of abstraction than network API s like sockets Leverage type checking and/or OO programming Main problem is packing, sending, and unpacking parameters efficiently while preserving semantics This is called marshalling 23

RPC Implementations CORBA is a language & OS independent solution Free & commercial implementations Microsoft DCOM and later.net remoting Java Remote Method Invocation (RMI) 24