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

  • View
    213

  • Download
    0

Embed Size (px)

Text of CS 149: Operating Systems March 12 Class Meeting Department of Computer Science San Jose State...

CS 146: Data Structures and Algorithms

CS 149: Operating SystemsMarch 12 Class MeetingDepartment of Computer ScienceSan Jose State University

Spring 2015Instructor: Ron Makwww.cs.sjsu.edu/~mak

2Linux Memory ManagementTwo main components

Physical memoryAllocating and freeing pages, groups of pages, small blocks of RAM

Virtual memoryMapping memory into the address space of running processes.Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak3Linux Physical Memory (Intel x86-32)Physical memory is divided into three zones.ZONE_DMAPages that used by legacy devices.ZONE_NORMALNormal, regularly mapped pages.ZONE_HIGHMEMPages 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-0Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak4Linux Kernel Memory AllocationThe 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 reservedComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak5

Linux Kernel Memory AllocationModern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reservedAllocations are contiguous.Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak6Linux Kernel Memory Allocation, contdA 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 reservedComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak7Linux Kernel Memory Allocation, contdThe 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-0Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak8Linux Kernel Memory Allocation, contd

Operating Systems Concepts, 9th editionSilberschatz, Galvin, and Gagne (c) 2013 John Wiley & Sons. All rights reserved. 978-1-118-06333-0Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak9Review for the MidtermA 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._Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak10Review for the Midterm, contdWhat is an operating system.Why study them?

History of operating systems.The good old days before your computer had an OS.Data processingComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak11Review for the Midterm, contdOS conceptsConcurrencyProcessesFile systemsI/O redirectionPipesShellAPIfork() and join()Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak12Review for the Midterm, contdOS structureKernel mode vs. user modeMonolithic vs. layeredVirtual machinesClient-serverDistributed_Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak13Review for the Midterm, contdProcessesContext switchingCreation and terminationStates

Process schedulingGoalsProcess behaviorcompute-bound vs. I/O bound_Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak14Review for the Midterm, contdScheduling algorithmsPreemptive vs. non-preemptiveScheduling criteriaAlgorithms: FCFS, SJF, SRT, RR, HPF, etc.Priority queuesTime quanta (slices)TimelinesInterrupt routines_Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak15Review for the Midterm, contdThreadsLightweight processesMultithreaded systemsUser-level vs. kernel-levelThread scheduling

UNIX Pthread librarycreate threadsstart threadswait for threads_Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak16Review for the Midterm, contdInterprocess communication

Shared memoryUNIX system calls for shared memory

Message passingblocking vs. nonblockingsocketsremote procedure call (RPC)pipeComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak17Review for the Midterm, contdProcess synchronization

race conditioncritical region

Mutual exclusionlock variablesbusy waitingPetersons solutiontest and set lock instructionsleep and wakeup

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak18Review for the Midterm, contdProducer-consumer problemReaders-writers problemDining philosophers problem

UNIX Pthreads librarysemaphoreswait and post (signal)mutexeslock and unlock

MonitorsDining philosophers problemJava implementationComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak19Review for the Midterm, contdDeadlocks

Requesting a resourcepreemptable vs non-preemptable

Four deadlock conditionsmutual exclusionhold and waitno preemptioncircular wait

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak20Review for the Midterm, contdDeadlock modeling

Strategies

Detection and recovery

Prevention

Bankers algorithms

Resource trajectoriesComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak21Review for the Midterm, contdMemory management

System memory designMemory manager

Memory hierarchyCacheMainDisk storage

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak22Review for the Midterm, contdMonoprogramming systemsMultiprogramming with fixed partitionsMultiprogramming with variable-sized partitions

Process relocationProcess protection_Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak23Review for the Midterm, contdAddress bindingSymbolic addresses to relocatable addressCompile-time, load-time, execution-time

Logical vs. physical address space

Dynamic loadingDynamic linking

SwappingKeep track of memory allocationsBacking store

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak24Review for the Midterm, contdMemory allocation algorithms

First fitNext fitBest fitWorst fit

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak25Review for the Midterm, contdPagingPagesPage frames

Page tableMultilevelInverted

Logical address formatShared pages

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak26Review for the Midterm, contdPage replacement algorithms

FIFO: First-in first-outSecond chance

LRU: Least recently usedLFU: Least frequently usedMFU: Most frequently usedRandom pick

Local vs. globalComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak27Review for the Midterm, contdFragmentationInternal and external

Locality of referenceTranslation lookaside buffer (TLB)Hit ratioComputer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak28Review for the Midterm, contdSegmentation

Virtual memoryDemand pagingPage faultCopy-on-write

Kernel memoryBuddy system_

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak29Review for the Midterm, contdWorking set modelThrashingPage fault frequency (PFF)

Locality modelPrepagingPage sizeTLB reach

Program structure

Memory-mapped files

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak30Sample Midterm QuestionsWhat is the output from Line A and Line B in the following code?

Answer:Line A: value = 15Line B: value = 5 #include #include

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; }}Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak31Sample Midterm Questions, contdWhat is the output from Line A and Line B in the following code?

Answer:Line A: value = 10Line B: value = 5 #include #include #include

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);}Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak32Sample Midterm Questions, contdWe 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.

Computer Science Dept.Spring 2015: March 10CS 149: Operating Systems R. Mak33Sample Midterm Questions, contdContinued

We indicate that a pr

Recommended

View more >