Title: ecs150 Spring 2006: Operating System
1ecs150 Spring 2006Operating System1 OS
Architecture, Kernel, Process
- Dr. S. Felix Wu
- Computer Science Department
- University of California, Davis
- http//www.cs.ucdavis.edu/wu/
- sfelixwu_at_gmail.com
2VM/MVS, DOS, Win95/98/ME/2000/XP, Freebsd/Linux,
MacOS-10, Mach, Minix, PalmOS, uCOS, TinyOS,
3.where applications meet Hardware!!!
Applications..
OS
Hardware CPU/Memory/HD/DVD/Wireless
4Information Router
- One NIC ?a processs user-level memory
- One file ?another file
- OS kernel layer
- Hardware layer
5.where applications meet Hardware!!!
Applications..
OS
Hardware CPU/Memory/HD/DVD/Wireless
6virtualization
Unmodified Applications
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
Standard Full Virtualization ? e.g.,
VirtualPC WindowXP
Hardware
7(No Transcript)
8Programmable Virtualization
Unmodified Applications
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
Programmable Full Virtualization
API
DLVM
DLVM
Hardware
9FreeBSD
Unmodified Applications
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
API
Virtual PC or VMware
Hardware
10This quarter.
- The internals of OS
- The basic design principles of OS
- The skills to modify or implement an OS.
11Operating System
- An interesting balance between
- Theories and Practical Experiences/Experiments
- Architectural Concept and Detailed Design
- Formal Verification and Empirical Validation
12About the Instructor
- S. Felix Wu
- sfelixwu_at_gmail.com, x4-7070
- Office 3057 Engineering II
- Office Hours
- 1-2 p.m. on Tuesday and Friday
- by appointment
13About the TA
- TA ?Valerie Szudziejka (szudziej_at_cs.ucdavis.edu)
- Office Hours TBA
- Discussion
14about Web site
- http//www.cs.ucdavis.edu/wu/ecs150/
- all lectures, notes, announcements, homework
assignments, tools, papers will be there.
15Textbook
Reading this book itself is a major
challenge!! But, you really learn when you go
through this process!
"The Design and Implementation of the FreeBSD
Operating Systems" by Marshall Kirk McKusick and
George V. Neville-NeilAddison Wesley
Professional, 2005, ISBN 0-201-70245-2.
http//www.freebsd.org/
16Prerequisites
- Programming Languages C and assembly (ecs50)
- Date Structure (ecs110) and basic Computer
Architecture (ecs154a/eec70). - ecs40
- Please talk to me if you have any concern.
17Syllabus
- Process/Kernel (09)
- Memory Management (06)
- midterm
- IO File Systems (10)
- Others (03)
- final
18OS Principles/Concepts
- What is kernel?
- What is the difference between a process and a
thread? - What is the difference between user-level and
kernel-level threads? - What is the difference between a system call and
a library function call? - What are SJF, RR, Lottery, LRU, TLB, Second
Chance? - How to do Mutual Exclusion?
- What is the difference between deadlock
prevention and avoidance? - What are the differences among hardware
interrupt, hardware trap, and software trap?
19OS
- Lets examine OS concepts in a realistic context
FreeBSD - Then, we can re-think those concepts.
- And, maybe you will realize later that some of
the concepts are either misleading or
irrelevant in certain context.
20Principles vs. Practice
- Ideas and Theories first, then we will go over
some FreeBSD code segments. - You will need to learn FreeBSD internals for
programming assignments!! - The first few discussion sessions will be
dedicated to FreeBSD internals. - Most of the discussion sessions are very
important and they will appear in the exams and
homeworks.
21Course Requirements
- 48 Programming Assignments
- teamwork 13 students
- 3 Programming Assignments (12, 18, 18)
- HW1 is out (check the website).
- 16 In-class open-book midterm
- 32 open-book final
- 04 Participation of Lectures and Discussion
sessions. - Deducted if missed more than three sessions.
22Grading
- I will give /- grades.
- possible grading (not finalized)
- A gt 93 A- gt 90 B gt 87
- B gt 84 B- gt 81 C gt 78
- C gt 75 C- gt 72 D gt 60
23FreeBSD
- Your team need to have access to a FreeBSD
environment - I386, VMware, VirtualPC
24The Structure of OS
- The Kernel
- Processes and Threads
- The System Call Interface
25What is kernel?
26Kernel
- The basic OS services
- Which services? What is it doing?
- Lets check a couple examples
27.what are the basic services?
OS
28FreeBSD Kernel Services
- Timer/clock, descriptor, process
- Memory Management paging/swapping
- I/O control and terminal
- File System
- Inter-process communication
- Networking
29(No Transcript)
30Kernel of SVR2 of ATT Unix
User programs
Libraries
trap
user
System Call Interface
File subsys
Process Control Subsys.
Inter-Process Communication
Buffer cache
kernel
Scheduler
Character block device drivers
Memory Management
Hardware Control
hardware
31Kernel Processes
- The concept of application process
32Kernel and User Space
Memory space for this process
Process FOO
program
System call (or trap into the kernel)
conceptually
Process FOO in the Kernel
System Call
Kernel Resources (disk or IO devices)
33Processes
gt ps PID TTY TIME CMD 2910 pts/4
000 tcsh gt ps -ef UID PID PPID C
STIME TTY TIME CMD root 0 0 0
Sep 25 ? 001 sched root 1 0
0 Sep 25 ? 000 /etc/init - root
2 0 0 Sep 25 ? 000 pageout
root 3 0 0 Sep 25 ? 001
fsflush root 223 1 0 Sep 25 ?
000 /usr/lib/utmpd root 179 1 0 Sep
25 ? 000 /usr/sbin/cron root 273
1 0 Sep 25 ? 000 /usr/lib/saf/sac -t
300 root 56 1 0 Sep 25 ?
000 /usr/lib/devfsadm/devfseventd root 58
1 0 Sep 25 ? 000
/usr/lib/devfsadm/devfsadmd root 106 1
0 Sep 25 ? 000 /usr/sbin/rpcbind
root 197 1 0 Sep 25 ? 001
/usr/sbin/nscd root 108 1 0 Sep 25 ?
000 /usr/sbin/keyserv root 168
1 0 Sep 25 ? 000 /usr/sbin/syslogd
root 118 1 0 Sep 25 ? 000
/usr/lib/netsvc/yp/ypbind root 159 1 0
Sep 25 ? 000 /usr/lib/autofs/automountd
34(No Transcript)
35(No Transcript)
36Memory Structure
heap
High
Arguments
String Growth
Return address
library
Prev. frame pointer
stack
Local variables
Stack Pointer
Stack Growth
Low
37Memory Structure
heap
High
foo
Arguments
String Growth
bar( ) foo( ) call bar( )
Return address
bar
Prev. frame pointer
Local variables
Stack Pointer
Stack Growth
Low
38Procedure Call? on the sameUser Stack
Per-process Kernel Stack
User-stack
Disk
Heap
Initialized data
Initialized data
text
text
a.out header
Memory
a.out magic number
39System Call? on a different stack
Per-process Kernel Stack
User-stack
Disk
Heap
Initialized data
Initialized data
text
text
a.out header
Memory
a.out magic number
40System Calls
- Not a normal procedure call
- It is a software trap into the kernel
- Hardware interrupt
- Hardware trap
- Software trap
41System Entry
- Hardware interrupt
- Asynchronous, might not relate to the context of
the executing process - Hardware trap
- Related to the current executing process, e.g.,
divided by zero - Software-initiated trap
- Instructions, int
42System Entry Vector
fork()
Trap
43System Entry Vector
fork()
Trap
Reserved for loadable system calls
XYZ()
44kldload
fork()
Trap
XYZ()
45Process
- Process a program in execution
- A process includes
- program counter
- stack
- data section
46Context Switching
47Scheduling Context Switching
48States of a Process
- Running, Blocked, and Ready
49(No Transcript)
501
RR
0
0
.
256 different priorities ?64 scheduling classes
063 bottom-half kernel (interrupt) 64127
top-half kernel 128159 real-time user 160223
timeshare 224255 idle
1
0
1
51Kernel Processes
- idle, swapper, vmdaemon, pagedaemon, pagezero,
bufdaemon, syncer, ktrace, vnlru, random,
g_event, g_up, g_down
/usr/src/sys/kern/kern_idle.c /usr/src/sys/kern/in
it_main.c /usr/src/sys/vm/vm_zeroidle.c /usr/src/s
ys/kern_ktrace.c /usr/src/sys/dev/random/randomsof
t_dev.c
52(No Transcript)
531
RR
0
0
.
256 different priorities ?64 scheduling classes
063 bottom-half kernel (interrupt) 64127
top-half kernel 128159 real-time user 160223
timeshare 224255 idle
1
0
1
54Running
Waiting
Ready
554.4BSD Process Structure(/usr/src/sys/sys/proc.h)
process group
session
process credential
user credential
VM space
region list
Process Structure
file descriptors
file entries
resource limits
statistics
user structure
signal actions
machine-dependent process information
process control block
process kernel stack
56FreeBSD User Structure
/ Per process structure containing data that
isnt needed in core when the process isnt
running (esp. when swapped out). This structure
may or may not be at the same kernel address
in all processes. / struct user struct pcb
u_pcb struct sigacts u_sigacts / p_sigacts
points here (use it!) / struct pstats u_stats
/ p_stats points here (use it!) /
/ Remaining fields only for core dump and/or
ptrace not valid at other times!
/ struct kinfo_proc u_kproc / proc eproc
/ struce md_coredump u_md / machine
dependent glop /
575.x Kernel
581
RR
0
KSE Kernel Scheduling Entity ? kernel-level
thread
0
.
256 different priorities ?64 scheduling classes
063 bottom-half kernel (interrupt) 64127
top-half kernel 128159 real-time user 160223
timeshare 224255 idle
1
0
1
59(No Transcript)
60What is a thread?
61Process and Thread(abstraction and abstraction)
- An execution instance of a program.
- Threads and resources
- a thread is a control entity of the logical flow
in the program. - A sequential program needs only one single thread
because it only need to be controlled by one
entity. - Can you distinguish a process and a thread?
- User mode versus (trap into the) Kernel mode.
62A Program and Threads
J0
(shared) variables
If (j0)
J100
63Threads
- Heavy-weight Process versus Light-weight Thread
- User-level versus Kernel-level
64a Process and a Thread
- A tradition process contains one thread (i.e, one
flow of control) and the resources (user or
kernel).
Resources
No obvious concurrency within a process
65Process and Threads
- A Process can contain more than one threads
sharing the resources (user or kernel).
Resources
66Threads
67Threads
- Blocking/Synchronous I/O
- One thread blocks all others???
- Block one ? block all
68CPU chip
register file
ALU
system bus
memory bus
main memory
I/O bridge
bus interface
I/O bus
Expansion slots for other devices such as network
adapters.
USB controller
disk controller
graphics adapter
mouse
keyboard
monitor
disk
69CPU chip
register file
CPU initiates a disk read by writing a command,
logical block number, and destination memory
address to a port (address) associated with disk
controller.
ALU
main memory
bus interface
I/O bus
USB controller
disk controller
graphics adapter
mouse
keyboard
monitor
disk
70CPU chip
register file
Disk controller reads the sector and performs a
direct memory access (DMA) transfer into main
memory.
ALU
main memory
bus interface
I/O bus
USB controller
disk controller
graphics adapter
mouse
keyboard
monitor
disk
71CPU chip
When the DMA transfer completes, the disk
controller notifies the CPU with an interrupt
(i.e., asserts a special interrupt pin on the
CPU)
register file
ALU
main memory
bus interface
I/O bus
USB controller
disk controller
graphics adapter
mouse
keyboard
monitor
disk
72Asynchronous I/O
- How to deal with multiple I/O operations
concurrently? - For example wait for a keyboard input, a
mouse click and input from a network connection. - Select system call
- Poll system call (same idea, different
implementation) - For more info see http//www.kegel.com/c10k.html
int select(int n, fd_set readfds, fd_set
writefds, fd_set exceptfds, struct
timeval timeout)
int poll(struct pollfd ufds, unsigned int nfds,
int timeout) struct pollfd int fd
/ file descriptor / short
events / requested events /
short revents / returned events /
73/usr/src/sys/kern/vfs_aio.c
Solaris, Linux 2.6, FreeBSD pp230231
- POSIX P1003.4 Asynchronous I/O interface
functions - aio_cancelcancel asynchronous read and/or write
requests - aio_errorretrieve Asynchronous I/O error status
- aio_fsyncasynchronously force I/O completion,
and sets errno to ENOSYS - aio_readbegin asynchronous read
- aio_returnretrieve return status of Asynchronous
I/O operation - aio_suspendsuspend until Asynchronous I/O
Completes - aio_writebegin asynchronous write
- lio_listioissue list of I/O requests
74Security Problem!!
75User-Level Threads
- Now, you should get the basic idea about how to
avoid block one ? block all.
76Threads
- User-level
- Kernel is unaware of multiple threading within
the same process. (Conceptually, the kernel
pretends one kernel thread per process.) - Kernel-level
- Kernel is fully aware of multiple kernel threads
within the same process, and therefore, it will
provide related kernel services.
77User and Kernel Threads
- One thread per process or multiple thread per
process
UTS
UserLevelTs
KernelTs
KTS
KTS
Which approach is better???
78User-Level Threads
- A small OS in the user-space to manage the
threads. - The kernel is totally unaware how many threads
the process currently has.
79(No Transcript)
80Why Multiple Threads??
81- Responsiveness
- Resource Sharing
- Economy
- Utilization of MP Architectures
82fork()
fork()
83fork()
execve()
Child
Global Variables
Code
Stack
84pthread_create()
Process A Thread 1
pthread_create()
Process A Thread 2
Stack
85Creation Time Difference
- Because threads are by definition lightweight,
they can be created more quickly that heavy
processes - Sun Ultra5, 320 Meg Ram, 1 CPU
- 94 forks()/second
- 1,737 threads/second (18x faster)
- Sun Sparc Ultra 1, 256 Meg Ram , 1 CPU
- 67 forks()/second
- 1,359 threads/second (20x faster)
- Sun Enterprise 420R, 5 Gig Ram, 4 CPUs
- 146 forks()/second
- 35,640 threads/second (244x faster)
- Linux 2.4 Kernel, .5 Gig Ram, 2 CPUs
- 1,811 forks()/second
- 227,611 threads/second (125x faster)
86(No Transcript)
87User Threads
- Thread management done by user-level threads
library - Examples
- - POSIX Pthreads
- - Mach C-threads
- - Solaris threads
88Kernel Threads
- Supported by the Kernel
- Examples
- - Windows 95/98/NT/2000
- - Solaris
- - Linux
89Solaris 2 Threads
90Linux Threads
- Linux refers to them as tasks rather than
threads. - Thread creation is done through clone() system
call. - clone() allows a child task to share the address
space of the parent task (process)
91(No Transcript)
92KT vs. UT
- pros and cons?
- BTW, how about FreeBSD?
93(No Transcript)
94UTS KTS
- Two independent schedulers
User Space
OS Kernel
95KTS
User Space
OS Kernel
96KT vs. UT
UTS
Kernel Interface
KTS
97Solaris 2 Threads
mapping but NOT coordinating
98Questions to ask
- Why do we need coordination?
- kernel-support user-level threads
- What do we need in this K/U coordination?
- extended system call API
- Is this only good for SMP?
- How about single processor?
- How about NPU? (e.g., IXP-2400)
99UTS
Library
Notify I/O events
Notify new decision
Kernel
KTS
100Is this a problem?
User Space
Kernel Space
Hardware
I dont know how many UTs you have up there? I
can guess but I am not sure that is exactly what
you want!
101Scheduler Activations
I dont know how many UTs you have up there?
102Scheduler Activations
- First proposed by Anderson et al. 91
- Idea cooperation between schedulers should take
place in both directions - User scheduler uses system calls
- Kernel scheduler should use upcalls!
- Upcalls
- Notify the user-level of kernel scheduling events
- Activations
- A new structure to support upcalls (kernel
thread) - As many running activations as processors
- Kernel controls activation creation and
destruction
103One Model (FreeBSD 5.x)
UTS - threads
Library
SA
SA
Notify I/O events
Notify new decision
SA
SA
Kernel
KTS virtual CPUs
104(No Transcript)
105I/O happens for Thread
User Program
(2)
(3)
(1)
User-Level Runtime System
(4)
(A)
(B)
Add Processor
Add Processor
Operating System Kernel
Processors
106As Thread has blocked on an I/O request
User Program
(3)
(2)
(1)
User-Level Runtime System
(4)
B
(A)
(B)
( C )
Operating System Kernel
As thread has blocked
Processors
107As Thread I/O completed
User Program
(2)
(4)
(3)
(1)
(1)
User-Level Runtime System
(A)
(B)
(D) As thread and Bs Thread can continue
( C )
Operating System Kernel
Processors
the upcall stack problem
108As Thread resumes on Scheduler Activation D
User Program
(2)
(4)
(3)
(1)
(1)
User-Level Runtime System
(D) As thread and Bs Thread can continue
( C )
Operating System Kernel
Processors
109(No Transcript)
110One Model (FreeBSD 5.x)
UTS - threads
Library
SA
SA
Notify I/O events
Notify new decision
SA
SA
Kernel
KTS virtual CPUs
111FreeBSD 5.x
- Kernel Scheduling Entity (KSE)
- a virtual CPU
- When anything changes regarding the service of
this KSE to the process, this KSE is unassigned
as the kernel doesnt know what other threads
might be there!! - Upcall to the UTS (via KSE mailbox).
- UTS uses both KSE mailbox and Thread mailbox to
handle/decide.
112(No Transcript)
113(No Transcript)
114include ltsys/types.hgt include ltsys/kse.hgt int
kse_create(struct kse_mailbox mbx,
int newsgroup) int kse_exit(void) int
kse_release(struct timespec timeout) int
kse_wakeup(struct kse_mailbox mbx) int
kse_thr_interrupt(struct
kse_thr_mailbox tmbx)
115struct kse_mailbox int km_version struct
kse_thr_mailbox km_curthread struct
kse_thr_mailbox km_completed sigset_t km_sigsc
aught unsigned int km_flags kse_func_t km_f
unc / UTS function / stack_t km_stack /
UTS context / void km_udata / For use by
the UTS / struct timespec km_timeofday / Time
of day / int km_quantum int km_spare8
116struct kse_thr_mailbox ucontext_t tm_context
/ User and machine context / unsigned
int tm_flags / Thread flags / struct
kse_thr_mailbox tm_next / Next thread in list
/ void tm_udata / For use by the UTS
/ unsigned int tm_uticks unsigned
int tm_sticks int tm_spare8
117upcalls
- ksec_new
- ksec_preempt
- ksec_block
- ksec_unblock
118UTS
Library
- ksec_new
- ksec_preempt
- ksec_block
- ksec_unblock
kse_create kse_exit kse_release kse_wakeup kse_thr
_interrupt
Kernel
KTS
119KSE Internal
120(No Transcript)
121(No Transcript)
122(No Transcript)
123(No Transcript)
124Linux VPI(Virtual Processor Interface)
- Experimental/Research Prototype
- Benson/Butner/Padden/Fedosov
- Scheduler activation in Linux Kernel 2.4.18
125(No Transcript)
126One Model (FreeBSD 5.x)
UTS - threads
Library
SA
SA
Notify I/O events
Notify new decision
SA
SA
Kernel
KTS virtual CPUs
127Kernel Processes(table 3.1 page 50)
- idle, swapper, vmdaemon, pagedaemon, pagezero,
bufdaemon, syncer, ktrace, vnlru, random,
g_event, g_up, g_down - Kernel processes execute code that is complied
into the kernels load image and operate with the
kernels privileged execution code.
128FreeBSD Kernel
129FreeBSD Kernel
130Kernel and User Space
Memory space for this process
Process FOO
program
System call (or trap into the kernel)
conceptually
Process FOO in the Kernel
System Call
Kernel Resources (disk or IO devices)
131What is micro-kernel?
132.what are the basic services?
OS
133An Alternative Micro-Kernel
Message Passing versus Optimized Procedure Calls
134(No Transcript)
135Micro versus Monolithic
- What is the real difference between these two
models?? - First Brainstorming!!
136Micro versus Monolithic
- Is this really relevant?
- Advantages of Micro Kernels
- Modules (Architectural Cleanness), Adaptive,
Small/Quick-to-Boot, - We did learn some lessons
- We have to consider the users applications,
and make a new engineering design decision.
137FreeBSD Kernel Size
- 689794 machine independent LOC
- 108346 machine dependent LOC
- 846525 device driver LOC
- Comparing
- Windows 3.1 6M LOC
- Windows 2000 30-50M LOC
- Windows XP 45M LOC
- Netscape 7M LOC
138OS Design
- Architectural Design
- how to organize the user and kernel resources?
- Module Control Design
- how to design a control mechanism to protect the
OS resource integrity? - Interface Design
- how to let user programs access the resources
easier? (e.g., system call interface,
multi-threaded interface).
139- What is Process?
- What is System Call?
- What is Kernel?