149
09/27/2007 ecs150 Fall 2007 1 ecs150 Fall 2007: Operating System Operating System #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/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 1

ecs150 Fall 2007:Operating SystemOperating System#1: OS Architecture, Kernel, & Process

Dr. S. Felix Wu

Computer Science Department

University of California, Davishttp://www.cs.ucdavis.edu/~wu/

[email protected]

Page 2: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 2

VM/MVS, DOS, Win95/98/ME/2000/XP, Freebsd/Linux, MacOS-10, Mach, Minix, PalmOS, uCOS, TinyOS, …

Page 3: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 3

Applications……..

Hardware: CPU/Memory/HD/DVD/Wireless…

OS

….where applications meet Hardware!!!

Page 4: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 4

““Information Router”Information Router”

One NIC a process’s user-level memory One file another file

– OS kernel layer– Hardware layer

Page 5: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 5

Applications……..

Hardware: CPU/Memory/HD/DVD/Wireless…

OS

….where applications meet Hardware!!!

Page 6: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 6

This quarter….This quarter….

The internals of OS The basic design principles of OS The skills to modify or implement an OS.

Page 7: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 7

Operating SystemOperating System

An interesting balance between:– Theories and Practical Experiences/Experiments– Architectural Concept and Detailed Design– Formal Verification and Empirical Validation

Page 8: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 8

About the InstructorAbout 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

Page 9: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 9

Page 11: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 11

Why 3 email addresses?Why 3 email addresses?

[email protected]

[email protected]– My main email contact for everything all the time.

[email protected]

Page 12: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 12

Why 3 email addresses?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…

Page 13: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 13

Why 3 email addresses?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…

Page 14: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 14

Anti-SpamAnti-Spam

[email protected] subject: [ecs150 f2007]…

[ecs150 2007]is the cyber social link between the instructor and the students in ecs150, fall 2007.

Page 15: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 15

Anti-SpamAnti-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…

Page 16: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 16

About the TAAbout the TA

TA Sean Whalen ([email protected])– Office Hours: TBA– Discussion: Mon 4:10~5:00 (146 Olson)

Page 17: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 17

about Web siteabout Web site

http://www.cs.ucdavis.edu/~wu/ecs150/ Also the Facebook group all lectures, notes, announcements,

homework assignments, tools, papers will be there.

Page 18: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 18

TextbookTextbook

http://www.freebsd.org/

"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.

Reading this book itself may be a major challenge.But, you really learn when you go through this process!

Page 19: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 19

PrerequisitesPrerequisites

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.

Page 20: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 20

SyllabusSyllabus

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.)

Page 21: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 21

OS Principles/ConceptsOS 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?

Page 22: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 22

OSOS

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.

Page 23: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 23

Principles vs. PracticePrinciples 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.

Page 24: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 24

Course RequirementsCourse 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.

Page 25: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 25

GradingGrading

I will give +/- grades. possible grading :

– A: >= 92 A-: >= 89 B+: >= 85– B: >= 82 B-: >= 79 C+: >= 75– C: >= 72 C-: >= 69 D+: >= 65– D: >= 62 D-: >= 59

Page 26: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 26

FreeBSDFreeBSD

You need to have access to a FreeBSD environment– I386, QEMU, VMware, VirtualPC, Parallel

Page 27: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 27

Standard Full Virtualization e.g.,

Unmodified OS (XP, Linux, Solaris, or, FreeBSD)

Unmodified Applications

Hardware

VirtualPCWindowXP

virtualizationvirtualization

Page 28: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 28

Page 29: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 29

Virtual PC or VMware

Unmodified OS (XP, Linux, Solaris, or, FreeBSD)

Unmodified Applications

Hardware

AP

I

FreeBSDFreeBSD

Page 30: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 30

“Programmable” Full Virtualization

Unmodified OS (XP, Linux, Solaris, or, FreeBSD)

Unmodified Applications

Hardware

AP

I

DLVM

Programmable VirtualizationProgrammable Virtualization

DLVM

Page 31: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 31

The Structure of OSThe Structure of OS

The Kernel Processes and Threads The System Call Interface

Page 32: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 32

What is “kernel”?What is “kernel”?

Page 33: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 33

KernelKernel

The basic OS services Which services? What is it doing? Let’s check a couple examples

Page 34: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 34

OS

….what are the basic services?

Page 35: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 35

FreeBSD Kernel: FreeBSD Kernel: ServicesServices

Timer/clock, descriptor, process Memory Management: paging/swapping I/O control and terminal File System Inter-process communication Networking

Page 36: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 36

Page 37: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 37

Kernel of SVR2 of AT&T UnixKernel of SVR2 of AT&T Unix

hardware

System Call Interface

LibrariesUser programs

trapuser

File subsys

Buffer cache

Hardware Control

Character blockdevice drivers

ProcessControlSubsys.

Inter-ProcessCommunication

Scheduler

MemoryManagement

kernel

Page 38: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 38

Kernel & ProcessesKernel & Processes

The concept of “application process”

Page 39: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 39

Kernel and User SpaceKernel and User Space

Process FOOFOOMemoryspace for thisprocess

System call(or trap into the kernel)

program

System Call

conceptually

Kernel Resources(disk or IO devices)

Process FOOFOOin the Kernel

Page 40: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 40

ProcessesProcesses

> ps PID TTY TIME CMD 2910 pts/4 0:00 tcsh> ps -ef UID PID PPID C STIME TTY TIME CMD root 0 0 0 Sep 25 ? 0:01 sched root 1 0 0 Sep 25 ? 0:00 /etc/init - root 2 0 0 Sep 25 ? 0:00 pageout root 3 0 0 Sep 25 ? 0:01 fsflush root 223 1 0 Sep 25 ? 0:00 /usr/lib/utmpd root 179 1 0 Sep 25 ? 0:00 /usr/sbin/cron root 273 1 0 Sep 25 ? 0:00 /usr/lib/saf/sac -t 300 root 56 1 0 Sep 25 ? 0:00 /usr/lib/devfsadm/devfseventd root 58 1 0 Sep 25 ? 0:00 /usr/lib/devfsadm/devfsadmd root 106 1 0 Sep 25 ? 0:00 /usr/sbin/rpcbind root 197 1 0 Sep 25 ? 0:01 /usr/sbin/nscd root 108 1 0 Sep 25 ? 0:00 /usr/sbin/keyserv root 168 1 0 Sep 25 ? 0:00 /usr/sbin/syslogd root 118 1 0 Sep 25 ? 0:00 /usr/lib/netsvc/yp/ypbind root 159 1 0 Sep 25 ? 0:00 /usr/lib/autofs/automountd

Page 41: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 41

Page 42: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 42

Page 43: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 43

Memory StructureMemory StructureHigh

LowStack Growth

String Growth

Arguments

Return address

Prev. frame pointer

Local variables

Stack Pointer

Page 44: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 44

Memory StructureMemory StructureHigh

LowStack Growth

String Growth

Arguments

Return address

Prev. frame pointer

Local variables

Stack Pointer

bar( ){……}

foo( ){ …… call bar( ); ……}

foo

bar

Page 45: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 45

Procedure CallProcedure Call on the same on the same

User StackUser Stack

Per-processKernel Stack

User-stack

Heap

Initialized data Initialized data

text text

a.out header

a.out magic numberMemory

Disk

Page 46: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 46

System CallSystem Call on a different stack on a different stack

Per-processKernel Stack

User-stack

Heap

Initialized data Initialized data

text text

a.out header

a.out magic numberMemory

Disk

Page 47: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 47

System CallsSystem Calls

Not a “normal” procedure call

It is a software trap “into” the kernel– Hardware interrupt– Hardware trap– Software trap

Page 48: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 48

System EntrySystem 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

Page 49: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 49

System Entry VectorSystem Entry Vector

fork()

::

Trap

Page 50: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 50

System Entry VectorSystem Entry Vector

fork()

::

TrapReserved forloadable system calls

XYZ()

Page 51: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 51

kldloadkldload

fork()

::

Trap

XYZ()

Page 52: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 52

OS ArchitectureOS Architecture

Page 53: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 53

ProcessProcess

Process – a program in execution A process includes:

– program counter – stack– data section

Page 54: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 54

Context SwitchingContext Switching

Page 55: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 55

Running

Blocked Ready

Running

Blocked Ready

Running

Blocked Ready

Running

Blocked Ready

Scheduling &Context Switching

Page 56: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 56

States of a ProcessStates of a Process

Running, Blocked, and Ready

Running

Waiting Ready

Page 57: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 57

Page 58: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 58

1

0

0

1

0

1

::.

256 different priorities64 scheduling classes

RR

0~63 bottom-half kernel (interrupt)64~127 top-half kernel128~159 real-time user160~223 timeshare224~255 idle

Page 59: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 59

Kernel ProcessesKernel 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

Page 60: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 60

Page 61: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 61

1

0

0

1

0

1

::.

256 different priorities64 scheduling classes

RR

0~63 bottom-half kernel (interrupt)64~127 top-half kernel128~159 real-time user160~223 timeshare224~255 idle

Page 62: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 62

Running

Waiting Ready

Page 63: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 63

4.4BSD Process Structure4.4BSD Process Structure(/usr/src/sys/sys/proc.h)(/usr/src/sys/sys/proc.h)

ProcessStructure

machine-dependentprocess information

process group

process credential

VM space

file descriptors

resource limits

statistics

signal actions

process control blockprocess kernel stack

session

user credential

region list

file entries

} user structure

Page 64: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 64

FreeBSD User StructureFreeBSD 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 */

}

Page 65: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 65

5.x Kernel

Page 66: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 66

1

0

0

1

0

1

::.

256 different priorities64 scheduling classes

RR

0~63 bottom-half kernel (interrupt)64~127 top-half kernel128~159 real-time user160~223 timeshare224~255 idle

KSE:Kernel Scheduling Entity kernel-level thread

Page 67: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 67

Page 68: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 68

What is a thread?What is a thread?

Page 69: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 69

Process and ThreadProcess 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.

Page 70: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 70

A Program and ThreadsA Program and Threads

(shared)variables

J=0;

If (j==0)

J=100

Page 71: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 71

ThreadsThreads

Heavy-weight Process versus Light-weight Thread

User-level versus Kernel-level

Page 72: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 72

a Process and a Threada 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

Page 73: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 73

Process and ThreadsProcess and Threads

A Process can contain more than one threads sharing the resources (user or kernel).

Resources

Page 74: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 74

ThreadsThreads

User-level Kernel-level

Page 75: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 75

ThreadsThreads

Blocking/Synchronous I/O– One thread blocks all others???– “Block one block all”

Page 76: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 76

mainmemory

I/O bridge

bus interface

ALU

register file

CPU chip

system bus memory bus

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus Expansion slots forother devices suchas network adapters.

Page 77: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 77

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller.

Page 78: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 78

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory.

Page 79: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 79

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU)

Page 80: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 80

Asynchronous I/OAsynchronous 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 */ };

Page 81: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 81

/usr/src/sys/kern/vfs_aio.c/usr/src/sys/kern/vfs_aio.c

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

Solaris, Linux 2.6, FreeBSD pp230~231

Page 82: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 82

Security Problem!!

Page 83: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 83

User-Level ThreadsUser-Level Threads

Now, you should get the basic idea about how to avoid “block one block all”….

Page 84: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 84

ThreadsThreads

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”.

Page 85: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 85

User and Kernel ThreadsUser and Kernel Threads One thread per process or multiple thread per

process

KernelTsUserLevelTs

Which approach is better???

UTS

KTS KTS

Page 86: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 86

User-Level ThreadsUser-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.

Page 87: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 87

Page 88: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 88

Why Multiple Threads??Why Multiple Threads??

Page 89: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 89

Responsiveness Resource Sharing Economy Utilization of MP Architectures

Page 90: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 90

fork()

fork()fork()Process A

GlobalVariables

Code

Stack

Process B

GlobalVariables

Code

Stack

Page 91: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 91

fork()Parent

GlobalVariables

Code

Stack

Child

GlobalVariables

Code

Stack

Child

GlobalVariables

Code

Stack

execve()

Page 92: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 92

pthread_create()pthread_create()Process AThread 1

GlobalVariables

Code

Stack

Process AThread 2

Stack

pthread_create()

Page 93: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 93

Creation Time DifferenceCreation 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)

Page 94: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 94

Page 95: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 95

User ThreadsUser Threads

Thread management done by user-level threads library

Examples

- POSIX Pthreads

- Mach C-threads

- Solaris threads

Page 96: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 96

Kernel ThreadsKernel Threads Supported by the Kernel Examples

- Windows 95/98/NT/2000

- Solaris

- Linux

Page 97: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 97

Solaris 2 ThreadsSolaris 2 Threads

Page 98: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 98

Linux ThreadsLinux 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)

Page 99: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 99

Operating System(Linux Native Thread)

Programming Library(POSIX thread)

Programming Language(Java)

Application(Web Server)

System call: Clone

Thread class: run

Lib call:

pthread_create

Open new connection

Page 100: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 100

KT vs. UTKT vs. UT

pros and cons?

BTW, how about FreeBSD?

Threads

Page 101: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 101

Page 102: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 102

UTS + KTSUTS + KTS Two independent schedulers:

processor processor processor

OS Kernel

Process Process Process

Scheduler

User Space

Scheduler Scheduler

Page 103: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 103

KTSKTS One single scheduler:

processor processor processor

OS Kernel

Process Process Process

Scheduler

User Space

Page 104: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 104

KT vs. UTKT vs. UT

Kernel Interface

UTS

KTS

Page 105: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 105

Solaris 2 ThreadsSolaris 2 Threads

mapping but NOT coordinating

Page 106: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 106

Questions to askQuestions 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)

Page 107: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 107

Kernel

Library

KTS

Notify I/O

events

UTS

Notify new

decision

Page 108: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 108

Kernel Space

User Space

Hardware

syscall

I/O request interrupt

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!

Is this a problem?

Page 109: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 109

Scheduler ActivationsScheduler Activations

Kernel Space

User Space

Hardware

upcall upcall

Kernel Space

User Space

Hardware

syscall

I/O request interrupt

CPU time wasted

CPU used

I don’t know how many UT’s you have up there?

Page 110: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 110

Scheduler ActivationsScheduler 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

Page 111: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 111

Kernel

Library

KTS – virtual CPU’s

Notify I/O

events

UTS - threads

Notify new

decision

SA SA

SA SA

One Model (FreeBSD 5.x)

Page 112: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 112

Page 113: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 113

I/O happens for ThreadI/O happens for Thread

(4)(3)(2)

(1)

User Program

User-LevelRuntime System

Operating System Kernel

Processors

AddProcessor

AddProcessor

(A) (B)

Page 114: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 114

A’s Thread has blocked on an I/O requestA’s Thread has blocked on an I/O request

(4)

(3)(2)(1)

User Program

User-LevelRuntime System

Processors

B

(A) (B) ( C )

A’s thread has blockedOperating System Kernel

Page 115: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 115

(4)(3)(2)

(1)

User Program

User-LevelRuntime System

Processors

(A) (B) ( C )Operating System Kernel

(1)

(D) A’s thread and B’s Thread can continue

A’s Thread I/O completedA’s Thread I/O completed

“the upcall stack problem”

Page 116: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 116

A’s Thread resumes on Scheduler Activation D

(4)(3)(2)

User Program

User-LevelRuntime System

Processors

( C )Operating System Kernel

(1)

(D) A’s thread and B’s Thread can continue

(1)

Page 117: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 117

Page 118: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 118

Kernel

Library

KTS – virtual CPU’s

Notify I/O

events

UTS - threads

Notify new

decision

SA SA

SA SA

One Model (FreeBSD 5.x)

Page 119: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 119

FreeBSD 5.xFreeBSD 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.

Page 120: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 120

Page 121: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 121

Page 122: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 122

#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);

Page 123: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 123

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];

};

Page 124: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 124

struct 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_spare[8];

};

Page 125: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 125

upcallsupcalls

ksec_new ksec_preempt ksec_block ksec_unblock

Page 126: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 126

Kernel

Library

KTS

UTS

ksec_new

ksec_preempt

ksec_block

ksec_unblock

kse_createkse_exitkse_releasekse_wakeupkse_thr_interrupt

Page 127: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 127

KSE InternalKSE Internal

KSE KSEG KSEC

Page 128: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 128

Page 129: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 129

Page 130: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 130

Page 131: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 131

Page 132: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 132

Linux VPILinux VPI(Virtual Processor Interface)

Experimental/Research Prototype– Benson/Butner/Padden/Fedosov– Scheduler activation in Linux Kernel 2.4.18

Page 133: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 133

Page 134: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 134

Kernel

Library

KTS – virtual CPU’s

Notify I/O

events

UTS - threads

Notify new

decision

SA SA

SA SA

One Model (FreeBSD 5.x)

Page 135: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 135

Kernel ProcessesKernel Processes((table 3.1 page 50)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.”

Page 136: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 136

FreeBSD KernelFreeBSD Kernel

Page 137: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 137

FreeBSD KernelFreeBSD Kernel

Page 138: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 138

Kernel and User SpaceKernel and User Space

Process FOOFOOMemoryspace for thisprocess

System call(or trap into the kernel)

program

System Call

conceptually

Kernel Resources(disk or IO devices)

Process FOOFOOin the Kernel

Page 139: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 139

What is “micro-kernel”?What is “micro-kernel”?

Page 140: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 140

OS

….what are the basic services?

Page 141: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 141

An Alternative: Micro-Kernel

Message Passing versus Optimized Procedure Calls

Page 142: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 142

Page 143: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 143

System callsSystem calls

int 80h *.s (not in /usr/src/sys/kern/*) *.c (not in /usr/src/sys/kern/*) sysent/sysvec function pointer lkmnosys()

Page 144: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 144

/usr/src/sys/i386/i386/*.*

Page 145: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 145

Micro versus MonolithicMicro versus Monolithic

What is the real difference between these two models??

First Brainstorming!!

Page 146: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 146

Micro versus MonolithicMicro 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.

Page 147: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 147

FreeBSD Kernel: SizeFreeBSD 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

Page 148: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 148

OS DesignOS 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).

Page 149: 09/27/2007ecs150 Fall 20071 Operating System ecs150 Fall 2007 : Operating System #1: OS Architecture, Kernel, & Process Dr. S. Felix Wu Computer Science

09/27/2007 ecs150 Fall 2007 149

What is “Process”? What is “System Call”? What is “Kernel”?