Threads Tuesday, September 28, :37 AM

Size: px
Start display at page:

Download "Threads Tuesday, September 28, :37 AM"

Transcription

1 Threads_and_fabrics Page 1 Threads Tuesday, September 28, :37 AM Threads A process includes an execution context containing Memory map PC and register values. Switching between memory maps can take time. Idea: share the memory map between a group of cooperating "lightweight processes". Do maintain separate PC, registers. Don't maintain separate memory maps. All "lightweight processes" share one memory map => it's "shared memory" from their point of view. Voila: we have the concept of a "thread". Reading: Stallings Chapter 4.

2 Threads_and_fabrics Page 2 What threads do for us: Enable efficient producer-consumer code. (Simplify programming of complex algorithms involving multiple kinds of buffering.) Allow extremely efficient locking mechanisms for avoiding resource conflicts (more efficient than for two independent processes).

3 Threads_and_fabrics Page 3 Problems of thread programming Making sure that threads collaborate reasonably in utilizing shared resources (Havoc can result if two threads try to read from the same I/O descriptor in an unstructured way) Avoiding race conditions in utilizing shared memory (E.g., doing something that affects what another process is doing)

4 Threads_and_fabrics Page 4 Example of a race condition between threads shared data structure: queue four operations: empty, full, dequeue, enqueue. #define SIZE 1024 int bot=0, top=0; char queue[size]; int empty() { return bot==top; int full() { return ((top+1)%size)==bot; void enqueue(char c) { if (!full()) { queue[top]=c; top=(top+1)%size; char dequeue() { if (!empty()) { char out = queue[bot]; bot=(bot+1)%size; return out; else { return 0;

5 Threads_and_fabrics Page 5

6 Threads_and_fabrics Page 6

7 Threads_and_fabrics Page 7

8 Threads_and_fabrics Page 8 What can go wrong? If these operations are utilized in one process, nothing can go wrong. If they are utilized by two threads, however, they are not atomic. In particular, it is possible that one thread will be in the process of enqueuing while another tries to enqueue. Result: queue overflow: top gets incremented after full() condition is reached; queue becomes empty! Class exercise: what else can go wrong with this code?

9 Threads_and_fabrics Page 9 Thread execution Tuesday, September 28, :37 AM Review of the memory map for a process

10 Threads_and_fabrics Page 10 Segmentation violation: core dumped attempt to read or write from blank space between stack and heap Bus error: attempt to write something that is read only. (literally, an attempt to use the bus in an illegal addressing mode) Recall that: a read from a location requires setting the address bus requesting the read a write to a location requires setting the address bus putting data to write on the data bus requesting the write A bus error is attempting to do the second when only the first is allowed.

11 Threads_and_fabrics Page 11 A thread (of execution) within a process is an execution subcontext that Shares a memory map with other threads in the process. Has a separate PC and register values. Has its own execution stack.

12 Threads_and_fabrics Page 12 Normal process: has its own (private) memory map. Thread: shares memory map with other threads within a process.

13 Threads_and_fabrics Page 13 "Lightweight" processes Lower context-switch time Lower resource use than for independent processes. Faster switching between threads than between separate processes Shared memory programming model.

14 Single-threaded processes Threads_and_fabrics Page 14

15 Threads_and_fabrics Page 15 Multi-threaded process model Class exercise: why separate stacks?

16 Threads_and_fabrics Page 16 Why separate stacks? Kernel/user: problem of state explosion/reproducibility of execution Uninitialized local variable values are inherited from prior use of stack memory. So kernel interrupts will put unpredictable values into local variables. Bug conditions will not be reproducible. Thread stacks: problem of return Each thread has a stack of calls. Call stacks are independent. Your "stack" would look like a heap!

17 Thread properties Fast spawn and exit. Quick context switches. Threads_and_fabrics Page 17

18 Context switching Threads_and_fabrics Page 18

19 Locks idea of a lock: other threads "stop" when one thread wants to run exclusively. cost of a lock: how much CPU time it takes to implement it. locks between processes: take kernel memory, and a system call. locks between threads: utilize user memory, no system call. Threads_and_fabrics Page 19

20 Threads_and_fabrics Page 20 Thread uses Tuesday, September 28, :15 PM Thread uses Simplifying logic and improving performance of complex latency hiding strategies. Avoiding confusing use of signals.

21 Latency hiding Make multiple requests from servers. Get answers asychronously Combine at end. "fork-join parallelism" Used in web browsers. Threads_and_fabrics Page 21

22 Latency hiding example Threads_and_fabrics Page 22

23 Without threads Must wait for each response Must maintain each connection Threads_and_fabrics Page 23

24 with threads: Threads_and_fabrics Page 24

25 Threads_and_fabrics Page 25 Problem with threads Thursday, October 01, :33 PM Problems with threads You must use locking when threads access shared memory. Otherwise, we have state botches in which one thread gets an invalid picture of memory.

26 Threads_and_fabrics Page 26 Critical sections Monday, October 02, :19 PM Fixing this problem: critical sections A critical section of code is one that should be atomic with respect to other threads. Example: #define SIZE 1024 int bot=0, top=0; char queue[size]; int empty() { // BEGIN CRITICAL SECTION int result = (bot==top); // END CRITICAL SECTION return result; int full() { // BEGIN CRITICAL SECTION int result = (((top+1)%size)==bot); // END CRITICAL SECTION return result; void enqueue(char c) { // BEGIN CRITICAL SECTION if ((top+1)%size!=bot) { queue[top]=c; top=(top+1)%size;

27 Threads_and_fabrics Page 27 top=(top+1)%size; // END CRITICAL SECTION char dequeue() { int out; // BEGIN CRITICAL SECTION if (bot!=top) { out = queue[bot]; bot=(bot+1)%size; else { out = 0; // END CRITICAL SECTION return out;

28 Threads_and_fabrics Page 28 Claim: if we limit each critical section so that no more than one thread can be in it at a time, there is no way to corrupt the data structure. How this works: critical sections make decisions and then act upon them. if we avoid interrupting the decision, once made, then we won't corrupt the data structure. In other words, we can avoid concurrency problems by making critical sections atomic.

29 One way of making sections atomic: shared locks. A binary mutual exclusion lock (mutex) is a data structure with two methods: lock(): block if anyone else has locked; then acquire lock and return when it is possible to get exclusive access. unlock(): release a previously acquired lock. Does nothing if one does not have the lock. Threads_and_fabrics Page 29

30 Threads_and_fabrics Page 30 Locks in action: #define SIZE 1024 int bot=0, top=0; char queue[size]; int empty() { lock(); int result = (bot==top); unlock(); return result; int full() { lock(); int result = (((top+1)%size)==bot); unlock(); return result; void enqueue(char c) { lock(); if ((top+1)%size!=bot) { queue[top]=c; top=(top+1)%size; unlock(); char dequeue() { int out; lock(); if (bot!=top) { out = queue[bot];

31 Threads_and_fabrics Page 31 q [ ] bot=(bot+1)%size; else { out = 0; unlock(); return out;

32 Threads_and_fabrics Page 32 Kinds of locks: in shared user memory: mutexes shared between threads of one process in kernel memory: semaphores can be shared between processes in a network of computers: semaphores and barriers What's a mutex? Basically, one word of user memory. Utilize a simple race condition to acquire lock, based upon atomicity of operations in assembly language If the word of memory is > 0, then a lock has been acquired. If the word of memory == 0, then there is no lock. (more details on this later) What's a kernel semaphore? Basically, a word of kernel memory. System calls increment and decrement the word of memory; these are atomic because during the call, the scheduler is not allowed to interrupt execution! (This is the concept of "kernel critical section"!) semaphore >=0 implies unlocked. semaphore < 0 implies locked. Can be used as a counter for tracking resource allocation What's a network semaphore? A data structure shared among processes on different computers.

33 Threads_and_fabrics Page 33 gp different computers. Can be implemented many different ways. Behavior same as kernel semaphore, but much slower in execution (time between someone's unlock() and someone else's lock() is longer). What's a barrier? A simple way of insuring that all processes are at the same point in a computation. Everybody calls "barrier()", and then no one proceeds until everyone has reached the barrier. Used heavily in parallel computation. Aside: implementing a barrier. Barrier implementation comes from a simple idea: if you send something to someone else, and receive a reply, then you are in a predictable state. So an easy way to implement a barrier: Everyone sends a message to a coordinator, and receives a response only when all processes report in. Easy implementation: network broadcast.

34 Threads_and_fabrics Page 34 Fabrics Tuesday, September 28, :37 AM Fabric: a cluster of cooperating entities Example: loosely-coupled multiprocessing: a group of otherwise unrelated computers collaborate on a single computation. Example: multi-core computing: two CPU's collaborate on serving a run queue. But how does one "weave threads into fabrics?" One key to this: the concept of "parallelism grain"

35 Parallelism Grain A measure of how coupled two entities are. Fine-grain parallelism: entities can exchange information efficiently. Example: shared memory. Exchange is instant; all one does is to read the memory cell that another process wrote. Coarse-grain parallelism: exchanging information is expensive and infrequent. Example: message-passing. Exchange is infrequent; requires sending a network packet from one entity to another; expensive; packet must be Threads_and_fabrics Page 35

36 Threads_and_fabrics Page 36 The grain hierarchy two threads of same process: fine-grain; closest to one another; share memory maps; most memory shared. two independent processes on same single-processor computer: intermediate grain, can share kernel memory (pipes), can (with some trouble) share user pages as well. (two independent processes in two different cores of the same computer: same as two processes on same computer, except that both can be executing concurrently.) two independent processes on different computers. Coarse grain; can communicate via message passing (UDP) or streams (TCP); no shared memory possible. (weird stuff: multiport disk drives allow sharing of one disk among two processors; can thus share disk memory rather than physical memory. Intermediate grain: physical sharing is finer-grain than being on two independent computers, but coarser than being co-resident on one computer)

37 Threads_and_fabrics Page 37 Basic principle of parallelism grain: The closer two processes are to one another in space, the easier it is to communicate, and the finer the communication grain can be. Examples: Threads: shared memory Processes: pipes Computers: sockets (TCP/IP) Q: So what's the difference between threads and processe from the point of view of communication grain? A: Two threads can share user memory, while two independent processes can only share kernel memory!

38 Threads_and_fabrics Page 38 Thread example Tuesday, September 28, :08 PM #include <pthread.h> struct inputdata { // input to thread char name[20]; ; struct outputdata { // output from thread int idno; x ; // must have lifetime greater than thread void *threaded_routine (void * v) { struct inputdata *d = (struct inputdata *)v; printf ("hello from the thread!\n"); printf ("my name is %s\n",d->name); sleep(5); printf ("setting idno\n"); x.idno = 42; printf ("bye from the thread!\n"); return (void *) &x; main() { pthread_t thread; printf("hello from the parent... creating thread\n"); struct inputdata input; strcpy(input.name,"george"); struct outputdata *retptr; if (pthread_create( &thread, NULL, threaded_routine, (void *)&input)==0) { printf("pretend to do useful work.\n"); pthread_join(thread,(void **)&retptr); printf("got id number %d\n",retptr->idno); else { printf("could not create thread!\n"); printf("bye from the parent\n");

39 Threads_and_fabrics Page 39 printf("bye from the parent\n");

40 Threads_and_fabrics Page 40 Whoa there! Lots of sophisticated stuff going on here: void *threaded_routine (void * v); Threaded routine can return a pointer to any type; one must cast it! In main: pthread_create( &thread, NULL, threaded_routine, (void *)&input) &thread: pointer to place to store thread id. threaded_routine: subroutine to call. (void *)&input: a pointer to the subroutine argument, with its type "forgotten". In threaded_routine: struct inputdata *d = (struct inputdata *)v; "remember the type" of the argument.

41 Threads_and_fabrics Page 41 Alternative: function pointer casts. Struct outputdata * sewingmachine (struct inputdata *d) { Sewingmachine: struct outputdata *(*)(struct inputdata *) Want: void *(*)(void *) pthread_create( &thread, NULL, (void *(*)(void *))sewingmachine, (void *)&input)==0) {...

42 Threads_and_fabrics Page 42 In threaded routine: return (void *) &x; Forget the type of x! In main: pthread_join(thread,(void **)&retptr); Remember that the return value is a (void *); but cast the returned pointer to the proper type.

43 Threads_and_fabrics Page 43 Thread Threats Tuesday, September 28, :19 PM Threads are dangerous: #include <pthread.h> int x=0; void *threaded_routine (void * v) { const int *n = (int *)v; int i; for (i=0; i<10; i++) { int y=x; y++; sleep(1); x=y; printf("%d: x=%d\n",*n,x); main() { pthread_t thread1, thread2, thread3; void *retptr; int n1=1,n2=2,n3=3; pthread_create( &thread1, NULL, threaded_routine, (void *)&n1); pthread_create( &thread2, NULL, threaded_routine, (void *)&n2); pthread_create( &thread3, NULL, threaded_routine, (void *)&n3);

44 Threads_and_fabrics Page 44 threaded_routine, (void *)&n3); pthread_join(thread1,(void **)&retptr); pthread_join(thread2,(void **)&retptr); pthread_join(thread3,(void **)&retptr); printf ("x = %d (should be 30)\n",x);

45 The previous example prints "10"! Threads_and_fabrics Page 45

46 Threads_and_fabrics Page 46 Locking Tuesday, September 28, :24 PM Locking to prevent races: #include <pthread.h> pthread_mutex_t locker; // pthread_mutex_init(&locker, NULL); // pthread_mutex_lock(&locker); // pthread_mutex_trylock(&locker); // pthread_mutex_unlock(&locker); // pthread_mutex_unlock(&locker); // pthread_mutex_destroy(&locker); int x=0; void *threaded_routine (void * v) { const int *n = (int *)v; int i; for (i=0; i<10; i++) { pthread_mutex_lock(&locker); int y=x; y++; sleep(1); x=y; printf("%d: x=%d\n",*n,x); pthread_mutex_unlock(&locker); main() { pthread_t thread1, thread2, thread3; void *retptr; int n1=1,n2=2,n3=3; pthread_mutex_init(&locker, NULL); pthread_create( &thread1, NULL, threaded_routine, (void *)&n1); pthread_create( &thread2, NULL, threaded_routine, (void *)&n2); pthread_create( &thread3, NULL, threaded_routine, (void *)&n3); pthread_join(thread1,(void **)&retptr);

47 Threads_and_fabrics Page 47 pthread_join(thread1,(void **)&retptr); pthread_join(thread2,(void **)&retptr); pthread_join(thread3,(void **)&retptr); printf ("x = %d (should be 30)\n",x); pthread_mutex_destroy(&locker);

48 Threads_and_fabrics Page 48 Then the bad news: deadlock It is really easy to construct locking schemes that have deadlock states in which neither process can proceed.

So far, we've seen situations in which locking can improve reliability of access to critical sections.

So far, we've seen situations in which locking can improve reliability of access to critical sections. Locks Page 1 Using locks Monday, October 6, 2014 9:49 AM So far, we've seen situations in which locking can improve reliability of access to critical sections. In general, how can one use locks? Locks

More information

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 CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

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 CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution

More information

CS533 Concepts of Operating Systems. Jonathan Walpole

CS533 Concepts of Operating Systems. Jonathan Walpole CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?

More information

CS 3305 Intro to Threads. Lecture 6

CS 3305 Intro to Threads. Lecture 6 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

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2018 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

Introduction to PThreads and Basic Synchronization

Introduction to PThreads and Basic Synchronization Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization

More information

Concurrent Server Design Multiple- vs. Single-Thread

Concurrent Server Design Multiple- vs. Single-Thread Concurrent Server Design Multiple- vs. Single-Thread Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN NTUT, TAIWAN 1 Examples Using

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization Administrivia Homework 1 Due today by the end of day Hopefully you have started on project 1 by now? Kernel-level threads (preemptable

More information

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

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control Processes & Threads Concurrent Programs Process = Address space + one thread of control Concurrent program = multiple threads of control Multiple single-threaded processes Multi-threaded process 2 1 Concurrent

More information

High Performance Computing Course Notes Shared Memory Parallel Programming

High Performance Computing Course Notes Shared Memory Parallel Programming High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed

More information

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011 Synchronization CS61, Lecture 18 Prof. Stephen Chong November 3, 2011 Announcements Assignment 5 Tell us your group by Sunday Nov 6 Due Thursday Nov 17 Talks of interest in next two days Towards Predictable,

More information

There are, of course, many other possible solutions and, if done correctly, those received full credit.

There are, of course, many other possible solutions and, if done correctly, those received full credit. Question 1. (20 points) STL. Complete the function ChangeWords below. This function has as inputs a vector of strings, and a map of key-value pairs. The function should return a new vector

More information

So far, we know: Wednesday, October 4, Thread_Programming Page 1

So far, we know: Wednesday, October 4, Thread_Programming Page 1 Thread_Programming Page 1 So far, we know: 11:50 AM How to create a thread via pthread_mutex_create How to end a thread via pthread_mutex_join How to lock inside a thread via pthread_mutex_lock and pthread_mutex_unlock.

More information

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

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

Coupling Thursday, October 21, :23 PM

Coupling Thursday, October 21, :23 PM Coupling Page 1 Coupling Thursday, October 21, 2004 3:23 PM Two kinds of multiple-processor systems Tightly-coupled Can share efficient semaphores. Usually involve some form of shared memory. Loosely-coupled

More information

Multicore and Multiprocessor Systems: Part I

Multicore and Multiprocessor Systems: Part I Chapter 3 Multicore and Multiprocessor Systems: Part I Max Planck Institute Magdeburg Jens Saak, Scientific Computing II 44/337 Symmetric Multiprocessing Definition (Symmetric Multiprocessing (SMP)) The

More information

POSIX / System Programming

POSIX / System Programming POSIX / System Programming ECE 650 Methods and Tools for Software Eng. Guest lecture 2017 10 06 Carlos Moreno cmoreno@uwaterloo.ca E5-4111 2 Outline During today's lecture, we'll look at: Some of POSIX

More information

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas HPCSE - I «Introduction to multithreading» Panos Hadjidoukas 1 Processes and Threads POSIX Threads API Outline Thread management Synchronization with mutexes Deadlock and thread safety 2 Terminology -

More information

PThreads in a Nutshell

PThreads in a Nutshell PThreads in a Nutshell Chris Kauffman CS 499: Spring 2016 GMU Logistics Today POSIX Threads Briefly Reading Grama 7.1-9 (PThreads) POSIX Threads Programming Tutorial HW4 Upcoming Post over the weekend

More information

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

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012 Pre-lab #2 tutorial ECE 254 Operating Systems and Systems Programming May 24, 2012 Content Concurrency Concurrent Programming Thread vs. Process POSIX Threads Synchronization and Critical Sections Mutexes

More information

20-EECE-4029 Operating Systems Fall, 2015 John Franco

20-EECE-4029 Operating Systems Fall, 2015 John Franco 20-EECE-4029 Operating Systems Fall, 2015 John Franco First Exam Question 1: Barrier name: a) Describe, in general terms, what a barrier is trying to do Undo some of the optimizations that processor hardware

More information

Parallel Programming Languages COMP360

Parallel Programming Languages COMP360 Parallel Programming Languages COMP360 The way the processor industry is going, is to add more and more cores, but nobody knows how to program those things. I mean, two, yeah; four, not really; eight,

More information

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole Threads & Concurrency 2 Why Use Threads? Utilize multiple CPU s concurrently Low cost communication via shared memory Overlap computation and blocking

More information

Parallel Programming: Background Information

Parallel Programming: Background Information 1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same

More information

Parallel Programming: Background Information

Parallel Programming: Background Information 1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same

More information

CS333 Intro to Operating Systems. Jonathan Walpole

CS333 Intro to Operating Systems. Jonathan Walpole CS333 Intro to Operating Systems Jonathan Walpole Threads & Concurrency 2 Threads Processes have the following components: - an address space - a collection of operating system state - a CPU context or

More information

TCSS 422: OPERATING SYSTEMS

TCSS 422: OPERATING SYSTEMS TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread

More information

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

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019 CS 31: Introduction to Computer Systems 22-23: Threads & Synchronization April 16-18, 2019 Making Programs Run Faster We all like how fast computers are In the old days (1980 s - 2005): Algorithm too slow?

More information

THREADS: (abstract CPUs)

THREADS: (abstract CPUs) CS 61 Scribe Notes (November 29, 2012) Mu, Nagler, Strominger TODAY: Threads, Synchronization - Pset 5! AT LONG LAST! Adversarial network pong handling dropped packets, server delays, overloads with connection

More information

CS-537: Midterm Exam (Spring 2001)

CS-537: Midterm Exam (Spring 2001) CS-537: Midterm Exam (Spring 2001) Please Read All Questions Carefully! There are seven (7) total numbered pages Name: 1 Grading Page Points Total Possible Part I: Short Answers (12 5) 60 Part II: Long

More information

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

So far, system calls have had easy syntax. Integer, character string, and structure arguments. Pointers Page 1 So far, system calls have had easy syntax Wednesday, September 30, 2015 10:45 AM Integer, character string, and structure arguments. But this is not always true. Today, we begin to explore

More information

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 22 Shared-Memory Concurrency 1 Administrivia HW7 due Thursday night, 11 pm (+ late days if you still have any & want to use them) Course

More information

Chapter 4 Concurrent Programming

Chapter 4 Concurrent Programming Chapter 4 Concurrent Programming 4.1. Introduction to Parallel Computing In the early days, most computers have only one processing element, known as the Central Processing Unit (CPU). Due to this hardware

More information

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency, Thread. Dongkun Shin, SKKU Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point

More information

More Shared Memory Programming

More Shared Memory Programming More Shared Memory Programming Shared data structures We want to make data structures that can be shared by threads. For example, our program to copy a file from one disk to another used a shared FIFO

More information

CS 261 Fall Mike Lam, Professor. Threads

CS 261 Fall Mike Lam, Professor. Threads CS 261 Fall 2017 Mike Lam, Professor Threads Parallel computing Goal: concurrent or parallel computing Take advantage of multiple hardware units to solve multiple problems simultaneously Motivations: Maintain

More information

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory 1 Example Programs Initially, A = B = 0 P1 P2 A = 1 B = 1 if (B == 0) if (A == 0) critical section

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

Programming in Parallel COMP755

Programming in Parallel COMP755 Programming in Parallel COMP755 All games have morals; and the game of Snakes and Ladders captures, as no other activity can hope to do, the eternal truth that for every ladder you hope to climb, a snake

More information

CS 326: Operating Systems. Process Execution. Lecture 5

CS 326: Operating Systems. Process Execution. Lecture 5 CS 326: Operating Systems Process Execution Lecture 5 Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 2 Today s Schedule Process Creation

More information

Synchronization I. To do

Synchronization I. To do Synchronization I To do q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors Cooperating processes Cooperating processes need

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

POSIX Threads. HUJI Spring 2011

POSIX Threads. HUJI Spring 2011 POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time

More information

Monitors; Software Transactional Memory

Monitors; Software Transactional Memory Monitors; Software Transactional Memory Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico October 18, 2012 CPD (DEI / IST) Parallel and

More information

pthreads CS449 Fall 2017

pthreads CS449 Fall 2017 pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate

More information

Synchronization I. Today. Next time. Race condition, critical regions and locks. Condition variables, semaphores and Monitors

Synchronization I. Today. Next time. Race condition, critical regions and locks. Condition variables, semaphores and Monitors Synchronization I Today Race condition, critical regions and locks Next time Condition variables, semaphores and Monitors Cooperating processes Cooperating processes need to communicate They can affect/be

More information

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems CSE 153 Design of Operating Systems Winter 19 Lecture 7/8: Synchronization (1) Administrivia How is Lab going? Be prepared with questions for this weeks Lab My impression from TAs is that you are on track

More information

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful? Chapter 2: Threads: Questions CSCI [4 6]730 Operating Systems Threads!! How is a thread different from a process?!! Why are threads useful?!! How can OSIX threads be useful?!! What are user-level and kernel-level

More information

Multitasking. Embedded Systems

Multitasking. Embedded Systems Multitasking in Embedded Systems 1 / 39 Multitasking in Embedded Systems v1.0 Multitasking in ES What is Singletasking? What is Multitasking? Why Multitasking? Different approaches Realtime Operating Systems

More information

Introduction to OS Synchronization MOS 2.3

Introduction to OS Synchronization MOS 2.3 Introduction to OS Synchronization MOS 2.3 Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Introduction to OS 1 Challenge How can we help processes synchronize with each other? E.g., how

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole The Process Concept 2 The Process Concept Process a program in execution Program - description of how to perform an activity instructions and static

More information

just a ((somewhat) safer) dialect.

just a ((somewhat) safer) dialect. Intro_to_C Page 1 Intro to C Tuesday, September 07, 2004 5:30 PM C was developed specifically for writing operating systems Low level of abstraction. "Just above machine language." Direct access to the

More information

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018 SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

ECE 574 Cluster Computing Lecture 8

ECE 574 Cluster Computing Lecture 8 ECE 574 Cluster Computing Lecture 8 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 16 February 2017 Announcements Too many snow days Posted a video with HW#4 Review HW#5 will

More information

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling. Background The Critical-Section Problem Background Race Conditions Solution Criteria to Critical-Section Problem Peterson s (Software) Solution Concurrent access to shared data may result in data inconsistency

More information

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

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization CS-345 Operating Systems Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization Threads A thread is a lightweight process A thread exists within a process and uses the process resources. It

More information

Multiprocessors 2007/2008

Multiprocessors 2007/2008 Multiprocessors 2007/2008 Abstractions of parallel machines Johan Lukkien 1 Overview Problem context Abstraction Operating system support Language / middleware support 2 Parallel processing Scope: several

More information

Monitors; Software Transactional Memory

Monitors; Software Transactional Memory Monitors; Software Transactional Memory Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico March 17, 2016 CPD (DEI / IST) Parallel and Distributed

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

Real-Time Programming

Real-Time Programming Real-Time Programming Week 7: Real-Time Operating Systems Instructors Tony Montiel & Ken Arnold rtp@hte.com 4/1/2003 Co Montiel 1 Objectives o Introduction to RTOS o Event Driven Systems o Synchronization

More information

CS516 Programming Languages and Compilers II

CS516 Programming Languages and Compilers II CS516 Programming Languages and Compilers II Zheng Zhang Spring 2015 Mar 12 Parallelism and Shared Memory Hierarchy I Rutgers University Review: Classical Three-pass Compiler Front End IR Middle End IR

More information

2 Threads vs. Processes

2 Threads vs. Processes 9 2 Threads vs. Processes A process includes an address space (defining all the code and data pages) a resource container (OS resource and accounting information) a thread of control, which defines where

More information

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and Synchronization I Today! Race condition, critical regions and locks Next time! Condition variables, semaphores and Monitors Cooperating processes! Cooperating processes need to communicate They can affect/be

More information

Concurrency and Threads

Concurrency and Threads Concurrency and Threads CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

POSIX Threads: a first step toward parallel programming. George Bosilca

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Fall 2017 1 Outline Inter-Process Communication (20) Threads

More information

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019 Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019 Bringing It Together OS has three hats: What are they? Processes help with one? two? three? of those hats OS protects itself

More information

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful? Chapter 2: Threads: Questions CSCI [4 6]730 Operating Systems Threads! How is a thread different from a process?! Why are threads useful?! How can OSIX threads be useful?! What are user-level and kernel-level

More information

CSC369 Lecture 2. Larry Zhang, September 21, 2015

CSC369 Lecture 2. Larry Zhang, September 21, 2015 CSC369 Lecture 2 Larry Zhang, September 21, 2015 1 Volunteer note-taker needed by accessibility service see announcement on Piazza for details 2 Change to office hour to resolve conflict with CSC373 lecture

More information

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score CS 3214 Here is the distribution of midterm scores for both sections (combined). Scores 20 18 16 14 12 10 8 6 4 2 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 0 The overall average was 58 points.

More information

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process Goals Processes and Threads Process vs. Kernel Thread vs. User Green Threads Thread Cooperation Synchronization Implementing Concurrency Concurrency Uniprogramming: Execute one program at a time EX: MS/DOS,

More information

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

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid. Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter

More information

CS 111. Operating Systems Peter Reiher

CS 111. Operating Systems Peter Reiher Operating System Principles: Mutual Exclusion and Asynchronous Completion Operating Systems Peter Reiher Page 1 Outline Mutual Exclusion Asynchronous Completions Page 2 Mutual Exclusion Critical sections

More information

POSIX Threads: a first step toward parallel programming. George Bosilca

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information

ENCM 501 Winter 2019 Assignment 9

ENCM 501 Winter 2019 Assignment 9 page 1 of 6 ENCM 501 Winter 2019 Assignment 9 Steve Norman Department of Electrical & Computer Engineering University of Calgary April 2019 Assignment instructions and other documents for ENCM 501 can

More information

Concurrent Programming

Concurrent Programming Concurrent Programming CS 485G-006: Systems Programming Lectures 32 33: 18 20 Apr 2016 1 Concurrent Programming is Hard! The human mind tends to be sequential The notion of time is often misleading Thinking

More information

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved. Processes Prof. James L. Frankel Harvard University Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved. Process Model Each process consists of a sequential program

More information

Threaded Programming. Lecture 9: Alternatives to OpenMP

Threaded Programming. Lecture 9: Alternatives to OpenMP Threaded Programming Lecture 9: Alternatives to OpenMP What s wrong with OpenMP? OpenMP is designed for programs where you want a fixed number of threads, and you always want the threads to be consuming

More information

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011 Administrative Thursday s class Meet in WEB L130 to go over programming assignment Midterm on Thursday

More information

Introduction to Embedded Systems

Introduction to Embedded Systems Introduction to Embedded Systems Edward A. Lee & Sanjit Seshia UC Berkeley EECS 124 Spring 2008 Copyright 2008, Edward A. Lee & Sanjit Seshia, All rights reserved Lecture 17: Concurrency 2: Threads Definition

More information

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

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread

More information

Shared Memory Programming. Parallel Programming Overview

Shared Memory Programming. Parallel Programming Overview Shared Memory Programming Arvind Krishnamurthy Fall 2004 Parallel Programming Overview Basic parallel programming problems: 1. Creating parallelism & managing parallelism Scheduling to guarantee parallelism

More information

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018 1 Introduction Green, green threads of home Johan Montelius HT2018 This is an assignment where you will implement your own thread library. Instead of using the operating systems threads you will create

More information

Parallelism Marco Serafini

Parallelism Marco Serafini Parallelism Marco Serafini COMPSCI 590S Lecture 3 Announcements Reviews First paper posted on website Review due by this Wednesday 11 PM (hard deadline) Data Science Career Mixer (save the date!) November

More information

Midterm Exam. October 20th, Thursday NSC

Midterm Exam. October 20th, Thursday NSC CSE 421/521 - Operating Systems Fall 2011 Lecture - XIV Midterm Review Tevfik Koşar University at Buffalo October 18 th, 2011 1 Midterm Exam October 20th, Thursday 9:30am-10:50am @215 NSC Chapters included

More information

First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems

First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems University of California, Berkeley College of Engineering Computer Science Division EECS Fall 2018 Ion Stoica First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems Your Name: SID AND 162

More information

CS444 1/28/05. Lab 03

CS444 1/28/05. Lab 03 CS444 1/28/05 Lab 03 Note All the code that is found in this lab guide can be found at the following web address: www.clarkson.edu/class/cs444/cs444.sp2005/labs/lab03/code/ Threading A thread is an independent

More information

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA Chapter 6: Process [& Thread] Synchronization CSCI [4 6] 730 Operating Systems Synchronization Part 1 : The Basics! Why is synchronization needed?! Synchronization Language/Definitions:» What are race

More information

Operating systems fundamentals - B06

Operating systems fundamentals - B06 Operating systems fundamentals - B06 David Kendall Northumbria University David Kendall (Northumbria University) Operating systems fundamentals - B06 1 / 12 Introduction Introduction to threads Reminder

More information

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course Exercise 1 (20 points). Autotest. Answer the quiz questions in the following table. Write the correct answer with its corresponding letter. For each 3 wrong answer, one correct answer will be subtracted

More information