UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking...

40
1 UniMAP The Linux Kernel The Linux Kernel (OS) (OS)

Transcript of UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking...

Page 1: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

1

UniMAP

The Linux Kernel The Linux Kernel (OS)(OS)

Page 2: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

2

UniMAP

Outline

• Features of Linux kernel– Multitasking– Multiuser– Multiprocessing– Protected memory– Hierarchical File Ssytem– Linux Process Model– Linux File system– Thus… how does linux fit for real time?

Page 3: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

3

UniMAP

Features of Linux Kernels

• Multitasking– the CPU scheduler implements true

preemptive multitasking: a lower priority process will be preempted should a higher priority process comes in

• Multiuser– Support time sharing which allow multiple

user to access the system ; hence incorporates mechanism of inter user data protection.

Page 4: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

4

UniMAP

…cont

• Multiprocessing– support true symmetric multi-processing (SMP)

• Protected Memory– each process is allocated a specified private memory

space• Hierachical file system

– a common features in all modern OS, but.. With the following special features`

• Link – a file system entry which is NOT a file by itself but point to another file

• Device-Independent I/O- treat device as an entry in a file system: ie- access a device and file is exactly the same implementation wise.

Page 5: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

5

UniMAP

Linux Process Model

Data

Code

Data

Code

Process 1 Process 2

Data

Thread 1 Thread 2 Thread 3

Multi-threaded process Model : a process with 3 thread

Page 6: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

6

UniMAP

Linux Process model continue

• A thread is code only, all thread of a particular process share same DATA with equal access rights

Page 7: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

7

UniMAP

Process Structure

• In order to manage a process in the system, each process is represented by a task-struct data structure … refer to file sched.h

• Conceptually known as process control block (PCB)

• task_struct is quite large and compex

Page 8: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

8

UniMAP

Linux Process StateRunning

(User Mode)

Zombie

Sleeping or

Stopped

Run Queue Running(Kernel)

fork ( )

Page 9: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

9

UniMAP

Linux File system

• Hierarchical– Any number of subdirectories is possible– Root directory ‘/ ’– “.”xyz …. Means nothing in Linux– Case sensitive– File permission, for owner, group, others

• Type: ls –l• -rw-r-r- zahereel RTS

– Zahereel is owner, RTS is the group– First flag identiy the file type -,d,l

Page 10: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

10

UniMAP

Page 11: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

11

UniMAP

Directory Content

/bin Common programs, shared by the system, the system administrator and the users.

/bootThe startup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders we know today.

/dev Contains references to all the CPU peripheral hardware, which are represented as files with special properties.

/etcMost important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows

/home Home directories of the common users.

/initrd (on some distributions) Information for booting. Do not remove!

/lib Library files, includes files for all kinds of programs needed by the system and the users.

/lost+found Every partition has a lost+found in its upper directory. Files that were saved during failures are here.

/misc For miscellaneous purposes.

/mnt Standard mount point for external file systems, e.g. a CD-ROM or a digital camera.

/net Standard mount point for entire remote file systems

/opt Typically contains extra and third party software.

/procA virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.

/rootThe administrative user's home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.

/sbin Programs for use by the system and the system administrator.

/tmp Temporary space for use by the system, cleaned upon reboot, so don't use this for saving any work!

/usr Programs, libraries, documentation etc. for all user-related programs.

/varStorage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.

Page 12: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

12

UniMAP

/proc

• Wonderful file system– provide current info of the system status– Try this out

• cd /proc• cat cpuinfo• cat interrupts

– Observe and analyze

Page 13: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

13

UniMAP

So…is Linux an RTOS

• Multitaksing– Preemption is only allowed in user mode, not kernel mode.– ( In 2.6.x kernel …kernel preemption is allowed, but NOT if

– Handling interrupts– “bottom half “processing– holding “spin-lock, read lock, write-lock”– Executing scheduler

• Memory Management– Paging; the process of swapping pages in and out of virtual

memory is simply NOT deterministic/predictable• CPU scheduling

– Try to be fair to all process, may give priority to a low priority process

– ( in 2.6.x Big 0(1) )

Page 14: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

14

UniMAP

• Design issue– Putting highlights in reducing disk head

movement, • which in turns may cost higher priority process

having to wait longer than it should

– Batching task• At the cost of speed.• Continue doing something else, which delays

execution of process

Page 15: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

15

UniMAP

An analysis of 2.4/ 2.6

Page 16: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

16

UniMAP

Process Scheduling

• Done with surface understanding of what linux is.

• Lets get into more details.. On Linux Process Scheduling

Page 17: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

17

UniMAP

Process Scheduling

• Linux kernel implements 2 separate priority range

• First is the nice value, a value in the range of -20 to 19 with a default to zero. Larger nice value correspond to lower priority

• A process with a nice value of -20 receive maximum time slice.

• Time slice: minimum 10ms, default 150ms, maximum 300ms

Page 18: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

18

UniMAP

Process scheduling

• The second range is the real time priority

• Default range 0 – 99

• All real time process are at higher priority that normal process

• Linux implements real time priorities in accordance with POSIX

Page 19: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

19

UniMAP

Proc….sched….

• Linux provides to real –time scheduling policies, SCHED_FF and SCHED_RR

• Normal scheduling is SCHED_OTHER SCHED_FIFO implements without time slices-so it can run until it blocks or explicitly yield its processor

• SCHED_RR is identical to SCHED_FIFO except that each process can only run untill it exhausts a predetermined time slice.

Page 20: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

20

UniMAP

Scheduler – system calls

• nice() Set a process's nice value• sched_setscheduler() Set a process's scheduling policy• sched_getscheduler() Get a process's scheduling policy• sched_setparam() Set a process's real-time priority• sched_getparam() Get a process's real-time priority• sched_get_priority_max() Get the maximum real-time priority

• sched_get_priority_min() Get the minimum real-time priority

• sched_rr_get_interval() Get a process's timeslice value

Page 21: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

21

UniMAP

Dual Mode

• In order to run Linux, the computer hardware must provide two modes of execution: User and Kernel.

• Each process has virtual address space, references to virtual memory are translated to physical memory locations using set of address translation maps

• When current process yields CPU to another process (a context switch), the kernel loads these registers with pointers to translation of new process

Page 22: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

22

UniMAP

Context of Process

• The Linux kernel is reentrant

• Kernel functions may execute either in process context or in system context

• User code runs in user mode and process context, and can access only the process space

Page 23: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

23

UniMAP

Context of Process

• System calls and exceptions are handled in kernel mode but in process context, and may access process and system space

• Interrupts and system wide tasks are handled in kernel mode and system context, and must only access system space

Page 24: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

24

UniMAP

Memory Mgmt

Page 25: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

25

UniMAP

Introduction

• One of important kernel sub system

• Virtual memory: programmer need not to worry about the size of RAM ( Large address space)

• OS jargon: – static allocation internal fragmentation– Dynamic allocation external fragmentation

Page 26: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

26

UniMAP

Memory Management

• Some portion of RAM is utilize for semi-permanently keeping the Kernel Code and static kernel data structure.

• The left over RAM is the valuable resource named Dynamic Memory which is needed both by processes as well as the kernel.

• Thus the performance of the sytem is totally dependent on how efficient this memory is managed.

Page 27: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

27

UniMAP

Virtual Memory

• For a 32 bit OS, the VM size will be 2 to the power of 32 i.e: 4GB. But the RAM size may be much smaller

• Each process has a separate virtual address space

• Each is protected from others• Support shared VM, more than one

process has access to a shared page• Uses paging technique

Page 28: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

28

UniMAP

… cont

• Yes… Linux support virtual memory mgmt which is good for a general purpose operating system…coz it gives the programmer luxury of memory which was a constraint long time ago.

• However, for embedded system which require high responsiveness, virtual memory is no good coz it is implemented at the cost of responsiveness.

• In Linux 2.6 u can get a linux kernel without the virtual memory part.

Page 29: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

29

UniMAP

File system

Page 30: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

30

UniMAP

File system

• Why?– To save date for later used– Large amount of data should be able to be

stored– Multiple access to the same data by different

process concurrently– Soln: store those data on units called files on

disk or other media

Page 31: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

31

UniMAP

Different types of filesFile Types

regular Directory Special

Device Link Socket FIFO

Soft

Hard

Character

Block

Page 32: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

32

UniMAP

File types

• Regular: contains user data, in ASCII or binary• Directories: these are system files used to

maintain the structure of the file sys• Character: related to I/O, used to model serial

I/O device like such as terminals and printers– /dev/tty, /dev/lp , dev/net

• Block: used to model disk– /dev/hd1, /dev/hd2

Page 33: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

33

UniMAP

FS creation

• When a file System is created, Linux creates a number of blocks on that device. These blocks are:

• Boot blocks contains bootstrap code, which is used when the system is booting

Boot block Super block Data Blocks I node table

Page 34: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

34

UniMAP

File System - super block

• Each device also contains more than one copies of the super-block as the super block contains information that must be available to use the device

• If the original super block is corrupt, an alternate super-block can be used to mount the file system.

Page 35: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

35

UniMAP

File System- Super block

• The super block contains info, such as:– A bitmap of blocks on the device, each bit

specifies whether a block is free or in use.– The size of the data block– The count of entries in the I-node table– The date and time when the file system was

last checked– The date and time when the file system was

last backed up

Page 36: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

36

UniMAP

File Sys- I node table

• The I node table contains an entry for each file stored in the file system. The total number of I nodes in the file sys determine the number of files that a file system can contain.

• When a file system is created, the I node for the root directory of the file system is automatically created.

Page 37: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

37

UniMAP

File Sys- I node table

• Each I node contains the following info:– The file owner UID and GID– File type and access permissions– Date and time the file was created, last

modified, last accessed– Size of the file– Number of hard links to the file– Each I node entry can track a very large file

Page 38: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

38

UniMAP

File I/O- System Calls

• System calls for the file I/O– open - To open or create a file– read, write - to perform file I/O– lseek - to seek a location in the file– close - to close an open file– dup,dup2 - to duplicate the file descriptors– fcntl - file control– stat - to obtain info about a file

Page 39: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

39

UniMAP

“ I know you believe you understand what you think I said, but I’m not sure you realize that what you heard was not what I meant.” … so…

Page 40: UniMAP 1 The Linux Kernel (OS). UniMAP 2 Outline Features of Linux kernel –Multitasking –Multiuser –Multiprocessing –Protected memory –Hierarchical File.

40

UniMAP

TQVM

• It is a good practice to always re- clarify what you think you understand as this will reduce the ambiguity.

• How to do that, raise your hand and say– Can I clarify my understanding– I just wanna make sure that I got u right– Or…etc etc