CS代考计算机代写 cache file system assembly assembler compiler ECS 150 – OS Structure

ECS 150 – OS Structure
Prof. Joël Porquet-Lupine
UC Davis – 2020/2021
Copyright © 2017-2021 Joël Porquet-Lupine – CC BY-NC-SA 4.0 International License /
1 / 16

OS Layers: overview
Application Libraries

Application Libraries
User Kernel
Portable OS layer Machine-dependent layer
2 / 16
/

OS Layers: details
Application
Libraries
Application
Libraries

User Kernel
Portable OS layer
Machine-dependent layer
Application(s)
User function calls
Written by programmers Compiled by programmers
int main(void)
{
statements;

func1();

func2();

return 0;
}
void func1(void)
{
statements;
… }
int func2(void)
{
statements;

func3();

return val;
}
void func3(void)
{
statements;
… }
3 / 16
/

OS Layers: details
Application
Libraries
Application
Libraries

User Kernel
Portable OS layer
Machine-dependent layer
Libraries
Definition
Via standard headers (e.g. stdio.h, stdlib.h, math.h)
Used like regular functions Declaration
Pre-compiled objects (e.g.
Input to linker (e.g. Code inclusion
Included in executable directly Or resolved at load-time
, libc.a, libm.so) )
libc.so.6
gcc -lc -lm
4 / 16
/

All about applications + libraries
Application compilation
foo.c
gcc (cpp + cc + as)
foo.o
bar.c
gcc (cpp + cc + as)
bar.o
gcc (ld)
a.out

GCC can pre-process, compile, assemble and link together Preprocessor (cpp) transform program before compilation
Compiler ( ) compiles a program into assembly code
Assembler ( ) compiles assembly code into relocatable object file Linker (ld) links object files into an executable
cc
libc.a / libc.so
as
5 / 16
/

All about applications + libraries
Application loading 101 Directly from executable
Code (a.k.a. text)
Instructions Data
Global variables
Created at runtime
Stack
Local variables
Heap
malloc() area
Segment characteristics
Memory
Disk
High address
a.out
Stack
Heap
Data
Code
Low address
Separate code and data for permissions and sharing reasons Maximize space for stack/heap
6 / 16
/

All about applications + libraries
Application dynamic loading
By default, loader dynamically prepares application for execution (unless compiled with -static)
Loaded before the application by the kernel
Read the executable file, and lays out the code, data (using syscalls) Dynamically links to shared libraries
$ ldd a.out
libc.so.6 => /usr/lib/libc.so.6 (0x00007fab5382b000) /lib64/ld-linux-x86-64.so.2 (0x00007fab53bc9000)
Application
Shared libraries
a.out
loader
7 / 16
/

All about applications + libraries
Static and dynamic libraries
#include #include
int main(void)
{
printf (“%f\n”, cos(2.0));
return 0; }
Dynamic
Static
$ gcc main.c -lm $ ldd a.out
linux-vdso.so.1
libm.so.6
libc.so.6
/lib64/ld-linux-x86-64.so.2
$ ./a.out -0.416147
$ gcc main.c /usr/lib/libm-2.28.a $ ldd a.out
linux-vdso.so.1
libc.so.6
/lib64/ld-linux-x86-64.so.2
$ ./a.out -0.416147
Math code will be loaded upon execution, by loader
Math code is inserted as part of the executable
8 / 16
/

All about applications + libraries
Dynamically loaded libraries
#include #include
int main(void)
{
void *handle;
double (*cosine)(double); char *error;
handle = dlopen (“/lib/libm.so.6”,
if (!handle) return 1;
RTLD_LAZY);
cosine = dlsym(handle, “cos”); if (!cosine)
return 1;
printf (“%f\n”, (*cosine)(2.0));
dlclose(handle);
return 0; }
$ gcc main.c -ldl $ ldd a.out
linux-vdso.so.1
libdl.so.2
libc.so.6
/lib64/ld-linux-x86-64.so.2
$ ./a.out -0.416147
Math code is neither part of the executable, nor is it referenced
Loaded at runtime only if specific code is executed
Handle case where library doesn’t exist
Great for plugins
9 / 16
/

OS Layers: details
Application
Libraries
Application
Libraries

User Kernel
Portable OS layer
Machine-dependent layer
Portable OS layer
Implementation of most system calls
High-level kernel code (i.e., top-half) for most subsystems
Virtual File System (VFS)
Inter-Process Communication (IPC) Process scheduler
Virtual memory
Networking, Sound, Cryptography, etc.
10 / 16
/

OS Layers: details
Application
Libraries
Application
Libraries

User Kernel
Portable OS layer
Machine-dependent layer
Machine-dependent layer
Bootstrap
System initialization
Exception handler (exceptions, interrupts and syscalls) I/O device drivers
Memory management
Processor mode switching
Processor management
11 / 16
/

OS Interfaces
Application
Libraries
Application
Libraries
API
UAPI
HAL
ISA+ABI

User Kernel
Portable OS layer
Machine-dependent layer
Processor (+ hardware system)
API (Application Programming Interface): interface between pieces of code UAPI (User API): syscall interface between apps and kernel
Software Hardware
HAL (hardware-abstraction layer), interface inside kernel between arch-independent code and arch-dependent code
ISA (Instruction Set Architecture): list of processor instructions
ABI (Application Binary Interface): interface between code and processor
12 / 16
/

Kernel structure
Monolithic kernel
Concept
Entire kernel code linked together in a single large executable
System call interface between kernel and applications

syscall
User Kernel
Application
Application
Monolithic kernel
Examples
GNU/Linux Unix
BSD
Pros and cons
Great performance
But increased potential for instability
Crash in any function brings the whole system down
kernel panic
13 / 16
/

Kernel structure
Microkernel Concept
Most kernel services implemented as regular user-space processes
Microkernel communicates with services using message passing
syscall
User Kernel
Application
Services
Micro-kernel
Examples
Minix Mach L4
Pros and cons
Great fault isolation
But inefficient (boundary crossings)
14 / 16
/

Kernel structure
Hybrid kernel Concepts
Trusted OS services implemented in kernel
Non-trusted OS services implemented as regular user-space processes
syscall
User Kernel
Best of both worlds?
Internal Services
Hybrid-kernel
Examples
Windows macOS
Pros and cons
Application
Monolithic kernel for the most part But user-space device drivers
3rd-party Services
15 / 16
/

Linux Kernel
Simplified internal structure
Linux kernel
Virtual File System
Linux kernel SCI (System Call Interface)
I/O subsystem
Sockets
Memory management subsystem
Process management subsystem
Virtual memory
Signal handling
Terminals
File systems
Netfilter / Nftables
Network protocols
Generic block layer
Paging page replacement
process/thread creation & termination
Linux kernel
I/O Scheduler
Block device drivers
Linux kernel Packet Scheduler
Network device drivers
Page cache
Linux kernel
Process Scheduler
Character device drivers
IRQs
Dispatcher
16 / 16
/
Line discipline