MULTITHREADED PROGRAMMING Processes vs. Threads Process states and state transitions Hazards and Semaphores 1.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Nachos.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Parallel Programming Models Jihad El-Sana These slides are based on the book: Introduction to Parallel Computing, Blaise Barney, Lawrence Livermore National.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 Confidential Enterprise Solutions Group Process and Threads.
Games Development 2 Concurrent Programming CO3301 Week 9.
Internet Software Development Controlling Threads Paul J Krause.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12B - Synchronization and semaphores Programming Language.
Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
CRITICAL SECTIONS Dijkstra’s Semaphores. Multiprogramming Multiple programs sharing a processor Ex) Your is open and receiving A web browser.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety of.
Big Picture Lab 4 Operating Systems C Andras Moritz
Tutorial 2: Homework 1 and Project 1
Interprocess Communication Race Conditions
Processes and threads.
Process Synchronization
Outline for this evening
Deadlocks (part II) Deadlock avoidance – (not reasonable)
Threading And Parallel Programming Constructs
Jonathan Walpole Computer Science Portland State University
Multithreaded Programming
Concurrency: Mutual Exclusion and Process Synchronization
Operating Systems (CS 340 D)
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 3: Process Management
Presentation transcript:

MULTITHREADED PROGRAMMING Processes vs. Threads Process states and state transitions Hazards and Semaphores 1

What is a process? (Operating Systems) In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. A computer program is a passive collection of instructions A process is the actual execution of those instructions. Several processes may be associated with the same program. Note: opening up several instances of the same program often means more than one process is being executed. 2

Process States 3

Question Suppose you are writing software to run a clock. Your have a method/function void addOneSecond() that adds one second to the current time of day. You have a method/function void setTime(h,m) that sets the time of day. You want to be able to run addOneSecond() forever and occasionally run setTime() How can you do this? 4

Possible solution in pseudo code while (1) { addOneSecond(); sleep(1 second); } setTime(3,20); Issue: setTime will never run 5

Possible solution in pseudo code setTime(3,20); while (1) { addOneSecond(); sleep(1 second); } Issue: setTime will never run a second time 6

Process Control Blocks (PCBs) 7

PCBs with Threads Added 8

Threads are not Processes Threads differ from traditional multitasking operating system processes in that: Processes are typically mean time, while threads exist as subsets of a process Processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources Processes have separate address spaces, whereas threads share their address space Processes interact only through system-provided inter- process communication mechanisms 9

Multithreaded Solution to clock issue runClockThread { while (1) { addOneSecond(); sleep( 1 second); } setClockThread { while (1) { setClock(h,m); } start runClockThread; start setClockThread; 10

Critical Section Issue Consider a multi-threaded program where 1 thread adds 5% interest to your checking and savings account and another thread transfers $100 from savings to checking. You start off with $100 in each account. What are the final balances when the 2 threads are done? A) $0 in savings and $210 in checking B) $5 in savings and $205 in checking C) $0 in savings and $205 in checking D) all of the above are possible 11

Consider the following Transfer ThreadInterest Thread savings = savings – 100 savings = savings * 1.05 checking = checking * 1.05 checking = checking

Semaphores In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment. Concept implemented by Dijkstra

Atomic operations on Semaphores P(s) { if (s = 1) { s = 0; } else { move process/thread onto the end of s-queue } } V(s) { if (s-queue is not empty) { move head back to ready } else { s = 1; } } 14

Solution to the missing interest problem CreateSemaphore(x) InterestThread P(x) savings = savings * 1.05 checking = checking * 1.05 V(x) TransferThread P(x) savings = savings checking = checking V(x) 15

Consider the following Transfer ThreadInterest Thread P(x) savings = savings – 100 P(x) checking = checking V(x) savings = savings * 1.05 checking = checking * 1.05 V(x) 16

When P(x) finds the resource in use 17

Example) 2 threads free for all Thread 1 { while(1) { print “thread 1”; } } Thread 2 { while(1) { print “thread 2”; } } Run thread1; Run thread2; 18

Example) 1 producer and 2 consumers a = 1; b = 0; c = 0 ProducerThread { P(a); // produce 1 V(b); P(a); // produce 2 V(c); } 19 Consumer1Thread { P(b) // consume 1 V(a) } Consumer2Thread { P(c) // consume 2 V(a) }

Condition Variables The value of a variable will determine if your thread can proceed or must wait. Ex) cashier1thread always runs and cashier2thread only runs if the condition (customerCount > 5) if true. This variable (i.e. customerCount) is referred to as a condition variable. You need obtain a mutex to access a condition variable. Based on that variables current value, your thread should continue or wait. 20

Condition Variables P(cc) if (customerCount < 5) { P(c5); } V(cc) // Handle customers P(cc) if (customerCount < 5) { V(cc); P(c5); } // Handle Customers 21 Question: You access the mutex for the condition variable, based on it’s value you determine you should wait…how do you release the mutex and make yourself wait? P(cc) if (customerCount < 5) { CP(cc,c5); } V(cc) // Handle customers