38
CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

Embed Size (px)

Citation preview

Page 1: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

CS 149: Operating SystemsMarch 12 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2015Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak 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.

Page 3: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 4: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 5: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.

Page 6: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 7: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 8: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 9: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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._

Page 10: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 11: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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()

Page 12: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

_

Page 13: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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_

Page 14: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

_

Page 15: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

_

Page 16: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 17: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 18: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 19: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 20: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 21: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 22: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

_

Page 23: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 24: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 25: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 26: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 27: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 28: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

_

Page 29: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 30: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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; }}

Page 31: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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);}

Page 32: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.

Page 33: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.

Page 34: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.

Page 35: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.

Page 36: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 37: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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

Page 38: CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak

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.”)