Midterm Review
OS Structure • User mode/ kernel mode – Memory protection, privileged instructions • System call – Definition, examples, how it works? • Other concepts to know – Monolithic kernel vs. Micro kernel 2
API - System Call - OS 3
UNIX: Monolithic Kernel • Implements CPU scheduling, memory management, filesystems, and other OS modules all in a single big chunk User Application User Application User Application System call Protection boundary Kernel Process Management Accounting Memory Management Filesystem Disk I/O TCP/IP Device Drivers • Pros and Cons + Overhead is low + Data sharing among the modules is easy – Too big. (device drivers!!!) – A bug in one part of the kernel can crash the entire system 4
Process • Address space layout • Code, data, heap, stack • Process states – new, ready, running, waiting, terminated • Other concepts to know • Process Control Block • Context switch • Zombie, Orphan • Communication overheads of processes vs. threads 5
Process Address Space • Text – Program code • Data – Global variables • Heap – Dynamically allocated memory • i.e., Malloc() • Stack – Temporary data – Grow at each function call 6
Process State – running : Instructions are being executed – waiting : The process is waiting for some event to occur – ready : The process is waiting to be assigned to a processor 7
Quiz • Hints int count = 0; int main() – Each process has its own { private address space int pid = fork(); if (pid == 0){ – Wait() blocks until the count++; child finish printf("Child: %d\n", count); } else{ wait(NULL); • Describe the output. count++; printf("Parent: %d\n", count); Child: 1 } count++; Main: 2 printf("Main: %d\n", count); return 0; Parent: 1 } Main: 2 8
Inter-Process Communication • Shared memory • Message passing
Models of IPC message passing shared memory 10
Quiz • A process produces 100MB data in memory. You want to share the data with two other processes so that each of which can access half the data (50MB each). What IPC mechanism will you use and why? • IPC mechanism: POSIX Shared memory • Reasons: (1) large data need high performance, (2) no need for synchronization,
Threads • Definition • Key differences compared to process • Communication between the threads • User threads vs. Kernel threads • Key benefits over processes?
Single and Multithreaded Process source: https://computing.llnl.gov/tutorials/pthreads/ 13
Recap: Multi-threads vs. Multi- processes • Multi-processes – (+) protection – (-) performance (?) Process-per-tab • Multi-threads – (+) performance Single-process multi-threads – (-) protection 14
Synchronization • Race condition • Synchronization instructions – test&set, compare&swap • Spinlock – Spin on wait – Good for short critical section but can be wasteful • Mutex – Block (sleep) on wait – Good for long critical section but bad for short one 15
Race Condition Initial condition: counter = 5 Thread 1 Thread 2 R2 = load (counter); R1 = load (counter); R2 = R2 – 1; R1 = R1 + 1; counter = store (R2); counter = store (R1); • What are the possible outcome? 16
Race Condition Thread 1 Thread 2 counter++; counter--; • Initially counter = 5 • Possible counter values?
Quiz • • Pseudo code of test&set Spinlock implementation using TestAndSet instruction int TestAndSet(int *lock) { void init_lock(int *lock) { int ret = *lock; *lock = 0; *lock = 1; } return ret; } void lock(int *lock) { while (TestAndSet(lock)); } void unlock(int *lock) { *lock = 0; }
void mutex_init (mutex_t *lock) { lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); } void mutex_lock (mutex_t *lock) { … while(TestAndSet(&lock->value)) { current->state = WAITING; list_add(&lock->wait_list, current); … schedule(); … } … } void mutex_unlock (mutex_t *lock) { … lock->value = 0; if (!list_empty(&lock->wait_list)) wake_up_process(&lock->wait_list) … }
void mutex_init (mutex_t *lock) More reading: mutex.c in Linux { lock->value = 0; list_init(&lock->wait_list); Thread waiting list spin_lock_init(&lock->wait_lock); To protect waiting list } void mutex_lock (mutex_t *lock) { spin_lock(&lock->wait_lock); while(TestAndSet(&lock->value)) { Thread state change current->state = WAITING; list_add(&lock->wait_list, current); Add the current thread to the spin_unlock(&lock->wait_lock); waiting list schedule(); Sleep or schedule another thread spin_lock(&lock->wait_lock); } spin_unlock(&lock->wait_lock); } void mutex_unlock (mutex_t *lock) { spin_lock(&lock->wait_lock); lock->value = 0; if (!list_empty(&lock->wait_list)) Someone is waiting for the lock wake_up_process(&lock->wait_list) Wake-up a waiting thread spin_unlock(&lock->wait_lock); 20 }
Recap: Bounded Buffer Problem Revisit Monitor version Semaphore version Mutex lock; Semaphore mutex = 1, full = 0, Condition full, empty; empty = N; produce (item) produce (item) { { lock.acquire(); empty.P(); while (queue.isFull()) mutex.P(); empty.wait(&lock); queue.enqueue(item); queue.enqueue(item); mutex.V(); full.signal(); full.V(); lock.release(); } } consume() consume() { { full.P(); lock.acquire(); mutex.P(); while (queue.isEmpty()) item = queue.dequeue(); full.wait(&lock); mutex.V(); item = queue.dequeue(item); empty.V(); empty.signal(); return item; lock.release(); } return item; } 21
Deadlock • Deadlock conditions • Resource allocation graph • Banker’s algorithm • Dining philosopher example • Other concepts to know • Starvation vs. deadlock 22
Conditions for Deadlocks • Mutual exclusion – only one process at a time can use a resource • No preemption – resources cannot be preempted, release must be voluntary • Hold and wait – a process must be holding at least one resource, and waiting to acquire additional resources held by other processes • Circular wait – There must be a circular dependency. For example, A waits B, B waits C, and C waits A. • All four conditions must simultaneously hold 23
Resource-Allocation Graph Process P 1 Resource Type with 4 instances P i requests instance of R j P i R j P i is holding an instance of R j P i R j 24
Quiz Draw a resource allocation graph for the following. P2 P3 R2 R1 R3 P1 P4 R4 R5 P5 25
Quiz • Using Banker’s algorithm, determine whether this state is safe or unsafe. Total resources: 10 Avail resources: 1 Process Max Alloc P 0 10 4 P 1 3 1 P 2 6 4 26
Scheduling • Three main schedulers • FCFS, SJF/SRTF, RR • Gant chart examples • Other concepts to know • Fair scheduling (CFS) • Fixed priority scheduling • Multi-level queue scheduling • Load balancing and multicore scheduling 27
Round-Robin (RR) Process Burst Times • Example P1 24 – Quantum size = 2 P2 3 P3 3 – Gantt chart P 1 P 1 P 2 P 3 P 1 P 2 P 3 P 1 P 1 30 0 6 8 9 12 14 2 4 10 – Response time (between ready to first schedule) • P1: 0, P2: 2, P3: 4. average response time = (0+2+4)/3 = 2 – Waiting time • P1: 6, P2: 6, P3: 7. average waiting time = (6+6+7)/3 = 6.33 28
Recommend
More recommend