The Fundamentals of Operating Systems
Recap: von Neumman Architecture
50f0900cbdb237
Copyright By PowCoder代写 加微信 powcoder
By loading diff your computer c
p ms into memory, ifferent functions
Instructions Data
Instructions Data
Recap: How processor executes a program
The program counter (PC) tells where the upcoming instruction is in
the memory
Processor fetches the instruction, decode the instruction, execute the The processor fetches the next instruction whenever it’s safe to do so
instruction, present the instruction results according to clock signals
instruction memory
120007a30: 0f00bb27 ldah gp,15(t12)
120007a34: 509cbd23 lda gp,-25520(gp)
120007a38: 00005d24 ldah t1,0(gp)
120007a3c: 0000bd24 ldah t4,0(gp)
120007a40: 2ca422a0 ldl t0,-23508(t1)
120007a44: 130020e4 beq t0,120007a94
120007a48: 00003d24 ldah t0,0(gp)
120007a4c: 2ca4e2b3 stl zero,-23508(t1)
120007a50: 0004ff47 clr v0
120007a54: 28a4e5b3 stl zero,-23512(t4)
120007a58: 20a421a4 ldq t0,-23520(t0)
120007a5c: 0e0020e4 beq t0,120007a98
120007a60: 0204e147 mov t0,t1
120007a64: 0304ff47 clr t2
120007a68: 0500e0c3 br 120007a80
Recap: von Neumman Architecture
50f0900cbdb237
By loading diff your computer c
p ms into memory, ifferent functions
Instructions Data
Instructions Data
Without OS
Operating System
Recap: What modern operating systems support?
Virtualize hardware/architectural resources
• Easyforprogramstointeractwithhardwareresources • Sharehardwareresourceamongprograms
• Protectprogramsfromeachother(security)
Execute multithreaded programs concurrently • Executemultithreadedprogramsefficiently
• Supportmultithreadedprogrammingmodel
Store data persistently • Storedatasafely
Operating systems: virtualizing computers
Process: the most important abstraction in modern OSs Restricted operations: kernel and user modes
Operating systems: virtualizing computers
The goal of an OS
Operating System
The idea of an OS: virtualization
Memory Memory
Memory Memory
Memory Memory
Operating System
von Neumann machine
The idea: virtualization
The operating system presents an illusion of a virtual machine
Processor Memory I/O
to each running program and maintains architectural states of a
Each virtualized environment accesses architectural facilities
through some sort of application programming interface (API)
Dynamically map those virtualized resources into physical 12
int main(int argc, char *argv[])
int cpu, status, i;
int *address_from_malloc;
Demo: Virtualization
cpu_set_t my_set;
CPU_ZERO(&my_set);
CPU_SET(4, &my_set);
sched_setaffinity(0, sizeof(cpu_set_t), &my_set); // Set affinity of this process to the defined mask, i.e. only 7.
getcpu system call to retrieve the executing CPU ID
if(argc < 2)
fprintf(stderr, "Usage: %s process_nickname\n",argv[0]);
// Define your cpu_set bit mask.
// Initialize it all to 0, i.e. no CPUs selected.
// set the bit that represents core 7.
status = syscall(SYS_getcpu, &cpu, NULL, NULL);
exit(1); }
srand((int)time(NULL)+(int)getpid());
a = rand();
create a random number
fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a);
print the value of a and address of a
fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a);
print the value of a and address of a again after sleep
return 0; }
The same processor!
Different values
Different values are preserved
The same memory address!
Some processes may use the same processor
Each process has the same address for variable a, but different You may see the content of a compiled program using objdump
Demo: Virtualization
Peer instruction
Before the lecture — You need to complete the required reading
During the lecture — I’ll bring in activities to ENGAGE you in exploring
your understanding of the material
Popup questions
Individual thinking — use polls in Zoom to express your opinion
Group discussion
• Breakout rooms based on your residential colleges!
• Use polls in Zoom to express your group’s opinion
Whole-classroom discussion — we would like to hear from you
Read Think
Poll close in
Why virtualization
How many of the following statement is true about why operating systems virtualize
running programs?
! Virtualizationcanhelpimprovetheutilizationandthethroughputoftheunderlying hardware
" Virtualizationmayallowthesystemtoexecutemoreprogramsthanthenumberof physical processors installed in the machine
# Virtualizationmayallowarunningprogramorrunningprogramstousemorethaninstall physical memory
$ Virtualizationcanimprovethelatencyofexecutingeachprogram
Now, open the png file sent through the chat (you cannot access it after we’re in break-out rooms)
After entering the break-out room, elect someone as your scriber — who will be responsible for express the thoughts/answers in your group today when your team is selected!
Once your group reach a consensus, go back to the main lobby and vote!
Now — let’s try to wrap up everything within 3 minutes!
Poll close in
Why virtualization
How many of the following statement is true about why operating systems virtualize
running programs?
! Virtualizationcanhelpimprovetheutilizationandthethroughputoftheunderlying hardware
" Virtualizationmayallowthesystemtoexecutemoreprogramsthanthenumberof physical processors installed in the machine
# Virtualizationmayallowarunningprogramorrunningprogramstousemorethaninstall physical memory
$ Virtualizationcanimprovethelatencyofexecutingeachprogram
running programs?
Why virtualization
How many of the following statement is true about why operating systems virtualize
! Virtualizationcanhelpimprovetheutilizationandthethroughputoftheunderlying hardware
" Virtualizationmayallowthesystemtoexecutemoreprogramsthanthenumberof physical processors installed in the machine
# Virtualizationmayallowarunningprogramorrunningprogramstousemorethaninstall
physical memory
$ Virtualizationcanimprovethelatencyofexecutingeachprogram
Make programs less machine-dependent
Latency v.s. Throughput
A 4K movie clip using H.265 coding takes 70GB in storage
If you want to transfer a total of 2 Peta-Byte video clips (roughly 29959 movies)
100 miles from UCR
Assume that you have a 100Gbps ethernet
• Throughput: 100 Gbits per second
• 2 Peta-byte (16 Peta-bits) over 167772 seconds = 1.94 Days
• Latency: first 70GB (first movie) in 6 seconds
Toyota Prius
100 miles from UCSD
75 MPH on highway!
Max load: 374 kg = 2,770 hard drives (2TB per drive) = 5.6 PB
10Gb Ethernet
Throughput/ bandwidth
100 Gb/s or 12.5GB/sec
2 Peta-byte over 167772 seconds = 1.94 Days
response time
You see nothing in the first 3.5 hours
You can start watching the first movie as soon as you get a frame!
Process: the most important abstraction in modern operating systems
The idea of an OS: virtualization
API API API
CPU CPU CPU
Memory Memory
I/O I/O I/O
Memory Memory
Memory Memory
Operating System
The most important abstraction in modern operating A process abstracts the underlying computer.
Program is a static file/combination of instructions Process = program + states
The states evolves over time
A process is a running program — a dynamic entity of a
A process may be dynamically switched out/back during the 27
Dynamically map those virtualized resources into physical
Virtualization
The operating system presents an illusion of a virtual machine to each running program — process
Each virtual machine contains architectural states of a von Neumann machine
Processor Memory I/O
Each virtualized environment accesses architectural facilities
through some sort of application programming interface (API)
resources — policies, mechanisms 28
— system calls
What happens when creating a process
Dynamic allocated data: malloc()
static data
Virtual memory
Local variables, arguments
Linux contains a .bss section for uninitialized global variables
static data
The illusion provided by processes
static data
Virtual memory
static data
Virtual memory
static data
Virtual memory
static data
Virtual memory
Virtually, every process seems to have a processor/memory space, but only a few of them are physically executing/using the installed DRAM.
Poll close in
What the OS must track for a process?
A. Stackpointer
B. Programcounter C. Process state
D. Registers
E. Alloftheabove
Which of the following information does the OS need to track
for each process?
Poll close in
What the OS must track for a process?
A. Stackpointer
B. Programcounter C. Process state
D. Registers
E. Alloftheabove
Which of the following information does the OS need to track
for each process?
What the OS must track for a process?
Which of the following information does the OS need to track
for each process?
A. Stackpointer
B. Programcounter C. Process state
D. Registers
E. Alloftheabove
You also need to keep other process information like an
unique process id, process states, I/O status, and etc...
OS has a PCB for each process
Switchframe
Process control block
Sometimes called Task Controlling Block, Task Struct, or
The data structure in the operating system kernel containing
the information needed to manage a particular process.
The PCB is the manifestation of a process in an operating 34
Example: struct task_struct in Linux
struct task_struct {
volatile long state;
void *stack;
atomic_t usage;
unsigned int flags;
unsigned int ptrace;
int on_rq;
Process state
/* -1 unrunnable, 0 runnable, >0 stopped */
/* per process flags, defined below */
int prio, static_prio, normal_prio;
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
unsigned int policy;
int nr_cpus_allowed;
cpumask_t cpus_allowed;
pid_t pid;
struct task_struct __rcu *real_parent;
struct task_struct __rcu *parent;
struct list_head children;
struct list_head sibling;
struct list_head tasks;
/* CPU-specific state of this task */
struct mm_struct *mm, *active_mm;
Process ID
Virtual memory pointers
Low-level architectural states
struct thread_struct thread;
You may find this struct in /usr/src/linux-headers-x.x.x-xx/include/linux/sched.h
Memory pointers in struct mm_struct
struct mm_struct {
struct vm_area_struct * mmap; /* list of VMAs */
unsigned long start_code, end_code, start_data, end_data;
unsigned long start_brk, brk, start_stack;
start of heap
end of heap
current stack pointer
Processor states in struct thread_struct
struct thread_struct {
struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
unsigned long
unsigned long
#ifdef CONFIG_X86_32
unsigned long
unsigned short
unsigned short
unsigned short
unsigned short
#ifdef CONFIG_X86_32
unsigned long
#ifdef CONFIG_X86_64
unsigned long
unsigned long
struct perf_event
unsigned long
unsigned long
unsigned long
unsigned long
sysenter_cs;
*ptrace_bps[HBP_NUM];
debugreg6;
ptrace_dr7;
error_code;
*io_bitmap_ptr;
io_bitmap_max;
Some x86 Register values
Program counter
struct vm86
unsigned long
unsigned long
struct fpu
unsigned long
#ifdef CONFIG_VM86
Virtualization
However, we don’t want everything to pass
through this API!
API API API API API API API API
CPU CPU CPU CPU CPU CPU CPU CPU Too slow!!!
Do you really need to track all
Memory Memory Memory Memory Memory Memory Memory Memory
I/O I/O I/O I/O I/O I/O I/O I/O
intermediate states?
Operating System
Solution — hardware support
Restricted operations: kernel and user modes
Restricted operations
Most operations can directly execute on the processor without OS’s
intervention
The OS only takes care of protected resources, change running processes
or anything that the user program cannot handle properly
Divide operations into two modes
• Usermode
• Restricted operations
• User processes
Kernel mode
• Can perform privileged operations
• The operating system kernel
Requires architectural/hardware supports 41
Architectural support: privileged instructions
The processor provides
• normalinstructionsandprivileged
Ring 3 Ring2
Ring 1 Ring 0
DeviceDrivers
DeviceDrivers
Applications
instructions
Normal instructions: ADD, SUB, MUL, and
Privileged instructions: HLT, CLTS, LIDT,
LMSW, SIDT, ARPL, and etc…
• Theprocessorprovidesdifferentmodes
• Userprocessescanusenormal instructions
• Privilegedinstructioncanonlybeusedif the processor is in proper mode
Leastprivileged Most privileged
How applications can use privileged operations?
Through the API: System calls
Implemented in “trap” instructions
• Raiseanexceptionintheprocessor
• Theprocessorsavestheexception PC and jumps to the corresponding exception handler in the OS kernel
user program
sbb %ecx,0x13(%rcx)
and %cl,(%rbx)
xor $0x19,%al
add %edx,(%rbx)
add %al,(%rax)
add %al,(%rbx)
Preserve register values/
add 0x1bad(%eax),%dh
add %al,(%eax)
decb PC0x5in2(t%oedmi)emory
in $0x8d,%al
mov %eax,0x101c
lea -0x2bb84(%ebx),%eax
mov %eax,-0x2bb8a(%ebx)
lgdtl -0x2bb8c(%ebx)
lea -0x2bf3d(%ebx),%eax
push $0x10
Restore register values/
…… PC from memory
return-from-trap
kernel/privileged mode
Interrupts, system calls, exceptions
All of them will trap to kernel mode
Interrupts: raised by hardware • Keystroke,networkpackets
System calls: raised by applications
• Displayimages,playsounds
Exceptions: raised by processor itself 44
• Dividedbyzero,unknownmemoryaddresses
Poll close in
Which of the following is true about kernel?
A. Itexecutesasaprocess
B. Itisalwaysexecuting,insupportofotherprocesses C. It should execute as little as possible.
What is “kernel”
Poll close in
Which of the following is true about kernel?
A. Itexecutesasaprocess
B. Itisalwaysexecuting,insupportofotherprocesses C. It should execute as little as possible.
What is “kernel”
What is “kernel”
Which of the following is true about kernel?
A. Itexecutesasaprocess —executingkernelfunctionwillthenrequirecontextswitch,but
B. Itisalwaysexecuting,insupportofotherprocesses
C. It should execute as little as possible.
context switch also needs to access kernel….
D. A&B E. B&C
The OS kernel only get involved when necessary
• System calls
• Hardware interrupts • Exceptions
The OS kernel works on behave of the requesting process — not a process
• Somehow like a function call to a dynamic linking library
• Preserve the current architectural states and update the PCB
• As a result — overhead of copying registers, allocating local variables for kernel code and etc…
— what if we only have one processor core? You cannot execute any other program…
Two reading quizzes next week
• Wewilldiscuss4papersnextweek
• Wesplitthemintotwosincethat’sprobablythefirsttimeyouread papers
Announcement
Engineering
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com