CSE 513 Introduction to Operating Systems Class 3 - Interprocesses ...
Recommend Documents
CSE 2431 (Approved): Systems II: Introduction to Operating Systems. Course
Description. Introduction to operating system concepts: process, CPU scheduling,
...
2 Oct 2013 ... Makefile and Modular Programming in C. TA: Kyungho Jeon (kyunghoj@buffalo.
edu). CSE 421: Introduction to Operating Systems ...
The exercises in this book assume a basic knowledge of both of ... The Windows
XP operating system is a dynamic and continually changing operating ..... Issuing
ls after the mkdir shows the newly created download directory, as shown in blue
.... p
you do not connect it to a production network. Several ... commands and actions
you need to know for the Security Essentials Boot Camp. This document ...
2Von Neumann was one of the early pioneers of computing systems. He also did
... problem. The details within the chapter, of course, present the solution,.
History lesson in change. – OS reacts to ... Today: OS and Computer Architecture
... OS as history teacher: learning from past to predict the future,. i.e., OS design ...
Today: OS and Computer Architecture. • Basic OS Functionality. • Basic
Architecture reminder. • What the OS can do is dictated in part by the architecture.
Introduction and History of Operating Systems. ◦ What is an Operating System ...
Enrollment policy: priority to graduating computer science seniors. CMPSCI 377:
...
BaseTech / Survey of Operating Systems / Holcombe, Holcome, and Smith /
222511-4 / Blind Folio 1. Understanding microcomputer operating systems (OSs)
is ...
Fundamental principles of operating systems: process synchronization,
deadlocks, ... Abraham Silberschatz, Peter B. Galvin and Greg Gagne, Operating
System ...
Main textbook, available at the bookstore: • Database Systems: The Complete
Book,. Hector Garcia-Molina,. Jeffrey Ullman,. Jennifer Widom. Second edition.
Database Systems: The Complete Book,. Hector Garcia-Molina,. Jeffrey Ullman,.
Jennifer Widom. Most important: COME TO CLASS ! ASK QUESTIONS !
Operating systems : internals and design principles / William Stallings. Operating
systems ... Operating systems : a modern perspective / Gary J. Nutt. Operating ...
CSE 6431 (Approved): Advanced Operating Systems. Course Description.
Advanced topics in operating systems and concurrency; introduction to
distributed ...
ARIZONA STATE UNIVERSITY. CSE 430 — Operating Systems — Spring 2012.
Schedule Line Number: 19464. Instructor: Dr. Violet R. Syrotiuk ...
Operating Systems. CSE 204. Dr. Nasim MAHMUD. Spring 2013 University of
Liberal Arts Bangladesh. Page 2. Meaning of knowledge. • Knowledge has its ...
Email Address: [email protected], [email protected] ...
algorithms, and major structures in computer operating systems. Students will be
able ...
UNIT-1. Introduction to Distributed Systems: Why do we develop distributed
systems? • availability of .... 3) some low-level process management and
scheduling.
Modern Operating Systems (3rd edition), Tanenbaum ... this, we will discuss the
various important aspects of operating systems in general, examine specific ...
This module covers the core concepts of modern operating systems, and
provides contextual ... Silberschatz, Abraham, Galvin, Peter and Gagne, Greg,. (
2012).
2004 Deitel & Associates, Inc. All rights reserved. Chapter 1 – Introduction to
Operating Systems. Outline. 1.1. Introduction. 1.2. What Is an Operating System?
Faculty of Computer Science / Institute of Systems Architecture / Operating
Systems. SYSTEMS PROGRAMMING. C++ INTRODUCTION. Alexander Warg ...
solution is algebraic, i.e. if (x1,...,xn) is a solution, then xi ∈ Q for all i. 1. Where
we ... Explicitly, if X and Y are two algebraic sets corresponding to ideals IX and.
CSE 513 Introduction to Operating Systems Class 3 - Interprocesses ...
1. CSE 513. Introduction to Operating Systems. Class 3 - Interprocesses
Communication &. Synchronization. Jonathan Walpole. Dept. of Comp. Sci. and
Eng.
CSE 513 Introduction to Operating Systems Class 3 - Interprocesses Communication & Synchronization Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and Science University
1
Race conditions q
q
q
What is a race condition? v two or more processes have an inconsistent view of a shared memory region (I.e., a variable) Why do race conditions occur? v values of memory locations replicated in registers during execution v context switches at arbitrary times during execution v processes can see “stale” memory values in registers What solutions can we apply? v prevent context switches by preventing interrupts? v make processes coordinate with each other to ensure mutual exclusion in accessing “critical sections” of code 2
Counter increment race condition
Incrementing a counter (load, increment, store) Context switch can occur after load and before increment! 3
Mutual exclusion conditions q
No two processes simultaneously in critical region
q
No assumptions made about speeds or numbers of CPUs
q
q
No process running outside its critical region may block another process No process must wait forever to enter its critical region
4
Critical regions with mutual exclusion
Mutual exclusion using critical regions
5
How can we implement mutual exclusion? q
q
What about using a binary “lock” variable in memory and having processes check it and set it before entry to critical regions? Many computers have some limited hardware support for setting locks v v
q
“Atomic” Test and Set Lock instruction “Atomic” compare and swap operation
Solves the problem of v v
Expressing intention to enter C.S. Actually setting a lock to prevent concurrent access
6
Test and Set Lock q
Test-and-set does two things atomically: v v
Test a lock (whose value is returned) Set the lock 1. extract value
Test_var q q
lock = {TRUE , FALSE}
2. Set TRUE
Lock obtained when the return value is FALSE If TRUE, someone already had the lock (and still has it) 7
Guaranteed that only one process returns with FALSE when a lock is returned to the system and others are waiting to act on the lock
20
Generalized primitives for critical sections q
q
Thus far, the solutions have used busy waiting v a process consumes CPU resources to evaluate when a lock becomes free v on a single CPU system busy waiting prevents the lock holder from running, completing the critical section and releasing the lock! v it would be better to block instead of busy wait (on a single CPU system) Blocking synchronization primitives v sleep – allows a process to sleep on a condition v wakeup – allows a process to signal another process that a condition it was waiting on is true v but how can these be implemented? 21
Blocking synchronization primitives q
q
Sleep and wakeup are system calls v OS can implement them by managing a data structure that records who is blocked and on what condition v but how can the OS access these data structures atomically? Concurrency in the OS: context switches and interrupts v the OS can arrange not to perform a context switch while manipulating its data structures for sleep and wakeup v but what about interrupts? v what if interrupt handlers manipulate the sleep and wakeup data structures? What if they need synchronization? v how can the OS synchronize access to its own critical sections? 22
Disabling interrupts q
Disabling interrupts in the OS vs disabling interrupts in user processes v why not allow user processes to disable interrupts? v is it ok to disable interrupts in the OS? v what precautions should you take?
23
Generic synchronization problems
24
Producer/Consumer with busy waiting process producer{ while(1){ //produce char c while (count==n) no_op; buf[InP] = c; InP = InP + 1 mod n count++; } } n-1
0 2
…
process consumer{ while(1){ while (count==0) no_op; c = buf[OutP]; OutP = OutP + 1 mod n count--; //consume char } }
3
Global variables: char buf[n] int InP, OutP; // [0-n-1] int count
25
Problems with busy waiting solution q q
q
Producer and consumer can’t run at the same time Count variable can be corrupted if context switch occurs at the wrong time Bugs difficult to track down
26
Producer/Consumer with blocking process producer{ • while(1){ • //produce char c • if (count==n) • sleep(full); • buf[InP] = c; • InP = InP + 1 mod n • count++; • if (count == 1) • wakeup(empty); • } } n-1
1 2
…
3
process consumer{ • while(1){ • while (count==0) • sleep(empty); • c = buf[OutP]; • OutP = OutP + 1 mod n • count--; • if (count == n-1) • wakeup(full); • //consume char • } } Global variables: char buf[n] int InP, OutP; // [0-n-1] int count 27
Problems with the blocking solution q q q q
q
q q
Count variable can be corrupted Increments or decrements may be lost Both processes may sleep forever Buffer contents may be over-written Code that manipulates count must be made a critical section and protected using mutual exclusion Sleep and wakeup must be implemented as system calls OS must use synchronization mechanisms (TSL or interrupt disabling) in its implementation of sleep and wake-up … I.e., the critical sections of OS code that manipulate sleep/wakeup data structures must be protected using mutual exclusion 28
Semaphores q
q
An abstract data type that can be used for condition synchronization and mutual exclusion Integer variable with two operations: v
v
q
down (sema_var) decrement sema_var by 1, if possible if not possible, “wait” until possible up(sema_var) increment sema_var by 1
Both up() and down() are assumed to be atomic v
made to be atomic by OS implementation
29
Semaphores q
There are multiple names for semaphores v v
q
Down(S), wait(S), P(S) Up(S), signal(S), V(S)
Semaphore implementations v
Binary semaphores (mutex) • support mutual exclusion (lock either set or free)
v
Counting semaphores • support multiple values for more sophisticated coordination and controlled concurrent access among processes 30
Using Semaphores for Mutex
semaphore mutex = 1
1 repeat
1 repeat
2
down(mutex);
2
down(mutex);
3
critical section
3
critical section
4
up(mutex);
4
up(mutex);
5
remainder section
5
remainder section
6 until FALSE
6 until FALSE
31
Using Semaphores for Mutex
semaphore mutex = 0
1 repeat
1 repeat
2
down(mutex);
2
down(mutex);
3
critical section
3
critical section
4
up(mutex);
4
up(mutex);
5
remainder section
5
remainder section
6 until FALSE
6 until FALSE
32
Using Semaphores for Mutex
semaphore mutex = 0
1 repeat
1 repeat
2
down(mutex);
2
down(mutex);
3
critical section
3
critical section
4
up(mutex);
4
up(mutex);
5
remainder section
5
remainder section
6 until FALSE
6 until FALSE
33
Using Semaphores for Mutex
semaphore mutex = 1
1 repeat
1 repeat
2
down(mutex);
2
down(mutex);
3
critical section
3
critical section
4
up(mutex);
4
up(mutex);
5
remainder section
5
remainder section
6 until FALSE
6 until FALSE
34
Using Semaphores for Mutex
Check again to see if it can be decremented
semaphore mutex = 1
1 repeat
1 repeat
2
down(mutex);
2
down(mutex);
3
critical section
3
critical section
4
up(mutex);
4
up(mutex);
5
remainder section
5
remainder section
6 until FALSE
6 until FALSE
35
In class exercise… q
Implement producer consumer solution:
36
Counting semaphores in producer/consumer Global variables semaphore full_buffs = 0; semaphore empty_buffs = n; char buff[n]; int InP, OutP; process producer{ • while(1){ • //produce char c • • • • • }
down(empty_buffs); buf[InP] = c; InP = InP + 1 mod n up(full_buffs); }
process consumer{ • while(1){ • • • •
down(full_buffs); c = buf[OutP]; OutP = OutP + 1 mod n up(empty_buffs);
• • }
//consume char }
37
Implementing semaphores q
Generally, the hardware has some simple mechanism to support semaphores v v
Control over interrupts (almost all) Special atomic instructions in ISA • test and set lock • compare and swap
q
Spin-Locks vs. Blocking v
Spin-locks (busy waiting) • may waste a lot of cycles on uni-processors
v
Blocking • may waste a lot of cycles on multi-processors
38
Implementing semphores q
Blocking struct semaphore{ int val; list L; }
Down(semaphore sem) DISABLE_INTS sem.val--; if (sem.val < 0){ add proc to sem.L block(proc); } ENABLE_INTS
Up(semaphore sem) DISABLE_INTS sem.val++; if (sem.val