Transcript General

:
Operating System
ecs150 Fall 2007
#1: OS Architecture, Kernel, & Process
Dr. S. Felix Wu
Computer Science Department
University of California, Davis
http://www.cs.ucdavis.edu/~wu/
[email protected]
09/27/2007
ecs150 Fall 2007
1
VM/MVS, DOS, Win95/98/ME/2000/XP,
Freebsd/Linux, MacOS-10, Mach, Minix,
PalmOS, uCOS, TinyOS, …
09/27/2007
ecs150 Fall 2007
2
….where applications meet Hardware!!!
Applications……..
OS
Hardware: CPU/Memory/HD/DVD/Wireless…
09/27/2007
ecs150 Fall 2007
3
“Information Router”
One NIC a process’s user-level memory
 One file another file

– OS kernel layer
– Hardware layer
09/27/2007
ecs150 Fall 2007
4
….where applications meet Hardware!!!
Applications……..
OS
Hardware: CPU/Memory/HD/DVD/Wireless…
09/27/2007
ecs150 Fall 2007
5
This quarter….
The internals of OS
 The basic design principles of OS
 The skills to modify or implement an OS.

09/27/2007
ecs150 Fall 2007
6
Operating System

An interesting balance between:
– Theories and Practical Experiences/Experiments
– Architectural Concept and Detailed Design
– Formal Verification and Empirical Validation
09/27/2007
ecs150 Fall 2007
7
About the Instructor

S. Felix Wu
– [email protected]
– Facebook group (under UCD network): ecs150
Office: 3057 Engineering II
 Phone: 530-754-7070
 Office Hours:

– 1-2 p.m. on Tuesday and Friday
– by appointment
09/27/2007
ecs150 Fall 2007
8
09/27/2007
ecs150 Fall 2007
9
Why 3 email addresses?
– [email protected][email protected][email protected]
09/27/2007
ecs150 Fall 2007
10
Why 3 email addresses?
– [email protected][email protected]
– My main email contact for
everything all the time.
– [email protected]
09/27/2007
ecs150 Fall 2007
11
Why 3 email addresses?
– [email protected][email protected]
– My main email contact for
everything all the time.
– [email protected]
– Read only once in the past three
months…
09/27/2007
ecs150 Fall 2007
12
Why 3 email addresses?
– [email protected]
read/response during the quarters,
especially before the homework
deadlines.
– [email protected]
– My main email contact for everything
all the time.
– [email protected]
– Read only once in the past three
months…
09/27/2007
ecs150 Fall 2007
13
Anti-Spam
 [email protected]
  subject: [ecs150 f2007]…


[ecs150 2007]is the cyber social link
between the instructor and the students in
ecs150, fall 2007.
09/27/2007
ecs150 Fall 2007
14
Anti-Spam
 [email protected]
  subject: [ecs150 f2007]…
 [ecs150 f2007] is the cyber social link
between the instructor and the students in
ecs150, fall 2007.
 Let’s see by the end of quarter whether this
little secret will be known to the
spammers…

09/27/2007
ecs150 Fall 2007
15
About the TA

TA Sean Whalen ([email protected])
– Office Hours:
TBA
– Discussion:
Mon 4:10~5:00 (146 Olson)
09/27/2007
ecs150 Fall 2007
16
about Web site
http://www.cs.ucdavis.edu/~wu/ecs150/
 Also the Facebook group
 all lectures, notes, announcements,
homework assignments, tools, papers will
be there.

09/27/2007
ecs150 Fall 2007
17
Textbook
Reading this book
itself may be 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-Neil
Addison Wesley Professional, 2005, ISBN 0-201-70245-2.
http://www.freebsd.org/
09/27/2007
ecs150 Fall 2007
18
Prerequisites
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.

09/27/2007
ecs150 Fall 2007
19
Syllabus
Process/Kernel
(09)
 Memory Management
(06)
 Midterm (11/01/2007, in class)
 IO & File Systems
(10)
 Others
(03)
 Final (12/13/2007, 8~10 a.m.)

09/27/2007
ecs150 Fall 2007
20
OS 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?
09/27/2007
ecs150 Fall 2007
21
OS
Let’s 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.
09/27/2007
ecs150 Fall 2007
22
Principles 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.
09/27/2007
ecs150 Fall 2007
23
Course Requirements

48%: Programming Assignments
– teamwork: 1~2 students (no more than 2!)
– 4 Assignments (10%, 18%, 12%, 8%)
– HW#1 is out.
16%: In-class open-book midterm
 32%: open-book final
 04%: Participation of Lectures and
Discussion sessions.

– Deducted if missed more than TWO sessions.
09/27/2007
ecs150 Fall 2007
24
Grading
I will give +/- grades.
 possible grading :

–
–
–
–
09/27/2007
A: >= 92
B: >= 82
C: >= 72
D: >= 62
A-: >= 89
B-: >= 79
C-: >= 69
D-: >= 59
ecs150 Fall 2007
B+: >= 85
C+: >= 75
D+: >= 65
25
FreeBSD

You need to have access to a FreeBSD
environment
– I386, QEMU, VMware, VirtualPC, Parallel
09/27/2007
ecs150 Fall 2007
26
virtualization
Unmodified Applications
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
VirtualPC
Standard Full Virtualization  e.g., WindowXP
Hardware
09/27/2007
ecs150 Fall 2007
27
09/27/2007
ecs150 Fall 2007
28
FreeBSD
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
API
Unmodified Applications
Virtual PC or VMware
Hardware
09/27/2007
ecs150 Fall 2007
29
Programmable Virtualization
Unmodified Applications
“Programmable” Full Virtualization
DLVM
API
Unmodified OS (XP, Linux, Solaris, or, FreeBSD)
DLVM
Hardware
09/27/2007
ecs150 Fall 2007
30
The Structure of OS
The Kernel
 Processes and Threads
 The System Call Interface

09/27/2007
ecs150 Fall 2007
31
What is “kernel”?
09/27/2007
ecs150 Fall 2007
32
Kernel
The basic OS services
 Which services? What is it doing?
 Let’s check a couple examples

09/27/2007
ecs150 Fall 2007
33
….what are the basic services?
OS
09/27/2007
ecs150 Fall 2007
34
FreeBSD Kernel:
Services
Timer/clock, descriptor, process
 Memory Management: paging/swapping
 I/O control and terminal
 File System
 Inter-process communication
 Networking

09/27/2007
ecs150 Fall 2007
35
09/27/2007
ecs150 Fall 2007
36
Kernel of SVR2 of AT&T Unix
user
User programs
trap
Libraries
System Call Interface
File subsys
kernel
Buffer cache
Process
Control
Subsys.
Inter-Process
Communication
Scheduler
Character block
device drivers
Memory
Management
Hardware Control
hardware
09/27/2007
ecs150 Fall 2007
37
Kernel & Processes

The concept of “application process”
09/27/2007
ecs150 Fall 2007
38
Kernel and User Space
Process FOO
Memory
space for this
process
System call
(or trap into the kernel)
program
conceptually
Process FOO
in the Kernel
System Call
Kernel Resources
(disk or IO devices)
09/27/2007
ecs150 Fall 2007
39
Processes
> ps
PID TTY
TIME CMD
2910 pts/4
0:00 tcsh
> ps -ef
UID
PID PPID C
root
0
0 0
root
1
0 0
root
2
0 0
root
3
0 0
root
223
1 0
root
179
1 0
root
273
1 0
root
56
1 0
root
58
1 0
root
106
1 0
root
197
1 0
root
108
1 0
root
168
1 0
root
118
1 0
root
159
1 0
09/27/2007
STIME TTY
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
Sep 25 ?
TIME CMD
0:01 sched
0:00 /etc/init 0:00 pageout
0:01 fsflush
0:00 /usr/lib/utmpd
0:00 /usr/sbin/cron
0:00 /usr/lib/saf/sac -t 300
0:00 /usr/lib/devfsadm/devfseventd
0:00 /usr/lib/devfsadm/devfsadmd
0:00 /usr/sbin/rpcbind
0:01 /usr/sbin/nscd
0:00 /usr/sbin/keyserv
0:00 /usr/sbin/syslogd
0:00 /usr/lib/netsvc/yp/ypbind
0:00 /usr/lib/autofs/automountd
ecs150 Fall 2007
40
09/27/2007
ecs150 Fall 2007
41
09/27/2007
ecs150 Fall 2007
42
Memory Structure
High
Arguments
Return address
String
Growth
Prev. frame pointer
Local variables
Stack
Pointer
Low
09/27/2007
Stack
Growth
ecs150 Fall 2007
43
Memory Structure
High
foo
Arguments
bar
Return address
String
Growth
Prev. frame pointer
Local variables
Stack
Pointer
Low
09/27/2007
Stack
Growth
ecs150 Fall 2007
bar( )
{……}
foo( )
{ ……
call bar( );
……
}
44
Per-process
Kernel Stack
User-stack
Procedure Call
 on the same
User Stack
Disk
Heap
Initialized data
Initialized data
text
a.out header
text
Memory
a.out magic number
09/27/2007
ecs150 Fall 2007
45
Per-process
Kernel Stack
User-stack
System Call
 on a different stack
Disk
Heap
Initialized data
Initialized data
text
a.out header
text
Memory
a.out magic number
09/27/2007
ecs150 Fall 2007
46
System Calls

Not a “normal” procedure call

It is a software trap “into” the kernel
– Hardware interrupt
– Hardware trap
– Software trap
09/27/2007
ecs150 Fall 2007
47
System 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
09/27/2007
ecs150 Fall 2007
48
System Entry Vector
fork()
Trap
:
:
09/27/2007
ecs150 Fall 2007
49
System Entry Vector
fork()
Trap
Reserved for
loadable system calls
:
:
XYZ()
09/27/2007
ecs150 Fall 2007
50
kldload
fork()
Trap
XYZ()
:
:
09/27/2007
ecs150 Fall 2007
51
OS Architecture
09/27/2007
ecs150 Fall 2007
52
Process
Process – a program in execution
 A process includes:

– program counter
– stack
– data section
09/27/2007
ecs150 Fall 2007
53
Context Switching
09/27/2007
ecs150 Fall 2007
54
Scheduling &
Context Switching
Running
Blocked
09/27/2007
Running
Ready
Blocked
Running
Ready
Blocked
ecs150 Fall 2007
Running
Ready
Blocked
Ready
55
States of a Process

Running, Blocked, and Ready
Running
Waiting
09/27/2007
Ready
ecs150 Fall 2007
56
09/27/2007
ecs150 Fall 2007
57
1
RR
0
0
:
:
.
1
0
1
09/27/2007
256 different priorities
64 scheduling classes
0~63
64~127
128~159
160~223
224~255
ecs150 Fall 2007
bottom-half kernel (interrupt)
top-half kernel
real-time user
timeshare
idle
58
Kernel 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/init_main.c
/usr/src/sys/vm/vm_zeroidle.c
/usr/src/sys/kern_ktrace.c
/usr/src/sys/dev/random/randomsoft_dev.c
09/27/2007
ecs150 Fall 2007
59
09/27/2007
ecs150 Fall 2007
60
1
RR
0
0
:
:
.
1
0
1
09/27/2007
256 different priorities
64 scheduling classes
0~63
64~127
128~159
160~223
224~255
ecs150 Fall 2007
bottom-half kernel (interrupt)
top-half kernel
real-time user
timeshare
idle
61
Running
Waiting
09/27/2007
Ready
ecs150 Fall 2007
62
4.4BSD Process Structure
(/usr/src/sys/sys/proc.h)
Process
Structure
process group
session
process credential
user credential
VM space
region list
file descriptors
file entries
resource limits
statistics
signal actions
machine-dependent
process information
09/27/2007
process control block
process kernel stack
ecs150 Fall 2007
}
user structure
63
FreeBSD User Structure
/*
* Per process structure containing data that isn’t needed in core when the
* process isn’t 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 */
}
09/27/2007
ecs150 Fall 2007
64
5.x Kernel
09/27/2007
ecs150 Fall 2007
65
1
RR
0
KSE:
Kernel Scheduling Entity
 kernel-level thread
0
:
:
.
1
0
1
09/27/2007
256 different priorities
64 scheduling classes
0~63
64~127
128~159
160~223
224~255
ecs150 Fall 2007
bottom-half kernel (interrupt)
top-half kernel
real-time user
timeshare
idle
66
09/27/2007
ecs150 Fall 2007
67
What is a thread?
09/27/2007
ecs150 Fall 2007
68
Process 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.
09/27/2007
ecs150 Fall 2007
69
A Program and Threads
J=0;
(shared)
variables
If (j==0)
J=100
09/27/2007
ecs150 Fall 2007
70
Threads
Heavy-weight Process versus Light-weight
Thread
 User-level versus Kernel-level

09/27/2007
ecs150 Fall 2007
71
a 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
09/27/2007
ecs150 Fall 2007
72
Process and Threads

A Process can contain more than one
threads sharing the resources (user or
kernel).
Resources
09/27/2007
ecs150 Fall 2007
73
Threads
User-level
 Kernel-level

09/27/2007
ecs150 Fall 2007
74
Threads

Blocking/Synchronous I/O
– One thread blocks all others???
– “Block one  block all”
09/27/2007
ecs150 Fall 2007
75
CPU chip
register file
ALU
system bus
memory bus
main
memory
I/O
bridge
bus interface
I/O bus
USB
controller
mouse keyboard
09/27/2007
graphics
adapter
disk
controller
Expansion slots for
other devices such
as network adapters.
monitor
ecs150 Fall 2007
disk
76
CPU 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
mouse keyboard
09/27/2007
graphics
adapter
disk
controller
monitor
ecs150 Fall 2007
disk
77
CPU 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
mouse keyboard
09/27/2007
graphics
adapter
disk
controller
monitor
ecs150 Fall 2007
disk
78
CPU 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
mouse keyboard
09/27/2007
graphics
adapter
disk
controller
monitor
ecs150 Fall 2007
disk
79
Asynchronous 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
int select(int n, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);

Poll system call (same idea, different implementation)
int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
struct pollfd { int fd;
short events;
short revents;
};

/* file descriptor */
/* requested events */
/* returned events */
For more info see http://www.kegel.com/c10k.html
09/27/2007
ecs150 Fall 2007
80
/usr/src/sys/kern/vfs_aio.c
Solaris, Linux 2.6, FreeBSD pp230~231

POSIX P1003.4 Asynchronous I/O interface functions:
– aio_cancel:cancel asynchronous read and/or write
requests
– aio_error:retrieve Asynchronous I/O error status
– aio_fsync:asynchronously force I/O completion, and sets
errno to ENOSYS
– aio_read:begin asynchronous read
– aio_return:retrieve return status of Asynchronous I/O
operation
– aio_suspend:suspend until Asynchronous I/O Completes
– aio_write:begin asynchronous write
– lio_listio:issue list of I/O requests
09/27/2007
ecs150 Fall 2007
81
Security Problem!!
09/27/2007
ecs150 Fall 2007
82
User-Level Threads

Now, you should get the basic idea about
how to avoid “block one  block all”….
09/27/2007
ecs150 Fall 2007
83
Threads

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”.
09/27/2007
ecs150 Fall 2007
84
User and Kernel Threads

One thread per process or multiple thread per
process
UTS
UserLevelTs
KTS
09/27/2007
Which approach is better???
ecs150 Fall 2007
KernelTs
KTS
85
User-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.

09/27/2007
ecs150 Fall 2007
86
09/27/2007
ecs150 Fall 2007
87
Why Multiple Threads??
09/27/2007
ecs150 Fall 2007
88
Responsiveness
 Resource Sharing
 Economy
 Utilization of MP Architectures

09/27/2007
ecs150 Fall 2007
89
fork()
Process A
Global
Variables
fork()
Process B
Code
Global
Variables
Stack
Code
Stack
09/27/2007
ecs150 Fall 2007
90
Parent
fork()
execve()
Global
Variables
Child
Code
Stack
09/27/2007
Child
Global
Variables
Global
Variables
Code
Code
Stack
Stack
ecs150 Fall 2007
91
pthread_create()
Process A
Thread 1
Global
Variables
pthread_create()
Code
Process A
Thread 2
Stack
Stack
09/27/2007
ecs150 Fall 2007
92
Creation 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)
09/27/2007
ecs150 Fall 2007
93
09/27/2007
ecs150 Fall 2007
94
User Threads

Thread management done by user-level
threads library

Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
09/27/2007
ecs150 Fall 2007
95
Kernel Threads
Supported by the Kernel
 Examples
- Windows 95/98/NT/2000
- Solaris
- Linux

09/27/2007
ecs150 Fall 2007
96
Solaris 2 Threads
09/27/2007
ecs150 Fall 2007
97
Linux 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)

09/27/2007
ecs150 Fall 2007
98
Open new
connection
Application
(Web Server)
Thread class:
run
Programming Language
(Java)
Lib call:
pthread_create
Programming Library
(POSIX thread)
System call: Clone
09/27/2007
Operating System
(Linux Native Thread)
ecs150 Fall 2007
99
KT vs. UT

pros and cons?

BTW, how about FreeBSD?
09/27/2007
Threads
ecs150 Fall 2007
100
09/27/2007
ecs150 Fall 2007
101
UTS + KTS

Two independent schedulers:
User Space
Process
Process
Scheduler
Process
Scheduler
Scheduler
OS Kernel
processor
09/27/2007
processor
ecs150 Fall 2007
processor
102
KTS

One single scheduler:
User Space
Process
Process
Process
Scheduler
OS Kernel
processor
09/27/2007
processor
ecs150 Fall 2007
processor
103
KT vs. UT
UTS
Kernel Interface
KTS
09/27/2007
ecs150 Fall 2007
104
Solaris 2 Threads
mapping but NOT
coordinating
09/27/2007
ecs150 Fall 2007
105
Questions 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)
09/27/2007
ecs150 Fall 2007
106
UTS
Library
Notify
I/O
events
Notify
new
decision
Kernel
KTS
09/27/2007
ecs150 Fall 2007
107
Is this a problem?
User Space
Kernel Space
syscall
I/O request
interrupt
Hardware
I don’t know how many UT’s you
have up there?
I can guess but I am not sure that is
exactly what you want!
09/27/2007
ecs150 Fall 2007
108
Scheduler Activations
CPU time wasted
User Space
Kernel Space
syscall
I/O request
interrupt
I don’t know how many
UT’s you have up there?
Hardware
CPU used
User Space
Kernel Space
upcall
upcall
Hardware
09/27/2007
ecs150 Fall 2007
109
Scheduler 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
09/27/2007
ecs150 Fall 2007
110
One Model (FreeBSD 5.x)
UTS - threads
Library
SA SA
Notify
I/O
events
Notify
new
decision
SA SA
KTS – virtual CPU’s
09/27/2007
ecs150 Fall 2007
Kernel
111
09/27/2007
ecs150 Fall 2007
112
I/O happens for Thread
User Program
(2)
User-Level
Runtime System
(3)
(1)
(A)
(4)
(B)
Add
Processor
Add
Processor
Operating System Kernel
Processors
09/27/2007
ecs150 Fall 2007
113
A’s Thread has blocked on an I/O request
User Program
(2)
User-Level
Runtime System
(3)
(1)
(4)
B
(A)
(B)
Operating
System Kernel
(C)
A’s thread has blocked
Processors
09/27/2007
ecs150 Fall 2007
114
A’s Thread I/O completed
User Program
(2)
(3)
User-Level
Runtime System
(1)
(1)
(A)
(B)
Operating
System Kernel
(C)
(4)
(D)
A’s thread and B’s
Thread can continue
Processors
“the upcall stack problem”
09/27/2007
ecs150 Fall 2007
115
A’s Thread resumes on Scheduler Activation D
User Program
(2)
(3)
(1)
User-Level
Runtime System
(C)
Operating
System Kernel
(1)
(4)
(D)
A’s thread and B’s
Thread can continue
Processors
09/27/2007
ecs150 Fall 2007
116
09/27/2007
ecs150 Fall 2007
117
One Model (FreeBSD 5.x)
UTS - threads
Library
SA SA
Notify
I/O
events
Notify
new
decision
SA SA
KTS – virtual CPU’s
09/27/2007
ecs150 Fall 2007
Kernel
118
FreeBSD 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 doesn’t 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.
09/27/2007
ecs150 Fall 2007
119
09/27/2007
ecs150 Fall 2007
120
09/27/2007
ecs150 Fall 2007
121
#include <sys/types.h>
#include <sys/kse.h>
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);
09/27/2007
ecs150 Fall 2007
122
struct kse_mailbox {
int
km_version;
struct kse_thr_mailbox
*km_curthread;
struct kse_thr_mailbox
*km_completed;
sigset_t
km_sigscaught;
unsigned int
km_flags;
kse_func_t
*km_func;
/* 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_spare[8];
};
09/27/2007
ecs150 Fall 2007
123
struct kse_thr_mailbox {
ucontext_t
unsigned int
struct kse_thr_mailbox
void
unsigned int
unsigned int
int
};
09/27/2007
tm_context;
tm_flags;
*tm_next;
*tm_udata;
tm_uticks;
tm_sticks;
tm_spare[8];
ecs150 Fall 2007
/*
/*
/*
/*
User and machine context */
Thread flags */
Next thread in list */
For use by the UTS */
124
upcalls
ksec_new
 ksec_preempt
 ksec_block
 ksec_unblock

09/27/2007
ecs150 Fall 2007
125
UTS
Library
ksec_new
ksec_preempt
ksec_block
ksec_unblock
kse_create
kse_exit
kse_release
kse_wakeup
kse_thr_interrupt
Kernel
KTS
09/27/2007
ecs150 Fall 2007
126
KSE Internal
KSE
 KSEG
 KSEC

09/27/2007
ecs150 Fall 2007
127
09/27/2007
ecs150 Fall 2007
128
09/27/2007
ecs150 Fall 2007
129
09/27/2007
ecs150 Fall 2007
130
09/27/2007
ecs150 Fall 2007
131
Linux VPI
(Virtual Processor Interface)

Experimental/Research Prototype
– Benson/Butner/Padden/Fedosov
– Scheduler activation in Linux Kernel 2.4.18
09/27/2007
ecs150 Fall 2007
132
09/27/2007
ecs150 Fall 2007
133
One Model (FreeBSD 5.x)
UTS - threads
Library
SA SA
Notify
I/O
events
Notify
new
decision
SA SA
KTS – virtual CPU’s
09/27/2007
ecs150 Fall 2007
Kernel
134
Kernel 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 kernel’s load image and
operate with the kernel’s privileged
execution code.”
09/27/2007
ecs150 Fall 2007
135
FreeBSD Kernel
09/27/2007
ecs150 Fall 2007
136
FreeBSD Kernel
09/27/2007
ecs150 Fall 2007
137
Kernel and User Space
Process FOO
Memory
space for this
process
System call
(or trap into the kernel)
program
conceptually
Process FOO
in the Kernel
System Call
Kernel Resources
(disk or IO devices)
09/27/2007
ecs150 Fall 2007
138
What is “micro-kernel”?
09/27/2007
ecs150 Fall 2007
139
….what are the basic services?
OS
09/27/2007
ecs150 Fall 2007
140
An Alternative: Micro-Kernel
Message Passing versus Optimized Procedure Calls
09/27/2007
ecs150 Fall 2007
141
09/27/2007
ecs150 Fall 2007
142
System calls




int 80h
*.s (not in /usr/src/sys/kern/*)
*.c (not in /usr/src/sys/kern/*)
sysent/sysvec function pointer 
lkmnosys()
09/27/2007
ecs150 Fall 2007
143
/usr/src/sys/i386/i386/*.*
09/27/2007
ecs150 Fall 2007
144
Micro versus Monolithic
What is the real difference between these
two models??
 First Brainstorming!!

09/27/2007
ecs150 Fall 2007
145
Micro 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.
09/27/2007
ecs150 Fall 2007
146
FreeBSD 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
09/27/2007
ecs150 Fall 2007
147
OS 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).
09/27/2007
ecs150 Fall 2007
148
What is “Process”?
 What is “System Call”?
 What is “Kernel”?

09/27/2007
ecs150 Fall 2007
149