CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State...
-
Upload
clifford-reeves -
Category
Documents
-
view
215 -
download
0
Transcript of CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State...
CS 149: Operating SystemsMarch 12 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2015Instructor: Ron Mak
www.cs.sjsu.edu/~mak
2Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Memory Management
Two main components
Physical memory Allocating and freeing pages, groups of pages,
small blocks of RAM
Virtual memory Mapping memory into the address space of running
processes.
3Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Physical Memory (Intel x86-32)
Physical memory is divided into three zones.1. ZONE_DMA
Pages that used by legacy devices.
2. ZONE_NORMAL Normal, regularly mapped pages.
3. ZONE_HIGHMEM Pages with high memory addresses that are not mapped
into the kernel address space.
Operating Systems Concepts, 9th editionSilberschatz, Galvin, and Gagne (c) 2013 John Wiley & Sons. All rights reserved. 978-1-118-06333-0
4Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Kernel Memory Allocation
The kernel uses the buddy system for its memory.
Suppose initially, a region of 64 pages is available.
A request is rounded up to a power of 2, say 8 pages.
Keep dividing in half until the allocation can be made.
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
5
Linux Kernel Memory Allocation
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
Allocations are contiguous.
6Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Kernel Memory Allocation, cont’d
A second request for 8 pages. A request for 4 pages. The second 8-page allocation is released. The first 8-page allocation is released.
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
7Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Kernel Memory Allocation, cont’d
The buddy algorithm causes internal fragmentation.
Linux uses a slab allocator to carve smaller allocations from what is allocated by the buddy algorithm.
Since the kernel frequently allocates certain types of objects, slabs are cached by object size.
Operating Systems Concepts, 9th editionSilberschatz, Galvin, and Gagne (c) 2013 John Wiley & Sons. All rights reserved. 978-1-118-06333-0
8Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Linux Kernel Memory Allocation, cont’d
Operating Systems Concepts, 9th editionSilberschatz, Galvin, and Gagne (c) 2013 John Wiley & Sons. All rights reserved. 978-1-118-06333-0
9Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm
A test of understanding, not memorization.
Open book, open notes, open laptop, open Internet.
But not open neighbor! Forbidden to communicate with anyone else
during the exam._
10Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
What is an operating system. Why study them?
History of operating systems. The good old days before your computer had an OS. Data processing
11Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
OS concepts Concurrency Processes File systems I/O redirection Pipes Shell API
fork() and join()
12Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
OS structure Kernel mode vs. user mode Monolithic vs. layered Virtual machines Client-server Distributed
_
13Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Processes Context switching Creation and termination States
Process scheduling Goals Process behavior
compute-bound vs. I/O bound_
14Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Scheduling algorithms Preemptive vs. non-preemptive Scheduling criteria Algorithms: FCFS, SJF, SRT, RR, HPF, etc. Priority queues Time quanta (slices) Timelines Interrupt routines
_
15Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Threads Lightweight processes Multithreaded systems User-level vs. kernel-level Thread scheduling
UNIX Pthread library create threads start threads wait for threads
_
16Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Interprocess communication
Shared memory UNIX system calls for shared memory
Message passing blocking vs. nonblocking sockets remote procedure call (RPC) pipe
17Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Process synchronization
race condition critical region
Mutual exclusion lock variables busy waiting Peterson’s solution test and set lock instruction sleep and wakeup
18Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Producer-consumer problem Readers-writers problem Dining philosophers problem
UNIX Pthreads library semaphores
wait and post (signal) mutexes
lock and unlock
Monitors Dining philosophers problem
Java implementation
19Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Deadlocks
Requesting a resource preemptable vs non-preemptable
Four deadlock conditions1. mutual exclusion2. hold and wait3. no preemption4. circular wait
20Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Deadlock modeling
Strategies
Detection and recovery
Prevention
Banker’s algorithms
Resource trajectories
21Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Memory management
System memory design Memory manager
Memory hierarchy Cache Main Disk storage
22Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Monoprogramming systems Multiprogramming with fixed partitions Multiprogramming with variable-sized partitions
Process relocation Process protection
_
23Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Address binding Symbolic addresses to relocatable address Compile-time, load-time, execution-time
Logical vs. physical address space
Dynamic loading Dynamic linking
Swapping Keep track of memory allocations Backing store
24Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Memory allocation algorithms
First fit Next fit Best fit Worst fit
25Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Paging Pages Page frames
Page table Multilevel Inverted
Logical address format Shared pages
26Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Page replacement algorithms
FIFO: First-in first-out Second chance
LRU: Least recently used LFU: Least frequently used MFU: Most frequently used Random pick
Local vs. global
27Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Fragmentation Internal and external
Locality of reference Translation lookaside buffer (TLB) Hit ratio
28Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Segmentation
Virtual memory Demand paging Page fault Copy-on-write
Kernel memory Buddy system
_
29Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Review for the Midterm, cont’d
Working set model Thrashing Page fault frequency (PFF)
Locality model Prepaging Page size TLB reach
Program structure
Memory-mapped files
30Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions
What is the output from Line A and Line B in the following code?
Answer:Line A: value = 15Line B: value = 5
#include <stdio.h>#include <sys/types.h>
int value = 5;
int main(){ pid_t pid = fork(); if (pid == 0) { value += 10; printf("Line A: value = %d\n", value); // Line A return 0; } else { int status; waitpid(-1, &status, 0); printf("Line B: value = %d\n", value); // Line B return 0; }}
31Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
What is the output from Line A and Line B in the following code?
Answer:Line A: value = 10Line B: value = 5
#include <stdio.h>#include <pthread.h>#include <sys/types.h>
int value = 5;void *my_thread(void *parm);
int main(){ pthread_t tid; pthread_attr_t attr; pid_t pid = fork(); if (pid == 0) { pthread_attr_init(&attr); pthread_create(&tid, &attr, my_thread, NULL); pthread_join(tid, NULL); printf("Line A: value = %d\n", value); // Line A } else { int status; waitpid(-1, &status, 0); printf("Line B: value = %d\n", value); // Line B }} void *my_thread(void *parm){ value = 10; pthread_exit(0);}
32Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
We can extend our resource allocation graphs as follows:
If there are multiple instances of a resource, we show each instance
with a dot inside the resource rectangle.
a.
b.
33Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
Continued …
We indicate that a process holds a resource instance by an arrow from a dot to the process circle.
We indicate that a process has requested (is waiting for) any instance of a resource by an arrow from the process circle to the resource rectangle.
a.
b.
34Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
For each of these resource allocation graphs, explain why or why not it represents a deadlock.
a) Answer:
Has two cycles:P2R3P3R2P2 and P1R1P2R3P3R2P1 P2 is waiting for R3, which is held by P3. P3 is waiting for either P1 or P2 to release an instance of R2. P1 is waiting for P2 to release R1. The processes are deadlocked.
a.
b.
35Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
For each of these resource allocation graphs, explain why or why not it represents a deadlock.
b) Answer:
Also has a cycle:P1R1P3R2P1P2 can release an instance of R1, which can be taken by P1.P4 can release an instance of R2, which can then be taken by P3. Thus, the cycle is broken. There is no deadlock
a.
b.
36Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
Consider this traffic deadlock (AKA traffic gridlock):
a. Briefly explain how the four deadlock conditions are satisfied in this example.
1. mutual exclusion2. hold and wait3. no preemption4. circular wait
37Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
a. Answer:
1. Mutual exclusion. Only one car can occupy a particular spot on the road at any instant.
2. Hold and wait: A car holds its spot and waits to move forward.
3. No preemption: A car cannot be removed (preempted) from its spot on the road.
4. Circular wait: Each side of the block contains cars whose movements are blocked by cars waiting at the next intersection
38Computer Science Dept.Spring 2015: March 10
CS 149: Operating Systems© R. Mak
Sample Midterm Questions, cont’d
b. What’s a simple rule to prevent this traffic gridlock?
Answer:
Do not allow cars to moveinto an intersection unless it can clear the intersection. (“Don’t block the box.”)