the operating system machine levelmachine...

96
The Operating System Machine Level Machine Level Chapter 6 1

Upload: phungdien

Post on 08-May-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

The Operating SystemMachine LevelMachine Level

Chapter 6

1

Contemporary Multilevel Machines

A i l l tA six-level computer. The support method for each level is indicated below it .2

Operating System Machine

a) Operating System is a program that, from the programmer’s point of view adds a variety of new instructions and featurespoint of view, adds a variety of new instructions and features .

b) Normally, operating system is implemented in software.) y, p g y p

c) Both the OSM and the ISA levels are abstract (software).

3

OSM and ISA

a) The OSM level instruction set is the complete set of instructions available to application programmersinstructions available to application programmers.

b) It contains all ISA level instructions plus the set of newb) It contains all ISA level instructions plus the set of new instructions.

c) The new instructions are called system calls. A system call invokes a predefined operating system service.

d) The OSM level is always interpreted.

4

Operating System Machine

Positioning of the operating system machine level.5

Three Topics of OSM

a) Virtual Memory – a technique to make the machine appear to h th it i lit hhave more memory than it in reality has.

b) File I/O – a higher level concept of I/O instructionsb) File I/O – a higher level concept of I/O instructions.

c) Parallel Processing – how multiple processes can execute, ) g p p ,communicate, and synchronize.

6

Virtual Memory

a) IBM 650 has only 2000 words of memory, PDP-1 only 4096 18-bit words.18 bit words.

b) The traditional solution was the use of secondary memory, such as disk. Overlay management

) 1961 M h t E l d th d d t fc) 1961, Manchester, England, a method proposed to perform the overlay process automatically – Virtual Memory!

d) By the early 1970s, virtual memory had become available on most computers.

7

Paging

a) The idea of separating the concepts of address space and memory locationsmemory locations

b) The technique for automatic overlaying is called paging and ) q y g p g gthe chucks of program read in from disk are called pages.

c) Because the programmer can program as though paging did not exist, the paging mechanism is said to be transparent.

d) The virtual machine provided by the operating system can provide the illusion that all the virtual addresses are backed up by real memory. Only operating system writers have to know how the illusion is supported. 8

Paging

A mapping in which virtual addresses 4096 to 8191 are mappedA mapping in which virtual addresses 4096 to 8191 are mappedonto main memory addresses 0 to 4095. 9

Paging - Example

a) The contents of main memory would be saved on diska) The contents of main memory would be saved on diskb) Words 8192 to 12287 would be located on diskc) Words 8192 to 12287 would be loaded into main memory) yd) The address map would be changed to map addresses 8192

to 12287 onto memory locations 0 to 4096) E i ld i h h hi l h de) Execution would continue as through nothing unusual had

happened.

10

Implementation of Paging

– The virtual address space is broken up into a number of equal-sized pages.pages.

• Page sizes ranging from 512 to 64K bytes are common.• Sizes as large as 4 MB are used occasionally.• The page size is always a power of 2.• The physical address space is broken up into pieces in

a similar way each piece being the size of a pagea similar way, each piece being the size of a page.• These pieces of main memory into which the pages go

are called page frames.p g

11

Implementation of Paging

– Every computer with virtual memory has a device for doing the virtual-to-physical mapping.

– This device is called the MMU (Memory Management Unit).• It may be on the CPU chip, or it may be on a separate

chip that works closely with the CPU chipchip that works closely with the CPU chip.• Since our sample MMU maps from a 32-bit virtual

address to a 15-bit physical address, it needs a 32-bit input register and a 15-bit output register.

12

Implementation of Paging

– On the following slide, the MMU is presented with a 32-bit virtual ddaddress.

– It separates the address into a 20-bit virtual page number and a 12-bit offset within the page (because the pages are 4K).

Th i t l b i d i d i t th• The virtual page number is used as an index into the page table to find the entry for the page referenced.

• In the example, the virtual page number is 3, so entry 3 of the page table is selected, as shown.

– The first thing the MMU does is check to see if the page referenced is in main memory.

13

Implementation of Paging

• Not all virtual pages can be in memory at once.• The MMU makes this check by examining the y g

present/absent bit in the page table entry.• In the example, the bit is 1, meaning the page is

currently in memorycurrently in memory.• Now, the page frame value from the selected entry (6 in

this case) is copied into the upper 3 bits of the 15-bit output registeroutput register.

• In parallel with this operation, the low-order 12-bits of the virtual address (the page offset field) are copied into the low-order 12 bits of the output register.

14

Implementation f P i (1)of Paging (1)

The first 64 KB of virtual address space divided into 16 pages, with each page being 4K.

a) Indexing, indirect addressing may be used to generate this address.

15

Implementation f P i (2)of Paging (2)

A 32 KB main memoryA 32 KB main memory divided up into eight page frames of 4 KB each.

16

Implementation of Paging (3)

Formation of a main memory address from a virtual

address.

17

Demand Paging and th W ki S t M d lthe Working Set Model

A possible mapping of the first 16 virtual pages

onto a main memory with eight page frames.

18

Paging

a) The alternative approach is based on the observation that t d t f th i ddmost programs do not reference their address space

uniformly but that the references tend to cluster on a small number of pages. Locality Principlep g y p

b) At any instant time, there exists a set consisting of all the d b th K t t fpages used by the K most recent memory references.

Working Set.

19

Page Replacement Policy

a) When a program references a page that is not in main memory, the needed page must be fetched from the disk. Some other page will generally have to be sent back to the disk.

b) Choosing a page to remove at random is not a good idea.c) One way is to make a prediction when the next reference to

each page will occur and remove the page whose predicted next reference lies furthest in the future.

d) Least Recently Used (LRU) algorithm – evicts the page leastd) Least Recently Used (LRU) algorithm – evicts the page least recently used because the a priori probability of its not being in the current working set is high.

20

Page Replacement Policy

F il f th LRU l ithFailure of the LRU algorithm.21

Alternative Page Replacement Algorithm

a) First-In First-Out (FIFO) algorithm – removes the least recently loaded page, independent of when this page was last referenced.

22

Page Internal Fragmentation

a) Internal Fragmentation waste: user’s program and data happen not to fill an integral number of pages exactlyhappen not to fill an integral number of pages exactly,

b) Using a small page size can minimize waste) g p g

c) On the other hand, a small page size means many pages, as ll l t bl i t i d twell as a large page table – more registers, increased cost,

and more time to load and save

23

Segmentation

a) In many cases, have two or more separate virtual address spaces may be much better than having only onespaces may be much better than having only one

b) Segments – many completely independent address spaces) g y p y p p

c) Each segment consists of a linear sequence of addresses. Different segments may have different lengths, and the lengths may change during execution.

d) Because each segment constitutes a separate address space, different segments can grow or shrink independently, without affecting each other.

24

Segmentation (1)

In a one dimensional address space with growing tablesIn a one-dimensional address space with growing tables, one table may bump into another. 25

Segmentation (2)

A segmented memory allows each table to growA segmented memory allows each table to grow or shrink independently of the other tables.

26

Segmentation (3)

Comparison of paging and segmentation.p p g g g

27

Implementation of Segmentation– Segmentation can be implemented in one of two ways: swapping and

paging.– In the former, some set of segments are in memory at a given instant., g y g

• If a reference to a segment not currently in memory, that segment is brought into memory.

• One or more segments may have to be written to the• One or more segments may have to be written to the disk.

• This is like demand paging, but pages are fixed size d t tand segments are not.

• External fragmentation can result because of this.

28

Implementation of Segmentation (1)

(a) (d) Development of external fragmentation(a)-(d) Development of external fragmentation. (e) Removal of the external fragmentation by compaction.29

Implementation of Segmentation– The second approach is to divide each segment up into fixed size

pages and demand page them.– In this scheme some of the pages of a segment may be in memoryIn this scheme, some of the pages of a segment may be in memory

and others may not be.– To page a segment, a separate page table is needed for each

segment.g• Since a segment is just a linear address space, all the

techniques we have seen so far for paging apply to each segmenteach segment.

• The only new feature here is that each segment gets its own page table.

30

Implementation of Segmentation (2)

Conversion of a two part MULTICS addressConversion of a two-part MULTICS address into a main memory address. 31

Virtual Memory on the Pentium 4– The Pentium 4 has a sophisticated virtual memory system that

supports demand paging, pure segmentation, and segmentation with pagingpaging.

– The heart of the Pentium 4 virtual memory consists of two tables: the LDT (Local Descriptor Table) and the GDT (Global Descriptor Table)Table).

• Each program has its own LDT, but there is a single GDT, shared by all programs.y g

32

Virtual Memory on the Pentium 4• The LDT describes segments local to each program,

including its code, data, stack, and so on, whereas the GDT d ib t t i l di th OSGDT describes system segments, including the OS itself.

• To access a segment, a Pentium 4 program first loads a selector for that segment into one of the segment registers.

• Each selector is a 16-bit number.Each selector is a 16 bit number.• One of the selector bits tells whether the segment is

local or global.Thi t th bit if th LDT GDT t• Thirteen other bits specify the LDT or GDT entry number. The other 2 bits relate to protection.

33

Virtual Memory on the Pentium 4

• Descriptor 0 is invalid and causes a trap if used.• At the time a selector is loaded into a segment register, the

corresponding descriptor is fetched from the LDT or GDT and stored in internal MMU registers so it can be accessed quickly.

• A descriptor consists of 8 bytes, including the segment’s base dd i d th i f tiaddress, size, and other information.

– The format of the selector makes locating the descriptor easy.y

• First either LDT or GDT is selected, based on bit 2.• Then the selector is copied to an MMU scratch register, and the 3

low-order bits are set to 0low-order bits are set to 0.

34

Virtual Memory on the Pentium 4

• Now the address of either the LDT or GDT table (kept in internal MMU registers) is added to it, to give a direct pointer to the descriptor.

– A (selector, offset) pair is converted to a physical address.A (selector, offset) pair is converted to a physical address.

• As soon as the hardware knows which segment register is being used, it can find the complete descriptor

di t th t l t i it i t l i tcorresponding to that selector in its internal registers.• If the segment does not exist (selector 0) or is currently

not in memory (P is 0), a trap occurs.not in memory (P is 0), a trap occurs.

35

Virtual Memory on the Pentium 4 (1)

A Pentium 4 selector.

A Pentium 4 code segment descriptor. Data segments differ slightly.36

Virtual Memory on the Pentium 4

• It then checks to see if the offset is beyond the end of the segment, in which case a trap also occurs.

• If the G (Granularity) field is 0, the LIMIT field is the exact segment size in bytes, up to 1 MB.

• If it is 1 the LIMIT field gives the segment size in pages• If it is 1, the LIMIT field gives the segment size in pages.• The Pentium 4 page size is never smaller than 4 KB, so

20 bits is enough for segments up to 232 bytes.• Assuming that the segment is in memory and the offset

is in range, the Pentium 4 then adds the 32-bit BASE field in the descriptor to the offset to form a linear address.

37

Virtual Memory on the Pentium 4 (2)

C i f ( l t ff t) i t li ddConversion of a (selector, offset) pair to a linear address.38

Virtual Memory on the Pentium 4

– If paging is disabled (by a bit in a global control register), the linear address is interpreted as the physical address and sent to the memory to read or write.• Thus, with paging disabled we have a pure

segmentation scheme with each segment’s base g gaddress given in its descriptor.

– If paging is enabled, the linear address is interpreted as a virtual address and mapped onto the physical address using page tables.• A two-level mapping is used.

39

Virtual Memory on the Pentium 4

• Each running program has a page directory consisting of 1024 32 bit entriesof 1024 32-bit entries.

• It is located at an address pointed to by a global register.

• Each entry points to a page table also containing 1024 32-bit entries.

• The page table entries point to page framesThe page table entries point to page frames.– To avoid making repeated references to memory, the Pentium 4 MMU

has special hardware support to look up the most recently used DIR-PAGE combinations quickly.q y

40

Virtual Memory on the Pentium 4 (3)

M i f li dd t h i l ddMapping of a linear address onto a physical address.41

Virtual Memory on the Pentium 4– If an application does not need segmentation, all segment registers

can be set up with the same selector, whose descriptor has BASE = 0 and LIMIT set to the maximumand LIMIT set to the maximum.

– The Pentium 4 also supports four protection levels, with level 0 being the most privileged and level 3 the least.• At each instant a running program is at a certain level• At each instant, a running program is at a certain level,

indicated by a 2-bit field in its PSW (Program Status Word).E h t l b l t t i l l• Each segment also belongs to a certain level.

42

Virtual Memory on the Pentium 4 (4)

P t ti th P ti 4Protection on the Pentium 4.43

Virtual Memory on the Pentium 4

– As long as a program restricts itself to using segments at its own level, g p g g g ,everything works fine.

– Attempts to access data at a higher level are permitted.– Attempts to access data at a lower level are illegal and cause traps.Attempts to access data at a lower level are illegal and cause traps.

44

Virtual I/O Instructions

a) Input/Output is one of the areas where the OSM and ISA levels differ considerably.ISA levels differ considerably.

b) Security reasons

c) Doing I/O at the ISA level is extremely tedious and lcomplex

45

Files

a) One way of organizing the virtual I/O is to use an abstraction called a fileabstraction called a file.

b) In its simplest form a file consists of a sequence ofb) In its simplest form, a file consists of a sequence of bytes written to an I/O device.

c) Different files have different lengths and other properties. (For example, files on a disk) The abstraction of a file allows virtual I/O to beabstraction of a file allows virtual I/O to be organized in a simple way.

46

Files

a) File I/O is done by system calls for opening, reading, writing, and closing files.reading, writing, and closing files.

b) The process of opening a file allows the operating system to locate the file on disk and bring into memory information necessary to access it.

c) The read system call must have:– An indication of which open file is to be read.– A pointer to a buffer in memory in which to put the data.– The number of bytes to be read.

d) The read call puts the requested data in the buffer.47

Files

a) Mainframe operating systems have a more sophisticated definition of file – A sequence of p qlogical records, each with a well-defined structure.

48

Implementation of Virtual I/O Instructions (1)

Reading a file consisting of logical records. (a) Before reading record 19. (b) After reading

record 19. 49

Implementation of Virtual I/O InstructionsInstructions

a) The allocation unit often consists of a block of consecutive sectors.of consecutive sectors.

b) A fundamental property of a file implementation is whether a file is stored in consecutive allocation units or not.

50

Implementation of Virtual I/O Instructions (2)

Disk allocation strategiesDisk allocation strategies. (a) A file in consecutive sectors.

(b) A file not in consecutive sectors. 51

Implementation of Virtual I/O InstructionsInstructions

a) The operating system sees the file as an ordered, although not necessarily consecutive,ordered, although not necessarily consecutive, collection of allocation units (on disk).

b) A table, file index, is needed to locate any arbitrary byte or logical record, given the allocation units and their actual disk addresses.allocation units and their actual disk addresses. (used by UNIX)

c) An alternative method of locating the allocation units of a file is to organize the file as a linked list (used by MS-DOS and Windows 95/98)list. (used by MS DOS and Windows 95/98)

52

Implementation of Virtual I/O InstructionsInstructions

a) Why do we need both consecutively allocated files and nonconsecutive allocated files?files and nonconsecutive allocated files?

53

Implementation of Virtual I/O InstructionsInstructions

a) In order to allocate space on the disk for a file, the operating system must keep track of which blocks are available and which are already in use storingare available, and which are already in use storing other files.

b) One method consists of maintaining a list of all the holes, a hole being any number of contiguous allocation units. This list is called the free list.

c) An alternative method is to maintain a bit map. If has the advantage of being constant in sizehas the advantage of being constant in size compared with the free list.

d) When any file on the disk is allocated and returned, the allocation list or table are updated.54

Implementation of Virtual I/O Instructions (3)

Two ways of keeping track of available sectors.Two ways of keeping track of available sectors. (a) A free list. (b) A bit map.

55

Directory Management Instructions

a) Information that is directly accessible to the computer without the need for human intervention is on-line as contrasted withthe need for human intervention is on line, as contrasted with off-line information.

b) The usual way for an operating system to organize on line filesb) The usual way for an operating system to organize on-line files is to group them into directories.

) S t ll idc) System calls provide– Create a file and enter it in a directory– Delete a file from a directory– Rename a file– Change the protection status of a file

d) Tree of directories56

Directory Management Instructions

A user file directory and the contents of aA user file directory and the contents of a typical entry in a file directory. 57

Virtual Instructions for Parallel ProcessingProcessing

a) Some computations can be most conveniently programmed for two or more cooperating processes running in parallel rather than for a single processorrunning in parallel rather than for a single processor. Others can be divided into pieces, which can then be carried out in parallel to decrease the elapsed time required for the total computationrequired for the total computation.

b) Physical limit (for subnanosecond computers)

c) On a computer with more than on CPU each of several cooperating processes can be assigned to its own CPU ( i lt l )(process simultaneously)

d) If only one processor available, the effect of parallel processing can be simulated by having the processor run each process in turn for a short time. 58

Virtual Instructions for Parallel Processing

(a) True parallel processing with multiple CPUs. (b) P ll l i i l t d b it hi(b) Parallel processing simulated by switching one

CPU among three processes. 59

Process Creationa) Most modern operating systems allow processes toa) Most modern operating systems allow processes to

be created and terminated dynamically.

b) Th t k f ll d t t ll t tb) The take full advantage, a system call to create a new process is needed to achieve parallel processing.

c) In some cases, the creating (parent) process maintains partial or complete control over themaintains partial or complete control over the created (child) process.

d) Vi t l i t ti i t f t t t t td) Virtual instructions exist for a parent to stop, restart, examine, and terminate its children. In other cases, once a process has been created, there is

f th t t f ibl t t tno way for the parent to forcibly stop, restart, examine, or terminate it. Two processes run 60

Race Conditions (1)

Use of a circular buffer.

61

Race Conditions (2)

Parallel processing with a fatal race condition.62

Race Conditions (3)

Parallel processing with a fatal race condition.63

Race Conditions (4)

Parallel processing with a fatal race condition.64

Race Conditions (5)

F il f th d i tiFailure of the producer-consumer communication mechanism. 65

Process Synchronization Using Semaphores (1)Semaphores (1)

The effect of a semaphore operation.

66

Process Synchronization Using Semaphores (2)Semaphores (2)

P ll l i i hParallel processing using semaphores.67

Process Synchronization Using Semaphores (3)Semaphores (3)

P ll l i i hParallel processing using semaphores.68

Process Synchronization Using Semaphores (4)Semaphores (4)

P ll l i i hParallel processing using semaphores.69

Critical Regions

a) While the CPU protects itself against simultaneous use, ) p g ,the code that interacts with the other serially reusable resources cannot.

b) Such code is called a critical region.

c) If two tasks enter the same critical region simultaneously, a catastrophic error occur.

70

Semaphores

a) The most common methods for protecting critical ) p gregions involves a special variable called a semaphore.

b) A h S i l ti th t t l kb) A semaphore S is a memory location that acts as a lock to protect critical regions.

c) Two operations: wait P(S), signal V(S)

71

void P(int S)void P(int S){

while (S == true);S = true;

}

void V(int S){

Semaphore is initialized to false{

S = false;}

false.

72

Semaphores

a) The wait operation suspends any program calling until the semaphore S is FALSE whereas theuntil the semaphore S is FALSE, whereas the signal operation sets the semaphore S to FALSE.

b) Code that enters a critical region is bracketed by calls to wait and signal. This prevents more than one process from entering the critical region.p g g

73

Process_1 Process_2..

.

.P(S)

.

.P(S)P(S)

critical regionV(S).

critical regionV(S).

.

...

74

Mailboxes and Semaphores

a) Mailboxes can be used to implement semaphores i h i iti t id d b this semaphore primitives are not provided by the operating system.

b) In this case, there is the added advantage that the pend instruction suspends the waiting

th th t ll iti f thprocess rather than actually waiting for the semaphore.

75

void P(int S){

int key = 0;int key 0;pend(key, S);

}

void V(int S){

int key = 0;post(key, S);

}

76

Counting Semaphores

a) The P and V semaphores are called binary ) p ysemaphores because they can take one of two values.

b) Alternatively, a counting semaphore can be used to protect pools of resources, or to keep track of the p p , pnumber of free resources.

77

void P(int S){

S ;S--;while(S < 0);

}

void V(int S){

S++;;}

78

void MP(int R) /* multiple wait */{

P(S); /* lock counter */R--; /* request a resource */if (R < 0) /* none available? */{

V(S); /* release counter */P(T); /* wait for free resource */P(T); / wait for free resource /

}V(S); /* release counter */

}

id MV(i t R) /* lti l i l */void MV(int R) /* multiple signal */{

P(S); /* lock counter */R++; /* free resource */if (R <= 0)( )

V(T);else

V(S); /* release counter */}

79

Counting Semaphores

a) The integer R keeps track of the number of free ) g presources. Binary semaphore S protects R, and binary semaphore T is used to protect the pool of resourcesresources.

b) The initial value of S is set to False, T to True, and R ) , ,to the number of available resources in the kernel.

80

OSM Level - ISA Level

a) Virtual Memorya) Virtual Memory

b) Virtual I/O Instructionsb) Virtual I/O Instructions

c) Virtual Instructions for Parallel Processing) g

81

UNIX (1)

A h b kd f th UNIX t llA rough breakdown of the UNIX system calls.82

UNIX (2)

Th t t f t i l UNIX tThe structure of a typical UNIX system.83

Windows XP

Th t t f Wi d XPThe structure of Windows XP.84

UNIX Virtual Memory

Th dd f i l UNIXThe address space of a single UNIX process.85

Windows XP Virtual Memory

Th i i l Wi d XP API ll f iThe principal Windows XP API calls for managing virtual memory. 86

UNIX Virtual I/O (1)

Th i i l UNIX fil t llThe principal UNIX file system calls.87

UNIX Virtual I/O (2)

A program fragment for copying a file using the UNIX system

calls. This fragment is in C because Java hides the low-level system calls and we are trying to expose them88

UNIX Virtual I/O (3)

Part of a typical UNIX

directory system.

89

UNIX Virtual I/O (4)

Th i i l UNIX di t t llThe principal UNIX directory management calls.90

Windows XP Virtual I/O (1)

The principal Win32 API functions for file I/OThe principal Win32 API functions for file I/O. The second column gives the nearest UNIX equivalent.91

Windows XP Virtual I/O (2)

A program fragment for copying a file using the Windows XP API

functions This fragment is in C because Java hides thefunctions. This fragment is in C because Java hides the low-level system calls and we are trying to expose

them 92

Windows XP Virtual I/O (3)

The principal Win32 API functions for directory management.

The second column gives the nearest UNIX equivalent, g q ,when one exists.

93

Windows XP Virtual I/O (4)

Th Wi d XP t fil t blThe Windows XP master file table.94

UNIX Process Management (1)

A t i UNIXA process tree in UNIX.95

UNIX Process Management (2)

Th i i l POSIX th d llThe principal POSIX thread calls.96