lecture 2: os programming interface - cs.ucsb.educs170/slides/3os-interface.pdf · web servers web...

37
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Lecture 2: OS Programming Interface T. Yang, CS 170 2018

Upload: ngoduong

Post on 08-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Lecture 2: OS Programming Interface

T. Yang, CS 170 2018

What to Learn?

n Operating System Services & Interfacen System Callsn System utilities

n OS Layersn Virtual Machines

A View of Operating System Services

Interface to OS: How to Interact with OS?

Shell Command InterpreterGUI: Graphical User Interface

OS Programming interface

Operating SystemServices

System Programs/Utilitiesn Categories of System programs/utilities

n Process status and managementn File /directory manipulation n File modification and text processingn Programming language support (compilers)n Program loading and executionn Communicationsn Application programs

n Most users’ view of the operation system is defined by system utilities programs, not the actual progrmaming API & system calls

Linux Utility Programs

Programming API – OS System Call

Role of system callsCompilers

Web Servers

Web Browsers

DatabasesEmail

Word Processing

Portable OS LibrarySystem Call

InterfacePortable OS Kernel

Platform support, Device Drivers

x86 ARMPowerPC

Ethernet 802.11 a/b/g/n SCSI IDE GraphicsPCI

Hardware

Software

System

UserOS

Application / Service

Linux Layers

Class Project: Nachos system Layers

Base Operating System (Linux for our class)

Nachos kernel threadsThread 1 Thread 2 Thread N

Nachos OS modules(Threads mgm, File System, Code execution/memory mapping,

System calls/Interrupt)

Simulated MIPS Machine(CPU, Memory, Disk, Console)

User processUser process

Projects 2&3

Project 1

Virtual machine

System Callsn System calls: Programming interface to the

services provided by the OSn Mostly accessed by programs via a high-

level Application Program Interface (API) rather than direct system call usen Three most common APIs are

n Win32 API for Windows,n POSIX API for POSIX-based systems (including

virtually all versions of UNIX, Linux, and Mac OS X), and

n Java API for the Java virtual machine (JVM)

n Why use APIs rather than system calls?

System Callsn System calls: Programming interface to the

services provided by the OSn Mostly accessed by programs via a high-

level Application Program Interface (API) rather than direct system call usen Three most common APIs are

n Win32 API for Windows,n POSIX API for POSIX-based systems (including

virtually all versions of UNIX, Linux, and Mac OS X), and

n Java API for the Java virtual machine (JVM)

n Why use APIs rather than system calls? Portability. Simplicity.

Transition from User to Kernel Mode

Standard C Library Examplen C program invoking printf() library call,

which calls write() system call

Kernel mode

User mode

Types of System Calls

n Process controln File managementn Device managementn Information maintenancen Communicationsn Protection

Examples of Windows and Unix System Calls

Unix I/O Calls with File DescriptorsnfileHandle = open(pathName, flags)

n A file handle (called file descriptor in Unix) is a small integer, pointing to a meta data structure about this file.

n Pathname: a name in the file system. n Flags: read only, read/write, append etc…

nerrorCode = close(fileHandle)

n Kernel will free the meta data structures associated

MemoryFile

540 1 2Metadata on a file

Yes

Does child process inherit file descriptors of parent process?

Parent ChildDuplicate

Unix I/O Calls nbyteCount = read(fileHandle, buf, count)

n Read at most count bytes from the device and put them in the byte buffer buf.

n Kernel can give the process fewer bytes, user process must check the byteCount to see how many were actually returned.

n A negative byteCount signals an error n byteCount = write(fileHandle, buf, count)

n Write at most count bytes from the buffer bufn Actual number written returned in byteCount n A negative byteCount signals an error

Seek offset

File content in bytes

Read count

Seek offset

File content in bytes

Write count

Where to store the seek offset?In-memory file meta data structure refered by file descriptor

19

Example: Implement Command copy file1 file2

#include <stdio.h>#include <fcntl.h>#define BUF_SIZE 8192void main(int argc, char* argv[]) {

int input_fd, output_fd; int ret_in, ret_out;char buffer[BUF_SIZE]; /* Create input file descriptor */input_fd = open (argv [1], O_RDONLY);if (input_fd == -1) {

printf ("Error in openning the input file\n"); return;}

File1 File2

Memory buffer

5 6

20

copy file1 file2/* Create output file descriptor */

output_fd = open(argv[2], O_WRONLY | O_CREAT, 0644);if(output_fd == -1){

printf ("Error in openning the output file\n"); return;}/* Copy process */while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){

ret_out = write (output_fd, &buffer, ret_in);if(ret_out != ret_in){ /* Write error */

printf("Error in writing\n");}

}close (input_fd); close (output_fd);

}

File1 File2

Memory buffer

5 6

Shelln A shell is a job control system

n Lets user execute system utilities/applicationsn Windows, MacOS, Linux all have shells

n Typical format:n cmd arg1 arg2 ... argnn i/o redirection <, >n filters & pipes

n ls | more

Proj 0

OS Design & Implementation

n Start by defining goals and specifications

n Affected by n Choice of hardwaren User goals –

n convenient to use, easy to learn, reliable, safe, and fast

n System goals –n easy to design, implement, and

maintain, as well as flexible, reliable, error-free, and efficient

OS Design Principles

n Separate policy (what to do) and mechanism (how to do)n Why?

n Layered structuren Modularn Monolithic kernel vs. Microkernel

Maximize flexibility

Layered Approachn The operating system is divided into a

number of layers (levels), each built on top of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.

MS-DOS: Simple Layer Structure n written to provide the most functionality in

the least space

Traditional UNIX System Structure

Modular approachn Object-orientedn Each core component is separaten Each talks to the others over known interfacesn Each is loadable as needed within the kernel

28

Monolithic Kernel vs. Microkernel

Powerful kernel which can do everything

Thin kernel which offers minimial features

Microkernel System Structure n Moves as much from the kernel into “user” spacen Communication takes place between user modules

using message passingn Benefits:

n Easier to extend a microkerneln Easier to port the operating system to new

architecturesn More reliable (less code is running in kernel

mode)n More secure

n Weakness:n Performance overhead of user space to kernel

space communication

Mac OS X and iOS

UNIX

Virtual Machinesn A virtual machine takes the layered

approachn A virtual machine provides an interface

identical to the underlying bare hardware.n The host creates the illusion that each guest

has its own processor and virtual memory/storage.

Nachos can be considered as providing MIPS virtual machine running an Intel Linux machine

Example of virtual machine?

The Java Virtual Machine

Virtual Machines (Cont.)

(a) Non-virtual machine (b) virtual machine

VMware Architecture

Android (Linux-based)

What we have learned?

n Operating System Services & Interfacen System Callsn System utilities

n OS Layers and Virtual Machines: Discuss later

Role of system calls and utilitiesCompilers

Web Servers

Web Browsers

DatabasesEmail

Word Processing

Portable OS LibrarySystem Call

InterfacePortable OS Kernel

Platform support, Device Drivers

x86 ARMPowerPC

Ethernet 802.11 a/b/g/n SCSI IDE GraphicsPCI

Hardware

Software

System

UserOS

Application / ServiceSystem utilities