Linux Memory Management

32
Linux Memory Management Suvendu Kumar Dash [email protected] M.Tech ECE VtP1492

description

Linux Memory Management 1.Memory Structure of Linux OS. 2.How Program is loaded into the memory. 3.Address Translation. 4.Feature for Multithreading and Multiprocessing.

Transcript of Linux Memory Management

Page 1: Linux Memory Management

Linux Memory Management

Suvendu Kumar [email protected] ECEVtP1492

Page 2: Linux Memory Management

04/12/2023 [email protected] 2

Memory Structure of Linux OS. How Program is loaded into the memory. Address Translation. Feature for Multithreading and

Multiprocessing.

Overview

Page 3: Linux Memory Management

04/12/2023 [email protected] 3

Linux is one of popular version of UNIX

operating System. It is open source as its source code is freely

available. It is free to use. Linux was designed

considering UNIX compatibility. It's functionality list is quite similar to that of

UNIX.

Linux OS

Page 4: Linux Memory Management

04/12/2023 [email protected] 4

Linux Operating System has primarily three components Kernel - Kernel is the core part of Linux. It is responsible for

all major activities of this operating system. It is consists of various modules and it interacts directly with the underlying hardware. Kernel provides the required abstraction to hide low level hardware details to system or application programs.

System Library - System libraries are special functions or programs using which application programs or system utilities accesses Kernel's features. These libraries implements most of the functionalities of the operating system and do not requires kernel module's code access rights.

System Utility - System Utility programs are responsible to do specialized, individual level tasks.

Components of Linux System

Page 5: Linux Memory Management

04/12/2023 [email protected] 5

Components of Linux System

Page 6: Linux Memory Management

04/12/2023 [email protected] 6

User Space Kernel Space Data Flow Between User Space and Kernel

Space Architecture

Kernel Architecture Overview

Page 7: Linux Memory Management

04/12/2023 [email protected] 7

The User Space is the space in memory, where

user processes run. This memory is above the Kernel.

It includes the rest of available memory. This Space is protected.

The system prevents one process from interfering with another process.

Only Kernel processes can access a user process

Kernel Architecture Overview..User Space

Page 8: Linux Memory Management

04/12/2023 [email protected] 8

The Kernel Space is the space in memory

where all kernel services are provided via kernel processes.

The user has access to it only through the system call. A user process becomes a kernel process when

it executes a system call.

Kernel Architecture Overview..Kernel Space

Page 9: Linux Memory Management

04/12/2023 [email protected] 9

User Space and Kernel Space are in different

spaces.

When a System Call is executed, the arguments to the call are passed from User Space to Kernel Space.

Kernel Architecture Overview..System Call

Page 10: Linux Memory Management

04/12/2023 [email protected] 10

Kernel Architecture Overview..User Space and Kernel Space

Relationship

Page 11: Linux Memory Management

04/12/2023 [email protected] 11

Kernel Architecture Overview..

Architecture

Page 12: Linux Memory Management

04/12/2023 [email protected] 12

Address Translation

Page 13: Linux Memory Management

04/12/2023 [email protected] 13

Address translations are kept in a special

cache called Translation Look-Aside Buffer or TLB

TLB must be on chip; its access time is comparable to cache.

Address Translation

Page 14: Linux Memory Management

04/12/2023 [email protected] 14

Address TranslationTypical TLB Format

Tag: Portion of virtual address Data: Physical Page number Dirty: since use write back, need to know

whether ornot to write page to disk when replaced Ref: Used to help calculate LRU on replacement Valid: Entry is valid Access rights: R (read permission), W (write

perm.)

Page 15: Linux Memory Management

04/12/2023 [email protected] 15

TLBs usually small, typically 128-256 entries. Like any other cache, the TLB can be fully

associative, set associative, or direct mapped.

Address TranslationTranslation Look_Aside

Buffers

Page 16: Linux Memory Management

04/12/2023 [email protected] 16

Assume 32 entries, fully associative TLB(Alpha AXP 21064)1: Processor sends the virtual address to all tags2: If there is a hit (there is an entry in TLB with that Virtual Page number and valid bit is 1) and there is no access violation, then3: Matching tag sends the corresponding Physical Page number4: Combine Physical Page number and Page Offset to get full physical address

Address TranslationTranslation Look_Aside

Buffers

Page 17: Linux Memory Management

04/12/2023 [email protected] 17

Feature for Multithreading and Multiprocessing

Page 18: Linux Memory Management

04/12/2023 [email protected] 18

What is Thread? A thread is a flow of execution through the process code, with its

own program counter, system registers and stack. A thread is also called a light weight process. Threads provide a way to improve application performance through

parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.

Each thread belongs to exactly one process and no thread can exist outside a process.

Each thread represents a separate flow of control. Threads have been successfully used in implementing network

servers and web server. They also provide a suitable foundation for parallel execution of

applications on shared memory multiprocessors.

Multithreading

Page 19: Linux Memory Management

04/12/2023 [email protected] 19

Multithreading..

Page 20: Linux Memory Management

04/12/2023 [email protected] 20

Multithreading..

Advantages of Thread Thread minimize context switching time. Use of threads provides concurrency within a

process. Efficient communication. Economy- It is more economical to create and

context switch threads. Utilization of multiprocessor architectures to a

greater scale and efficiency.

Page 21: Linux Memory Management

04/12/2023 [email protected] 21

Multithreading..

Types of Thread

Threads are implemented in following two ways User Level Threads -- User managed

threads Kernel Level Threads -- Operating System

managed threads acting on kernel, an operating system core.

Page 22: Linux Memory Management

04/12/2023 [email protected] 22

Multithreading..

User Level Threads In this case, application

manages thread management kernel is not aware of the existence of threads.

The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts.

The application begins with a single thread and begins running in that thread.

Page 23: Linux Memory Management

04/12/2023 [email protected] 23

Multithreading..

ADVANTAGES OF USER LEVEL THREADS: Thread switching does not require Kernel mode

privileges. User level thread can run on any operating system. Scheduling can be application specific in the user level

thread. User level threads are fast to create and manage.DISADVANTAGES OF USER LEVEL THREADS: In a typical operating system, most system calls are

blocking. Multithreaded application cannot take advantage of

multiprocessing.

Page 24: Linux Memory Management

04/12/2023 [email protected] 24

Multithreading..

Kernel Level Threads In this case, thread management done by the Kernel. There is no thread management code in the application area.

Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of

the threads within an application are supported within a single process.

The Kernel maintains context information for the process as a whole and for individuals threads within the process. Scheduling by the Kernel is done on a thread basis.

The Kernel performs thread creation, scheduling and management in Kernel space.

Kernel threads are generally slower to create and manage than the user threads.

Page 25: Linux Memory Management

04/12/2023 [email protected] 25

Multithreading..

ADVANTAGES OF KERNEL LEVEL THREADS: Kernel can simultaneously schedule multiple threads

from the same process on multiple processes. If one thread in a process is blocked, the Kernel can

schedule another thread of the same process. Kernel routines themselves can multithreaded.DISADVANTAGES OF KERNEL LEVEL THREADS: Kernel threads are generally slower to create and

manage than the user threads. Transfer of control from one thread to another within

same process requires a mode switch to the Kernel.

Page 26: Linux Memory Management

04/12/2023 [email protected] 26

Multithreading..

Multithreading Models:

Multithreading models are three types Many to many relationship. Many to one relationship. One to one relationship.

Page 27: Linux Memory Management

04/12/2023 [email protected] 27

Multithreading..

Many to Many Model In this model, many user

level threads multiplexes to the Kernel thread of smaller or equal numbers. The number of Kernel threads may be specific to either a particular application or a particular machine.

Following diagram shows the many to many model. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallels on a multiprocessor.

Page 28: Linux Memory Management

04/12/2023 [email protected] 28

Multithreading..

Many to One Model Many to one model maps many

user level threads to one Kernel level thread. Thread management is done in user space. When thread makes a blocking system call, the entire process will be blocks. Only one thread can access the Kernel at a time , so multiple threads are unable to run in parallel on multiprocessors.

If the user level thread libraries are implemented in the operating system in such a way that system does not support them then Kernel threads use the many to one relationship modes.

Page 29: Linux Memory Management

04/12/2023 [email protected] 29

Multithreading..

One to One Model There is one to one

relationship of user level thread to the kernel level thread. This model provides more concurrency than the many to one model. It also another thread to run when a thread makes a blocking system call. It support multiple thread to execute in parallel on microprocessors.

Disadvantage of this model is that creating user thread requires the corresponding Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship model.

Page 30: Linux Memory Management

04/12/2023 [email protected] 30

Multiprocessing

Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system. The term also refers to the ability of a system to support more than one processor and/or the ability to allocate tasks between them.

According to some on-line dictionaries, a multiprocessor is a computer system having two or more processing units (multiple processors) each sharing main memory and peripherals, in order to simultaneously process programs

Page 32: Linux Memory Management

04/12/2023 [email protected] 32