Ecs150 spring 2006 operating system 1 os architecture kernel process
Download
1 / 139

ecs150 Spring 2006 : Operating System #1: OS Architecture, Kernel, & Process - PowerPoint PPT Presentation


  • 123 Views
  • Uploaded on

ecs150 Spring 2006 : 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]

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' ecs150 Spring 2006 : Operating System #1: OS Architecture, Kernel, & Process' - rowa


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Ecs150 spring 2006 operating system 1 os architecture kernel process

ecs150 Spring 2006: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]

ecs150 Spring 2006


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

ecs150 Spring 2006


….where applications meet Hardware!!! Mach, Minix, PalmOS, uCOS, TinyOS, …

Applications……..

OS

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

ecs150 Spring 2006


Information router
“Information Router” Mach, Minix, PalmOS, uCOS, TinyOS, …

  • One NIC a process’s user-level memory

  • One file another file

    • OS kernel layer

    • Hardware layer

ecs150 Spring 2006


….where applications meet Hardware!!! Mach, Minix, PalmOS, uCOS, TinyOS, …

Applications……..

OS

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

ecs150 Spring 2006


Virtualization
virtualization Mach, Minix, PalmOS, uCOS, TinyOS, …

Unmodified Applications

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

Standard Full Virtualization  e.g.,

VirtualPC

WindowXP

Hardware

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


Programmable Virtualization Mach, Minix, PalmOS, uCOS, TinyOS, …

Unmodified Applications

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

“Programmable” Full Virtualization

API

DLVM

DLVM

Hardware

ecs150 Spring 2006


FreeBSD Mach, Minix, PalmOS, uCOS, TinyOS, …

Unmodified Applications

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

API

Virtual PC or VMware

Hardware

ecs150 Spring 2006


This quarter
This quarter…. Mach, Minix, PalmOS, uCOS, TinyOS, …

  • The internals of OS

  • The basic design principles of OS

  • The skills to modify or implement an OS.

ecs150 Spring 2006


Operating system
Operating System Mach, Minix, PalmOS, uCOS, TinyOS, …

  • An interesting balance between:

    • Theories and Practical Experiences/Experiments

    • Architectural Concept and Detailed Design

    • Formal Verification and Empirical Validation

ecs150 Spring 2006


About the instructor
About the Instructor Mach, Minix, PalmOS, uCOS, TinyOS, …

  • S. Felix Wu

    • [email protected], x4-7070

  • Office: 3057 Engineering II

  • Office Hours:

    • 1-2 p.m. on Tuesday and Friday

    • by appointment

ecs150 Spring 2006


About the ta
About the TA Mach, Minix, PalmOS, uCOS, TinyOS, …

  • TA Valerie Szudziejka ([email protected])

    • Office Hours: TBA

    • Discussion:

ecs150 Spring 2006


About web site
about Web site Mach, Minix, PalmOS, uCOS, TinyOS, …

  • http://www.cs.ucdavis.edu/~wu/ecs150/

  • all lectures, notes, announcements, homework assignments, tools, papers will be there.

ecs150 Spring 2006


Textbook
Textbook Mach, Minix, PalmOS, uCOS, TinyOS, …

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/

ecs150 Spring 2006


Prerequisites
Prerequisites Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Syllabus
Syllabus Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Process/Kernel (09)

  • Memory Management (06)

  • midterm

  • IO & File Systems (10)

  • Others (03)

  • final

ecs150 Spring 2006


Os principles concepts
OS Principles/Concepts Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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?

ecs150 Spring 2006


OS Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Principles vs practice
Principles vs. Practice Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Course requirements
Course Requirements Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 48%: Programming Assignments

    • teamwork: 1~3 students

    • 3 Programming Assignments (12%, 18%, 18%)

    • HW#1 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.

ecs150 Spring 2006


Grading
Grading Mach, Minix, PalmOS, uCOS, TinyOS, …

  • I will give +/- grades.

  • possible grading (not finalized):

    • A: >= 93 A-: >= 90 B+: >= 87

    • B: >= 84 B-: >= 81 C+: >= 78

    • C: >= 75 C-: >= 72 D : > 60

ecs150 Spring 2006


Freebsd
FreeBSD Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Your team need to have access to a FreeBSD environment

    • I386, VMware, VirtualPC

ecs150 Spring 2006


The structure of os
The Structure of OS Mach, Minix, PalmOS, uCOS, TinyOS, …

  • The Kernel

  • Processes and Threads

  • The System Call Interface

ecs150 Spring 2006


What is kernel
What is “kernel”? Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


Kernel
Kernel Mach, Minix, PalmOS, uCOS, TinyOS, …

  • The basic OS services

  • Which services? What is it doing?

  • Let’s check a couple examples

ecs150 Spring 2006


….what are the basic services? Mach, Minix, PalmOS, uCOS, TinyOS, …

OS

ecs150 Spring 2006


Freebsd kernel services
FreeBSD Kernel: Services Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Timer/clock, descriptor, process

  • Memory Management: paging/swapping

  • I/O control and terminal

  • File System

  • Inter-process communication

  • Networking

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


Kernel of svr2 of at t unix
Kernel of SVR2 of AT&T Unix Mach, Minix, PalmOS, uCOS, TinyOS, …

User programs

Libraries

user

trap

System Call Interface

File subsys

Process

Control

Subsys.

Inter-Process

Communication

Buffer cache

kernel

Scheduler

Character block

device drivers

Memory

Management

Hardware Control

hardware

ecs150 Spring 2006


Kernel processes
Kernel & Processes Mach, Minix, PalmOS, uCOS, TinyOS, …

  • The concept of “application process”

ecs150 Spring 2006


Kernel and user space
Kernel and User Space Mach, Minix, PalmOS, uCOS, TinyOS, …

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)

ecs150 Spring 2006


Processes
Processes Mach, Minix, PalmOS, uCOS, TinyOS, …

> 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

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


Memory structure
Memory Structure Mach, Minix, PalmOS, uCOS, TinyOS, …

heap

High

Arguments

String Growth

Return address

library

Prev. frame pointer

stack

Local variables

Stack Pointer

Stack Growth

Low

ecs150 Spring 2006


Memory structure1
Memory Structure Mach, Minix, PalmOS, uCOS, TinyOS, …

heap

High

foo

Arguments

String Growth

bar( )

{……}

foo( )

{ ……

call bar( );

……

}

Return address

bar

Prev. frame pointer

Local variables

Stack Pointer

Stack Growth

Low

ecs150 Spring 2006


Procedure call on the same user stack
Procedure Call Mach, Minix, PalmOS, uCOS, TinyOS, … 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

ecs150 Spring 2006


System call on a different stack
System Call Mach, Minix, PalmOS, uCOS, TinyOS, … 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

ecs150 Spring 2006


System calls
System Calls Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Not a “normal” procedure call

  • It is a software trap “into” the kernel

    • Hardware interrupt

    • Hardware trap

    • Software trap

ecs150 Spring 2006


System entry
System Entry Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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

ecs150 Spring 2006


System entry vector
System Entry Vector Mach, Minix, PalmOS, uCOS, TinyOS, …

fork()

Trap

:

:

ecs150 Spring 2006


System entry vector1
System Entry Vector Mach, Minix, PalmOS, uCOS, TinyOS, …

fork()

Trap

Reserved for

loadable system calls

:

:

XYZ()

ecs150 Spring 2006


Kldload
kldload Mach, Minix, PalmOS, uCOS, TinyOS, …

fork()

Trap

XYZ()

:

:

ecs150 Spring 2006


Process
Process Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Process – a program in execution

  • A process includes:

    • program counter

    • stack

    • data section

ecs150 Spring 2006


Context switching
Context Switching Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


Running Mach, Minix, PalmOS, uCOS, TinyOS, …

Running

Running

Running

Blocked

Blocked

Blocked

Blocked

Ready

Ready

Ready

Ready

Scheduling &

Context Switching

ecs150 Spring 2006


States of a process

Running Mach, Minix, PalmOS, uCOS, TinyOS, …

Waiting

Ready

States of a Process

  • Running, Blocked, and Ready

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


1 Mach, Minix, PalmOS, uCOS, TinyOS, …

RR

0

0

:

:

.

256 different priorities

64 scheduling classes

0~63 bottom-half kernel (interrupt)

64~127 top-half kernel

128~159 real-time user

160~223 timeshare

224~255 idle

1

0

1

ecs150 Spring 2006


Kernel processes1
Kernel Processes Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


1 Mach, Minix, PalmOS, uCOS, TinyOS, …

RR

0

0

:

:

.

256 different priorities

64 scheduling classes

0~63 bottom-half kernel (interrupt)

64~127 top-half kernel

128~159 real-time user

160~223 timeshare

224~255 idle

1

0

1

ecs150 Spring 2006


Running Mach, Minix, PalmOS, uCOS, TinyOS, …

Waiting

Ready

ecs150 Spring 2006


4 4bsd process structure usr src sys sys proc h
4.4BSD Process Structure Mach, Minix, PalmOS, uCOS, TinyOS, …(/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

ecs150 Spring 2006


Freebsd user structure
FreeBSD User Structure Mach, Minix, PalmOS, uCOS, TinyOS, …

/*

* 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 */

}

ecs150 Spring 2006


5.x Kernel Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


1 Mach, Minix, PalmOS, uCOS, TinyOS, …

RR

0

KSE:

Kernel Scheduling Entity

 kernel-level thread

0

:

:

.

256 different priorities

64 scheduling classes

0~63 bottom-half kernel (interrupt)

64~127 top-half kernel

128~159 real-time user

160~223 timeshare

224~255 idle

1

0

1

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


What is a thread
What is a thread? Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


Process and thread abstraction and abstraction
Process and Thread Mach, Minix, PalmOS, uCOS, TinyOS, …(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.

ecs150 Spring 2006


A program and threads
A Program and Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

J=0;

(shared)

variables

If (j==0)

J=100

ecs150 Spring 2006


Threads
Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Heavy-weight Process versus Light-weight Thread

  • User-level versus Kernel-level

ecs150 Spring 2006


A process and a thread
a Process and a Thread Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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

ecs150 Spring 2006


Process and threads
Process and Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

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

Resources

ecs150 Spring 2006


Threads1
Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • User-level

  • Kernel-level

ecs150 Spring 2006


Threads2
Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Blocking/Synchronous I/O

    • One thread blocks all others???

    • “Block one  block all”

ecs150 Spring 2006


CPU chip Mach, Minix, PalmOS, uCOS, TinyOS, …

register file

ALU

system bus

memory bus

main

memory

bus interface

I/O

bridge

I/O bus

Expansion slots for

other devices such

as network adapters.

USB

controller

graphics

adapter

disk

controller

mouse

keyboard

monitor

disk

ecs150 Spring 2006


CPU chip Mach, Minix, PalmOS, uCOS, TinyOS, …

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

graphics

adapter

disk

controller

mouse

keyboard

monitor

disk

ecs150 Spring 2006


CPU chip Mach, Minix, PalmOS, uCOS, TinyOS, …

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

graphics

adapter

disk

controller

mouse

keyboard

monitor

disk

ecs150 Spring 2006


CPU chip Mach, Minix, PalmOS, uCOS, TinyOS, …

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

graphics

adapter

disk

controller

mouse

keyboard

monitor

disk

ecs150 Spring 2006


Asynchronous i o
Asynchronous I/O Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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 */

};

ecs150 Spring 2006


Usr src sys kern vfs aio c
/usr/src/sys/kern/vfs_aio.c Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Security Problem!! Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


User level threads
User-Level Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Threads3
Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


User and Kernel Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • One thread per process or multiple thread per process

UTS

UserLevelTs

KernelTs

KTS

KTS

Which approach is better???

ecs150 Spring 2006


User level threads1
User-Level Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • A small OS in the user-space to manage the threads.

  • The kernel is totally unaware how many threads the process currently has.

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


Why multiple threads
Why Multiple Threads?? Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


  • Responsiveness Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Resource Sharing

  • Economy

  • Utilization of MP Architectures

ecs150 Spring 2006


Process A Mach, Minix, PalmOS, uCOS, TinyOS, …

Process B

Global

Variables

Global

Variables

Code

Code

Stack

Stack

fork()

fork()

ecs150 Spring 2006


Parent Mach, Minix, PalmOS, uCOS, TinyOS, …

Child

Global

Variables

Global

Variables

Code

Code

Stack

Stack

fork()

execve()

Child

Global

Variables

Code

Stack

ecs150 Spring 2006


Global Mach, Minix, PalmOS, uCOS, TinyOS, …

Variables

Code

Stack

pthread_create()

Process A

Thread 1

pthread_create()

Process A

Thread 2

Stack

ecs150 Spring 2006


Creation time difference
Creation Time Difference Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


User threads
User Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Thread management done by user-level threads library

  • Examples

    - POSIX Pthreads

    - Mach C-threads

    - Solaris threads

ecs150 Spring 2006


Kernel threads
Kernel Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

  • Supported by the Kernel

  • Examples

    - Windows 95/98/NT/2000

    - Solaris

    - Linux

ecs150 Spring 2006


Solaris 2 threads
Solaris 2 Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

ecs150 Spring 2006


Linux threads
Linux Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


Open new connection Mach, Minix, PalmOS, uCOS, TinyOS, …

Thread class: run

Lib call: pthread_create

System call: Clone

ecs150 Spring 2006


Kt vs ut

Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

KT vs. UT

  • pros and cons?

  • BTW, how about FreeBSD?

ecs150 Spring 2006


ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


Uts kts

Process Mach, Minix, PalmOS, uCOS, TinyOS, …

Process

Process

Scheduler

Scheduler

Scheduler

processor

processor

processor

UTS + KTS

  • Two independent schedulers:

User Space

OS Kernel

ecs150 Spring 2006


Process Mach, Minix, PalmOS, uCOS, TinyOS, …

Process

Process

Scheduler

processor

processor

processor

KTS

  • One single scheduler:

User Space

OS Kernel

ecs150 Spring 2006


Kt vs ut1
KT vs. UT Mach, Minix, PalmOS, uCOS, TinyOS, …

UTS

Kernel Interface

KTS

ecs150 Spring 2006


Solaris 2 threads1
Solaris 2 Threads Mach, Minix, PalmOS, uCOS, TinyOS, …

mapping but NOTcoordinating

ecs150 Spring 2006


Questions to ask
Questions to ask Mach, Minix, PalmOS, uCOS, TinyOS, …

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

ecs150 Spring 2006


UTS Mach, Minix, PalmOS, uCOS, TinyOS, …

Library

Notify I/O events

Notify new decision

Kernel

KTS

ecs150 Spring 2006


syscall Mach, Minix, PalmOS, uCOS, TinyOS, …

I/O request

interrupt

Is this a problem?

User Space

Kernel Space

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!

ecs150 Spring 2006


Scheduler activations

CPU Mach, Minix, PalmOS, uCOS, TinyOS, …timewasted

syscall

User Space

I/O request

interrupt

Kernel Space

Hardware

CPU used

User Space

upcall

upcall

Kernel Space

Hardware

Scheduler Activations

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

ecs150 Spring 2006


Scheduler activations1
Scheduler Activations Mach, Minix, PalmOS, uCOS, TinyOS, …

  • 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

  • ecs150 Spring 2006


    One Model (FreeBSD 5.x) Mach, Minix, PalmOS, uCOS, TinyOS, …

    UTS - threads

    Library

    SA

    SA

    Notify I/O events

    Notify new decision

    SA

    SA

    Kernel

    KTS – virtual CPU’s

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    I o happens for thread

    I/O happens for Thread Mach, Minix, PalmOS, uCOS, TinyOS, …

    User Program

    (2)

    (3)

    (1)

    User-Level

    Runtime System

    (4)

    (A)

    (B)

    Add

    Processor

    Add

    Processor

    Operating System Kernel

    Processors

    ecs150 Spring 2006


    A s thread has blocked on an i o request

    User Program Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    (3)

    (2)

    (1)

    User-Level

    Runtime System

    (4)

    B

    (A)

    (B)

    ( C )

    Operating

    System Kernel

    A’s thread has blocked

    Processors

    ecs150 Spring 2006


    A s thread i o completed
    A’s Thread I/O completed Mach, Minix, PalmOS, uCOS, TinyOS, …

    User Program

    (2)

    (3)

    (4)

    (1)

    (1)

    User-Level

    Runtime System

    (A)

    (B)

    (D)

    A’s thread and B’s

    Thread can continue

    ( C )

    Operating

    System Kernel

    Processors

    “the upcall stack problem”

    ecs150 Spring 2006


    A’s Thread resumes on Scheduler Activation D Mach, Minix, PalmOS, uCOS, TinyOS, …

    User Program

    (2)

    (3)

    (4)

    (1)

    (1)

    User-Level

    Runtime System

    (D)

    A’s thread and B’s

    Thread can continue

    ( C )

    Operating

    System Kernel

    Processors

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    One Model (FreeBSD 5.x) Mach, Minix, PalmOS, uCOS, TinyOS, …

    UTS - threads

    Library

    SA

    SA

    Notify I/O events

    Notify new decision

    SA

    SA

    Kernel

    KTS – virtual CPU’s

    ecs150 Spring 2006


    Freebsd 5 x
    FreeBSD 5.x Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    #include <sys/types.h> Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    ecs150 Spring 2006


    struct kse_mailbox { Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    };

    ecs150 Spring 2006


    struct kse_thr_mailbox { Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    };

    ecs150 Spring 2006


    Upcalls
    upcalls Mach, Minix, PalmOS, uCOS, TinyOS, …

    • ksec_new

    • ksec_preempt

    • ksec_block

    • ksec_unblock

    ecs150 Spring 2006


    UTS Mach, Minix, PalmOS, uCOS, TinyOS, …

    Library

    ksec_new

    ksec_preempt

    ksec_block

    ksec_unblock

    kse_create

    kse_exit

    kse_release

    kse_wakeup

    kse_thr_interrupt

    Kernel

    KTS

    ecs150 Spring 2006


    Kse internal
    KSE Internal Mach, Minix, PalmOS, uCOS, TinyOS, …

    • KSE

    • KSEG

    • KSEC

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    Linux vpi virtual processor interface
    Linux VPI Mach, Minix, PalmOS, uCOS, TinyOS, …(Virtual Processor Interface)

    • Experimental/Research Prototype

      • Benson/Butner/Padden/Fedosov

      • Scheduler activation in Linux Kernel 2.4.18

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    One Model (FreeBSD 5.x) Mach, Minix, PalmOS, uCOS, TinyOS, …

    UTS - threads

    Library

    SA

    SA

    Notify I/O events

    Notify new decision

    SA

    SA

    Kernel

    KTS – virtual CPU’s

    ecs150 Spring 2006


    Kernel processes table 3 1 page 50
    Kernel Processes Mach, Minix, PalmOS, uCOS, TinyOS, …(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.”

    ecs150 Spring 2006


    Freebsd kernel
    FreeBSD Kernel Mach, Minix, PalmOS, uCOS, TinyOS, …

    ecs150 Spring 2006


    Freebsd kernel1
    FreeBSD Kernel Mach, Minix, PalmOS, uCOS, TinyOS, …

    ecs150 Spring 2006


    Kernel and user space1
    Kernel and User Space Mach, Minix, PalmOS, uCOS, TinyOS, …

    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)

    ecs150 Spring 2006


    What is micro kernel
    What is “micro-kernel”? Mach, Minix, PalmOS, uCOS, TinyOS, …

    ecs150 Spring 2006


    ….what are the basic services? Mach, Minix, PalmOS, uCOS, TinyOS, …

    OS

    ecs150 Spring 2006


    An Alternative: Micro-Kernel Mach, Minix, PalmOS, uCOS, TinyOS, …

    Message Passing versus Optimized Procedure Calls

    ecs150 Spring 2006


    ecs150 Spring 2006 Mach, Minix, PalmOS, uCOS, TinyOS, …


    Micro versus monolithic
    Micro versus Monolithic Mach, Minix, PalmOS, uCOS, TinyOS, …

    • What is the real difference between these two models??

    • First Brainstorming!!

    ecs150 Spring 2006


    Micro versus monolithic1
    Micro versus Monolithic Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    ecs150 Spring 2006


    Freebsd kernel size
    FreeBSD Kernel: Size Mach, Minix, PalmOS, uCOS, TinyOS, …

    • 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

    ecs150 Spring 2006


    Os design
    OS Design Mach, Minix, PalmOS, uCOS, TinyOS, …

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

    ecs150 Spring 2006


    • What is “Process”? Mach, Minix, PalmOS, uCOS, TinyOS, …

    • What is “System Call”?

    • What is “Kernel”?

    ecs150 Spring 2006


    ad