Main purpose of a computer system:

execute programs.These programs,together with the data they access,

must be in main memory(at least partially) during execution.

To improve both the utilization of the CPU and the speed of its responresponse

to users,the computer must keep several processes in memory.Many memory-management

schemes exist,reflecting variable approaches,and the effectiveness of each algotithm

depends on the situation.

MEMORY:https://www.quora.com/What-is-the-connection-between-logical-memory-physical-memory-page-fault-process-in-my-question

Primary storage (also main memory and physical memory) are generally used interchangeably to refer to the memory that is attached directly to the processor.

There is no secondary memory!!!!,so for valid-invalid bit attached to each entry in the page table:valid indicates in memory,which means in main memory.

Secondary storage is storage that is not directly connected to the CPU. The most common case of secondary storage is the hard disk.

Basic Hardware

Main memory and the registers built into the processor itself are the only storage that the CPU can access directly.There are machine instructions that take the memory addresses as

arguments,but none take disk addreses. Therefore any instructions in execution and any data being used by the instruction must be in on eof these direct access storage devices.

To Protect (1)the os from access by user processes  

       (2)user processes from one another

the protection must be provided by hardware.

1.first to make sure each process has a separate memory space.

To do this:

 Determine the range of legal addresses that the process can and only can access these

legal addresses.

->Two registers used:base and limit.

 base:holds the smallest legal physical memory address

 limit:specifies the size of the range.

 wKioL1buh9DwVBg-AAA4UxAQ8w8562.png

Protection of memory space is accomplished by having the CPU hardware compare

every address generated in user mode with the registers.

wKioL1bui8iAz4d9AABYdf1-Qb4217.png

This scheme allows the os to change the value of the registers,but prevents user programs 

from changing the registers'contents.

Address Binding

Usually,a program resides on a disk as a binary executable file.To be executed,the program

must be brought into memory and placed within a process.Depending on the memory management in use,the process may be moved between disk and memory during its execution.The processes on the disk that are waiting to be brought into memory for execution form the input queue.


The normal procedure is to select one of the processes in the input queue and to load that process into memory.As the process is executed,it accesses the instructions and data from memory.Eventually,the process terminates,and its memory space is declared available.

The binding of instructions and data to memory addresses can be done at 3 different stages:

1.Compile time-->If you know at compile time where the process will reside in memory,then absolute code can be generated.For example,if you know that a user process will reside starting at location R,then the generated compiler code will start at that location and extend up from there.If at some later time,the starting location changes,then it will be necessary to recompile this code.The MS-DOS .COM-format programs are bound at compile time.

2.Load time-->If it is not known at compile time where the process will reside in memory,then the compiler must generate relocatable code.In this case,the final binding is delayed until load time.If the starting address changes,we need only reload the user code to incorporate this changed value.

3.Execution time-->If the process can be moved during its execution from one memory segment to another,then binding must be delayed until run time.Special hw must be available for this schema to work.Most general-purpose os use this method.

wKioL1bukrjCVPgfAABJVeOf5cE455.png

Logican vs.physical address space

Logical address-->an address generated by the CPU.

Physical address-->an address seen by the memory unit-that is,the one loaded into the memory-address registerof the memory.

The compile-time and load-time address-binding methods generate identical logical and physical addresses.However the execution-time address-binding schemme results in difffering logical and physical address.In this case,we usually refer to the logical address ad virtual address.

Logical address space-->the set of all logical addresses generated by a program.

Physical address space-->the set of all physical addresses corresponding to these logical addresses.

MMU

Hardware device that maps tvirtual to physical address.

wKioL1bulaWx3LopAABM96zpp3E108.png

The base register is now called a relocation register.An access to location 346 is mapped to location 14346.

The user program never sees the real physical addresses.The program can create a pointer 346,store it in memory,manipulate it,and compare it with other addresses-all as the number 346.Only when it is used as a memory address(in an indirect load or store,perhaps)is it relocated relative to the base register.The user program deals with logical address.

The memory mapping hw converts logical addresses into physical addresses/


Dynamic loading

The entire program and all data of a process must be in physical memory for the process to execute.The size of a process is thus limited to the size of physical memory.Dynamic loading can obtain better memoty-space utilization. With it,a routine is not loaded until it is called,an unused routine is never loaded.This method is particularly useful when large amounts of code are needed to handle infrequently occuring cases,such as error routines.In this case,although the total program size may be large,the portion that is used(and hence loaded)may be much smaller. 

It does not require special support from the os.but the responsibility of the users to design their programs to take advantage of such a method.

Dynamic linking and shared Libraris

Dynamic linking,similar to dynamic loading.Here,though,linking,rather than loading,is postponed until execution time.This feature is usually used with system libraries,such as kanguage subroutine libraries.Without this facility,each program on a system must include a copy of its language libray(or at least thr routines referenced by the program)in the executable p_w_picpath.This requires wastes both disk space and main memory.

 With dynamic linking,a stub is included in the p_w_picpath for each library routine reference.The stub is a small piece of code that indicates how to locate the appropriate memory-resident library routine or how to locate the routine is not ready prese the stub is executed,it checks to see whether the needed routine is already in memory.if not,loads it into memory.Either way,the stub replaces itself with the address of the routine and executes the routine.Thus,the next time,that particular code segment is reached,the library routine is executed directly,incurring no cost for dynamic linking.Under this scheme,all processes that use a language libreary execute only one copy of the library code.

System also known as shared libraries.

Swapping

 A process must be in memory to be executed.A process,however,can be sawpped temporily out of memory to a banking store and then brought back into memory for continued execution(e.g.a multiprograming environment with a round-robin CPU-scheduling algorithm.)

 When the swapping policy is used for priority-based scheduling algorithms-->roll in,roll out.

 Major part of the swap time is transfer time.

 Ready queue->the systems maintains consisting of all processes whose memory p_w_picpaths are on the backing store or in the memory and are ready to run.

wKioL1bupQyzCEu6AACBHh5Rk5E512.png

Contiguous Memory Allocation

The main memory must accomodate both the os and the various user processes.We therefore need to allocate main memory in the most efficient way possible.Contiguous memory allocation is one method which each process is contained in a single contiguous section of memory.

Memory Allocation

multiple partion method->

wKiom1buxOHBY4_JAABFUb1sffs597.png

Dynamic Allocation Problem

concerns how to satisfy a request of size n from a list of holes(block of variable memory)

First-fit,best-fit,worst-fit

Fragmentation

External Fragmentation->total memory space exits to satisfy a request,but it's not contiguous.Storage is fragmented into a large number of small holes.

Solution->compaction:shuffle the memory contents so as to place all free memory together in one large block.It's possible only if relocation is dynamic,and is done at execution time

Internal Fragmentation->allocated memory may be slightly larger than requested memory;the size difference is emory internal to a partition,but not being used.

Another possible solution to the external-fragmentation problem is to permit the logical address space of the processes to be noncontiguous,thus allowing a process to be alloccated physical memory wherever such memory is available.To complementary techniques achieve this solution:

paging,segmentation and combined.

Paging

a memory-management scheme that permits the physical address space of a process to be noncontiguous.

wKioL1buzlPxmOk5AABFhRPJWfQ934.png 

Every address generated by CPU is divided into 2 parts:page number(p) and a page offset(d)

Basic method

involves breaking physical memory into fixed-sized blocks called frames and breaking logical memory into blocks of the same size called pages


when a process is to be executed,its pages are loaded into any available frames from their source(a file system or the backing store)

wKiom1bu0K_zWip1AAA8SMKTHKM238.png

the figure above is for a 32-byte memory with 4-bute pages.

Indexing into the page table,find that page 0 is in frame 5,thus,logical address 0 maps to physical address 20[=(5*4)+0]

Frame table->keep the allocation details of physical memory-which frames are allocated,which frames are available,how many total frames there are,and so on.

wKiom1bu1LTiYw5FAABesf3bYDI358.png

Implementation of Page table

The hw implementation of the page table(used both in logical memory and virtual memory) can be done in several ways:

registers: satisfactory when the page table is small.If large:

the page table is kept in main memory,and a page-table base register(PTBR) points to the page table.Changing only this one register,substantially reducing context-switch time.


If we want to access location i,we must first index into the page table,using the value in the PTBR offset by the page number for i.This task requires a memory access.It provides us with the frame number,which is combined with the page of offset to produce the actual address.We can then access the desired place in memory.->2 memory accesses needed to access a byte(page-table entry and byte)->slow


Solution: use a special,small,fast-lookup hardware cache,called a translation look-aside buffer(TLB).It is associative,high-speed memory.Each entry:key and a value.Find the value corresponding to the key->fast.

Address-space identifiers(ASIDs): Some TLBs store these in each TLB entry.An ASID uniquely identifies each process and is used to provide address-space protection for that process.

wKioL1bu4TzCQqA5AABdcMksm3I363.png

TLB miss:page number is not in the TLB or ASIDs do not match(a memory reference to the page table must be made.)

TLB hit:page number is found

When the frame number is obtained,we can use it to access memory.

hit ratio:the percentage of times that a particular page number is found in the TLB .

Increase hit ratio,increase the effective access time.

Protection

Memory protection in a paged environment is accomplished bt protection bits associated with each frame.Normally,these bits are kept in the page table.

valid-invalid bit-->valid: the associated page is in the process's logical address space and is thus a legal(or valid)page;invalid; not.

wKioL1bu5tOiERLkAABBHuWrTnE398.png

Any attempt to generate an address in page 6 or 7,will find that the valid-invalid bit is set to invalid,and the computer will trap to the os(invalid page reference)

Some systems provide hw,in the form of a page-table length register(PTLR),to indicate the size of the page table.This value is checked aagainst every logical address to verify that the address is in the valid range for the process.Failure of this test causes an error trap to the os. 

Structure of the page table

1.Hierachical Paging

Most modern computer systems support a large logical address space.In such an environment,the page table itself becomes excessively large.

Solution->divide the page table into smaller pieces as following differing ways.

(1)use a two-level paging algorithm,in which the page table itself is also paged.


tu 2l

with a page size of 4 KB.A logical address is divided into a page number consisting

of 20 bits and a page offset consisting of 12 bits.Because we page the page table,

the page number is further divided into a 10-bit page number and a 10-bit page offset.

Thus, a logical address is as follows:

tu pp

p1->an index into the outer page table

p2->the displacement within the page of the outer page table.

For a system with address space >32 bits,better use three-level paging

For 64-bit architectures,hierarchical page tables are generally considered inappropriate.

(require more levels of paging-a prohibitive number of memory access-to translate each logical address).

2.Hashed Page Tables

A common approach for handling address spaces larger than 32 bits.

The virtual page number in the virtual address is hashed into the hash table.

The virtual page number is compared with field 1 in the first element in the linked

list.If there is a match,the corresponding page frame(field2)is used to form the desired

physical address.If there is no match,subsequent entries in the linked list are

searched for a matching virtual page number.

3.Inverted Page Tables

tu ip

Usually,each process has an assoaited page table..The page table has one entry for each

page that the process is using(or one slot for each virtual address,regardless of the latter's validity).This table representation is a natural one,since processes reference

pages through the pages' virtual addresses.The operating system must then translate this

reference into a physical memory address.Since the table is sorted by virtual address,the 

operating system is able to calculate where in the table the associated physical address

entry is and to use that value directly. One drawback of this method is that each page table may consist of millions of entries which may consume large physical memory.


To solve this problem,can use an Inverted Page Table.An inverted page table has one 

entry for each real page(or frame) of memory.Each entry consists of the virtual address

of the page stored in that real memory location,with information about the process that

owns that page.Thus only one page table is in the system,and it has only one entry for each page of physical memory.

Each virtual address in the system consists of a triple

  <process-id,page-number,offset>

process-id assumes the role of the address-space identifiel.When a memory reference

occurs,part of the virtual address,consisting of <process-id,page-number>,is presented

to the memory subsystem.The inverted page table is then searched for a match.If a match is

found-say,at entry i-then the physical address<i,offset> is generated.If no match is found,

then an illegal address access has been attempted.


Segmentation

An important aspect of memory management that became unavoiable with paging

is the separation of the user's view of memory and the actual physical memory.The user's view of memory is not the same as the actual physical memory,it's mapped

onto physical memory.

tu uw

Segmentation is a memory-management scheme that supports this user's view of memory.A logical address space is a collection of segments.Each segment has 

a name and a length.The user therefore specifies each address by 2 quantities:

a segment name and an offset.

For simplicity,useing segment number rather than segment name:

            <segment-number,offser>

Normally,the user program is compiled,and the compiler automatically constructs segments reflecting the input program.

A C compiler might create separate segments for the following:

1.code

2.global variables

3.the heap,from which memory is allocated

4.the stacks used by each thread

5.the standard C library

Libraries that are linked in during compile time might be assigned separate segments.The loader would take all these segments and assign them segment numbers.

Hardware

Although the user can now refer to objects in the program by two-dimensional

address,the actual physical memory is still,of course,a one-dimensional sequence of bytes.Thus,we must define an implementation to map two-dimensional user-

defined addresses into one-dimensional physical addresses.This mapping is effected by a segment table.Each entry in the segment table has a segment base and a segment limit.

segment base:contains the starting physical address where the segment resides in memory;

segment limit:specifies the length of the segment

As the picture below:A logical address consists of 2 parts:a segment number,s,and an offset into that segment,d.The segment number is used as an index to the segment table.The offset d of the logical address must be between 0 and the segment limit.If it is not,we trap to the os(logical addressing attempt beyond end of segment).When an offset is legal,it is added to the segment base to produce the address in physical memory of the desired byte.The segment table is thus essentially an array of base-limit register pairs.

tu sh

tu es

A reference to segment 3,byte 852,is mapped to 3200(the base of segment 3)+852=4052.A reference to byte 1222 of segment 0 would resullt in a trap to the os,as this segment is only 1000 bytes long.

Example:The Intel Pentium

Supports both pure segmentation and segmentation with paging.

In Pentium systems,the CPU generates logical addresses,which are given to the segmentation unit.The segmentation unit produces a linear address for each logical address.The linear address is then given to the paging unit,which in turn genrates the physical address in main memory.Thus,the segmentation and paging units form the equivalent of the memory-management unit(MMU).

tu ll

The Pentium architecture allows a segment to be as large as 4GB,and the maximum number of segments per process is 16KB.The logical-address space of a process is

divided into two partitions.The 1st consists of up to 8KB segments that are private to that process.The second partition consists of up to 8KB segments that are shared among all the processes.Information about the 1st partition is kept in the local descriptor table(LDT);information about the second partition is kept in the global descriptor table(GDT).Each entry in the LDT and GDT consists of an 8-byte segment descriptor with detailed information about a particular segment,including the base location and limit of that segment.

The logical address is a pair(selector,offset),where the selector is a 16-bit number:

sgp

13                    1                    2

in which s designates the segment number,g indicates whether the segment is in the GDT or LDT,and p deals with protection.The offset is a 32-bit number specifying the location of the byte(or word) within the segment in question.


Pentium Paging

The Pentium architecture allows a page size of either 4KB or 4MB.For 4KB pages,the Pentium uses a two-level paging scheme in which the division of the 32-bit linear address is as follows:

Page number
Page offset
p1p2d
101012

tu ips

The ten high-order bits reference an entry in the outermost page table,which the Pentium terms the Page directory,(The CR3 register points to the page directory for the current process.)The page directory entry points to an inner page table that is indexed by the contents of the innermost ten bits in the linear address.Finally,the lower-order bits 0-11 refer to the offset in the 4-KB page pointed to in the page table.

Linux on Pentium Systems

Consider the Linux os running on the Intel Pentium architecture.Because Linux is designed to run on a variety of processors-many of which may provide only limiited support for segmentation-Linux does not rely on segmentation and uses it minimally.On the Pentium,Linux uses only six segments:

1.A segment for kernel code

2.A segment for kernel data

3.A segment for user data

4.A segment for user data

5.A task-state segment(TSS)

6.A default LDT segment

The segments for the user code and user data are shared by all processes running in user mode.This is possible because all processes use the same logical address space and all segment descriptors are stored in the global descriptor table(GDT)

Furthermore,each process has its own task-state segment(TSS),and the descriptor for this segment is stored in the GDT.The TSS is used to store the hardwarecontext of each process during context switches.The default LDT segment is normally shared by all processes and is usually not used.However,if a process requires its own LDT,it can create one and use that instead of the default LDT.

The linear address in Linux is broken into the following four parts:

global directorymiddle directorypage tableoffset

The number of bits in each part of the linear address varies according to architecture.However,as the Pentium architecture only uses a two-level paging model.How, then,does Linux apply its three-level model on the Pentium?In this situation,the size of the middle directory is zero bits,effectively bypassing the middle directory. 

tu tl

Each task in Linux has its own set of page tables and--just as in the Pentium paging architecture,the CR3 register points to the global directory for the task currently executing.During a context switch,the value of the CR3 register is saved and restored in the TSS segments of the tasks involved in the context switch.

tu s1

tu s2

user program can be declared execute-only,or read-write.This restriction is necessary with shared code or data and is generally useful in any case to provde simple run-time checks for common programming errors.


Why partition

higher data sequrity in case of disaster.

Whenan accident occurs,only the data in the partition that got the hit will be damaged.--->use fdisk to create partitions and set their properties

Mount points


All partitions are attached to the system via a mount point. The mount point defines the place of a particular data set in the file system. Usually, all partitions are connected through the root partition. On this partition, which is indicated with the slash (/), directories are created. These empty directories will be the starting point of the partitions that are attached to them. An example: given a partition that holds the following directories:

We want to attach this partition in the filesystem in a directory called /opt/media. In order to do this, the system administrator has to make sure that the directory/opt/media exists on the system. Preferably, it should be an empty directory. How this is done is explained later in this chapter. Then, using the mount command, the administrator can attach the partition to the system. When you look at the content of the formerly empty directory /opt/media, it will contain the files and directories that are on the mounted medium (hard disk or partition of a hard disk, CD, DVD, flash card, USB or other storage device).