Chapter 2 OperatingSystem Structures T Yang 2012 Partially

















![copy file 1 file 2 /* Create output file descriptor */ output_fd = open(argv[2], copy file 1 file 2 /* Create output file descriptor */ output_fd = open(argv[2],](https://slidetodoc.com/presentation_image_h2/50fe08f94356d373ff6638a524c68eb4/image-18.jpg)


















- Slides: 36

Chapter 2: Operating-System Structures T. Yang, 2012 Partially based on the OSCE text book Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

Chapter 2: Operating-System Structures n n n Operating System Services & User OS Interface System Calls System Programs (System utilities) Operating System Design and Implementation Virtual Machines

A View of Operating System Services

Linux Layers

Nachos system Layers User process Projects 2&3 Project 11 Thread Nachos kernel threads 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) Base Operating System (Linux for our class)

OS UI: Shell Command Interpreter

OS User Interface: GUI

Programming API – OS System Call

Standard C Library Example n C program invoking printf() library call, which calls write() system call

System Calls n n System calls: Programming interface to the services provided by the OS Mostly accessed by programs via a high-level Application Program Interface (API) rather than direct system call use n Three most common APIs are n n Win 32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM) Why use APIs rather than system calls?

System Calls n n System calls: Programming interface to the services provided by the OS Mostly accessed by programs via a high-level Application Program Interface (API) rather than direct system call use n Three most common APIs are n n Win 32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM) Why use APIs rather than system calls? Portability. Simplicity.

Types of System Calls n n n Process control File management Device management Information maintenance Communications Protection

Examples of Windows and Unix System Calls

Transition from User to Kernel Mode

Unix I/O Calls n file. Handle n n n = open(path. Name, flags) A file handle is a small integer, valid only within a single process, to operate on the device or file Pathname: a name in the file system. In unix, devices are put under /dev. E. g. /dev/ttya is the first serial port, /dev/sda the first SCSI drive Flags: read only, read/write, append etc… Mode may be added as the third argument for file permission error. Code = close(file. Handle) n Kernel will free the data structures associated

Unix I/O Calls n byte. Count = read(file. Handle, buf, count) n n Read at most count bytes from the device and put them in the byte buffer buf. Bytes placed from 0 th byte. Kernel can give the process fewer bytes, user process must check the byte. Count to see how many were actually returned. A negative byte. Count signals an error (value is the error type) byte. Count = write(file. Handle, buf, count) n n n Write at most count bytes from the buffer buf Actual number written returned in byte. Count A negative byte. Count signals an error

Copy file 1 to file 2 #command syntax: copy file 1 file 2 #include <stdio. h> #include <fcntl. h> #define BUF_SIZE 8192 void 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 filen"); return; } 17
![copy file 1 file 2 Create output file descriptor outputfd openargv2 copy file 1 file 2 /* Create output file descriptor */ output_fd = open(argv[2],](https://slidetodoc.com/presentation_image_h2/50fe08f94356d373ff6638a524c68eb4/image-18.jpg)
copy file 1 file 2 /* Create output file descriptor */ output_fd = open(argv[2], O_WRONLY | O_CREAT, 0644); if(output_fd == -1){ printf ("Error in openning the output filen"); 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 writingn"); } } close (input_fd); close (output_fd); } 18

System Programs/Utilities n Categories of System programs/utilities n n n n Process status and management File /directory manipulation File modification and text processing Programming language support (compilers) Program loading and execution Communications Application programs Most users’ view of the operation system is defined by system programs, not the actual system calls

Linux Utility Programs

OS Design & Implementation n Start by defining goals and specifications n Affected by n Choice of hardware n User goals – n n convenient to use, easy to learn, reliable, safe, and fast 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? Maximize flexibility Layered structure n Modular n Monolithic kernel vs. Microkernel n

Layered Approach n 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 approach n n Object-oriented Each core component is separate Each talks to the others over known interfaces Each is loadable as needed within the kernel

Monolithic Kernel vs. Microkernel 27

Microkernel System Structure n n Moves as much from the kernel into “user” space Communication takes place between user modules using message passing Benefits: n Easier to extend a microkernel n Easier to port the operating system to new architectures n More reliable (less code is running in kernel mode) n More secure Weakness: n Performance overhead of user space to kernel space communication

Mac OS X Structure

Virtual Machines n n n A virtual machine takes the layered approach A virtual machine provides an interface identical to the underlying bare hardware. The host creates the illusion that each guest has its own processor and virtual memory/storage.

Virtual Machines (Cont. ) (a) Non-virtual machine (b) virtual machine

VMware Architecture

The Java Virtual Machine

New OS Interface for Applications Google Android Microsoft Windows Phone 7 Application Store Android Market App Marketplace. App. Store User Interface Java Application Framework Browser 3 D Graphics Apple i. OS Cocoa Webkit Silverlight Internet Explorer Open. GL Direct. X Open. GL Main programming language Java C# Objective-C Virtual machine CLR None Dalvik VM Webkit

Android (Linux-based)

Apple i. OS Unix-based