COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala

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

Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
Processes CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
CPS110: Intro to processes, threads and concurrency Author: Landon Cox.
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Concurrency: Locks and synchronization Slides by Prof. Cox.
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
CS 162 Discussion Section Week 2. Who am I? Prashanth Mohan Office Hours: 11-12pm Tu W at.
6 July 2015 Charles Reiss
29 June 2015 Charles Reiss CS162: Operating Systems and Systems Programming Lecture 5: Basic Scheduling + Synchronization.
Processes and threads.
Chapter 8: Main Memory.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
CS 6560: Operating Systems Design
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Sujata Ray Dey Maheshtala College Computer Science Department
Scheduler activations
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 3: Process Concept
Chapter 3: Processes.
Threads, Concurrency, and Parallelism
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Operating System Concepts
Applied Operating System Concepts
Chapter 3: Processes.
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Lecture 2: Processes Part 1
ICS 143 Principles of Operating Systems
Recap OS manages and arbitrates resources
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 26 Concurrency and Thread
Process & its States Lecture 5.
Operating System Concepts
Dr. Mustafa Cem Kasapbaşı
Sujata Ray Dey Maheshtala College Computer Science Department
Slides by Prof. Landon Cox and Vamsi Thummala
Concurrency, Processes and Threads
Semester Review Brian Kocoloski
Chapter 3: Processes.
Threads vs. Processes Hank Levy 1.
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
CSE 153 Design of Operating Systems Winter 2019
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala Concurrency COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala

Venues for systems research

Comments on lab submissions so far.. Read handout, ask questions Think before you start coding Write readable code man/doc are your friends Please do not post your code on the web Please do not copy any code directly from the web or other sources You can look, but write your own code When in doubt, always ask first

We hear your feedback Next lab: Multi-threaded programming in Java We provided only the interfaces You can start from the scratch Due on 26th Oct, 11:59pm You can work in groups of 2 or at most 3. Exam FAQ Check the course page

process descriptor (PCB) Processes: A Closer Look virtual address space thread stack process descriptor (PCB) user ID process ID parent PID sibling links children + + resources The address space is represented by page table, a set of translations to physical memory allocated from a kernel memory manager. The kernel must initialize the process memory with the program image to run. Each process has a thread bound to the VAS. The thread has a saved user context as well as a system context. The kernel can manipulate the contexts to start the thread running wherever it wants. Process state includes a file descriptor table, links to maintain the process tree, and a place to store the exit status.

Two threads sharing a CPU concept reality context switch

Concurrency Having multiple threads active at one time Thread is the unit of concurrency Primary topics How threads cooperate on a single task How multiple threads can share the CPUs

An example Two threads (A and B) Thread A: Thread B: A tries to increment i B tries to decrement i Thread A: i = 0; while (i < 10){ i++; } printf(“A done.”) Thread B: i = 0; while (i > -10){ i--; } printf(“B done.”)

Example continued .. Who wins? Does someone has to win? Thread A: while (i < 10){ i++; } printf(“A done.”) Thread B: i = 0; while (i > -10){ i--; } printf(“B done.”)

Debugging non-determinism Requires worst-case reasoning Eliminate all ways for program to break Debugging is hard Can’t test all possible interleavings Bugs may only happen sometimes Heisenbug Re-running program may make the bug disappear Doesn’t mean it isn’t still there!

Constraining concurrency Synchronization Controlling thread interleavings Some events are independent No shared state Relative order of these events don’t matter Other events are dependent Output of one can be input to another Their order can affect program results

Goals of synchronization All interleavings must give correct result Correct concurrent program Works no matter how fast threads run Important for your projects! 2. Constrain program as little as possible Why? Constraints slow program down Constraints create complexity

“Too much milk” rules The fridge must be stocked with milk Milk expires quickly, so never > 1 milk Landon and Melissa Can come home at any time If either sees an empty fridge, must buy milk Code (no synchronization) if (noMilk){ buy milk; }

“Too much milk” principals

Look in fridge (no milk) Arrive home, stock fridge Time 3:00 Look in fridge (no milk) 3:05 Go to grocery store 3:10 3:15 Buy milk 3:20 3:25 Arrive home, stock fridge 3:30 3:35 Too much milk!

What broke? Code worked sometimes, but not always Code contained a race condition Processor speed caused incorrect result First type of synchronization Mutual exclusion Critical sections

Synchronization concepts Mutual exclusion Ensure 1 thread doing something at a time E.g. 1 person shops at a time Code blocks are atomic w/re to each other Threads can’t run code blocks at same time

Synchronization concepts Critical section Code block must run atomically w.r.t some piece of the code If A and B are critical w/re to each other Threads mustn’t interleave code from A and B A and B mutually exclude each other Conflicting code is often same block But executed by different threads Reads/writes shared data (e.g. screen, fridge)

Back to “Too much milk” What is the critical section? Landon and Melissa’s critical sections Must be atomic w/re to each other if (noMilk){ buy milk; }

Solution 1 code Atomic operations Load: check note Store: leave note if (noMilk) { if (noNote){ leave note; buy milk; remove note; }

Does it work? 1 2 3 4 if (noMilk) { if (noNote){ leave note; buy milk; remove note; } if (noMilk) { if (noNote){ leave note; buy milk; remove note; } 1 2 3 4 Is this better than no synchronization at all? What if “if” sections are switched?

What broke? Melissa’s events can happen After Landon checks for a note Before Landon leaves a note if (noMilk) { if (noNote){ leave note; buy milk; remove note; }

Next solution Idea: Change the order of “leave note”, “check note” Requires labeled notes (else you’ll see your note)

Does it work? Nope. (Illustration of “starvation.”) leave noteLandon if (no noteMelissa){ if (noMilk){ buy milk; } remove noteLandon leave noteMelissa if (no noteLandon){ if (noMilk){ buy milk; } remove noteMelissa Nope. (Illustration of “starvation.”)

What about now? Nope. (Same starvation problem as before) while (noMilk){ leave noteLandon if(no noteMelissa){ if(noMilk){ buy milk; } remove noteLandon while (noMilk){ leave noteMelissa if(no noteLandon){ if(noMilk){ buy milk; } remove noteMelissa Nope. (Same starvation problem as before)

Next solution We’re getting closer Problem Who buys milk if both leave notes Solution Let Landon hang around to make sure job is done

Does it work? Yes! It does work! Can you show it? leave noteLandon while (noteMelissa){ do nothing } if (noMilk){ buy milk; remove noteLandon leave noteMelissa if (no noteLandon){ if (noMilk){ buy milk; } remove noteMelissa Yes! It does work! Can you show it?