CMSC421: Principles of Operating Systems
-
Upload
hyatt-burt -
Category
Documents
-
view
31 -
download
2
description
Transcript of CMSC421: Principles of Operating Systems
![Page 1: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/1.jpg)
1
CMSC421: Principles of Operating Systems
Nilanjan Banerjee
Principles of Operating SystemsAcknowledgments: Some of the slides are adapted from Prof. Mark Corner
and Prof. Emery Berger’s OS course at Umass Amherst
Assistant Professor, University of MarylandBaltimore [email protected]
http://www.csee.umbc.edu/~nilanb/teaching/421/
![Page 2: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/2.jpg)
2
Announcements
• Readings from Silberchatz [4th and 6th chapter]
![Page 3: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/3.jpg)
3
Scheduling
One-one (linux)Many-one (Green Threads)
Many-many (Window-NT)
![Page 4: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/4.jpg)
4
How does it work in Linux
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
clone() allows a child task to share the address space of the parent task (process)
struct task_struct points to process data structures (shared or unique)
![Page 5: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/5.jpg)
5
When to use Threads and When to use processes
Processes or Threads Performance? Flexibility/Ease-of-use Robustness
Simple Scheduling OS has a list of Threads and schedules them
similar to Processes. In Linux, threads/processes treated similarly
Chooses one of the threads and loads them to be run
Runs them for a while, runs another.
![Page 6: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/6.jpg)
6
Flexibility/Ease of use?
Process are more flexible Easy to spawn processes, I can ssh into a server and
spawn a process Can communicate over sockets= distributes across
machines
Threads Communicate using memory – must be on the same
machine Requires thread-safe code
![Page 7: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/7.jpg)
7
Robustness
Process are more robust Processes are separate or sandboxed from other
processes If one process dies, no effect on other processes
Threads If one thread crashes, whole process terminates Since there is sharing, there are problems with using the
stack efficiently
![Page 8: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/8.jpg)
8
Context Switch cost
Threads – much cheaper– Stash registers, PC (“IP”), stack pointer
Processes – above plus– Process context – TLB shootdown (later in VM lecture)
Process switches more expensive, or require long quanta
![Page 9: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/9.jpg)
9
Synchronization and Concurrency
Probably the MOST important concept in OS
Lets look at a demonstration
![Page 10: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/10.jpg)
10
What really happened?
load X R1
add Y Z R1
store Y in Mem
Thread 1 executes the first two statementsand is preempted…
Thread 2 executes all the three statements
Thread 1 returns and executes the third statement
Count = count + 1
![Page 11: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/11.jpg)
11
Concurrency Terminology
Mutual exclusion (“mutex”)– prevents multiple threads from entering
Critical section– code only one thread can execute at a
time Lock
– mechanism for mutual exclusion– Lock entering critical section, accessing
shared data– Unlock when complete– Wait if locked
Invariant– Something that must be true
![Page 12: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/12.jpg)
12
Why do we need concurrency?
Synchronization serves two purposes:– Ensure safety for shared updates
• Avoid race conditions– Coordinate actions of threads
• Parallel computation• Event notification
ALL interleavings must be correct– there are lots of interleavings of events– also constrain as little as possible
![Page 13: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/13.jpg)
13
Locks/mutexes
Provide mutual exclusion to shared data– Two routines
• acquire – wait for lock, then take it• release – unlock, wake up waiters
Rules:– Acquire lock before accessing shared data– Release lock afterwards– Lock initially released
![Page 14: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/14.jpg)
14
Locks and Queueing
• Acquire:– if unlocked,
go in;otherwise wait in line
• Release:– Unlock & leave
![Page 15: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/15.jpg)
15
Synchronization and Concurrency
Demonstration on using pthread locks/mutexes
![Page 16: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/16.jpg)
16
Implementing Locks
Requires hardware support (in general) Can build on atomic operations:
– Load/Store– Disable interrupts
• Uniprocessors only– Test & Set, Compare & Swap
![Page 17: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/17.jpg)
17
Disabling Interrupts
int value; Queue q;
FREE = 0, BUSY = 1 value = 0; q = empty;
void acquire () { disable interrupts; if (value == BUSY) { add this thread to q;
enable interrupts; sleep(); } else { value = BUSY; } enable interrupts; }
void release () { disable interrupts; if (q not empty) { thread t = q.pop(); put t on ready queue; } value = FREE;
enable interrupts; }
![Page 18: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/18.jpg)
18
Lock Variants
Implementing Locks with Test and Set Blocking Locks Spin locks Hybrids
![Page 19: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/19.jpg)
19
Atomic Operation
But: locks are also variables, updated concurrently by multiple threads– Who locks the lock?
Answer: use hardware-level atomic operations– Test-and-set– Compare-and-swap
![Page 20: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/20.jpg)
20
Test and Set semantics
What’s the effect of testAndset(value) when: value = 0? (“unlocked”) value = 1? (“locked”)
int testAndset (int* v) { int old = *v; *v = 1; return old;}
![Page 21: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/21.jpg)
21
Blocking Locks
Suspend thread immediately– Lets scheduler execute another thread– Go to back of run queue or wait to be woken
Minimizes time spent waiting But: always causes context switch
void blockinglock (Lock& l) { while (testAndSet(l.v) == 1) { sched_yield(); }}
![Page 22: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/22.jpg)
22
Spin Locks
Instead of blocking, loop until released
void spinlock (Lock& l) { while (testAndSet(l.v) == 1) { ; }}
![Page 23: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/23.jpg)
23
Other variants
Spin-then-yield:– Spin for some time, then yield
• Fixed spin time• Exponential backoff
![Page 24: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/24.jpg)
24
Safety
Locks can enforce mutual exclusion,but notorious source of errors– Failure to unlock– Double locking– Deadlock (its own lecture)– Priority inversion
• not an “error” per se pthread_mutex_t l;void square (void) { pthread_mutex_lock (&l); // acquires lock // do stuff if (x == 0) { return; } else { x = x * x; } pthread_mutex_unlock (&l);}
![Page 25: CMSC421: Principles of Operating Systems](https://reader030.fdocuments.in/reader030/viewer/2022032605/56812b4c550346895d8f70ca/html5/thumbnails/25.jpg)
25
An in-class discussion