OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Similar documents
Inter-Process Communication

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Operating System Labs. Yuanbin Wu

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Process Management! Goals of this Lecture!

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

Files and the Filesystems. Linux Files

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Process Management 1

File Descriptors and Piping

Recitation 8: Tshlab + VM

CSE 333 SECTION 3. POSIX I/O Functions

Processes COMPSCI 386

UNIX Kernel. UNIX History

Process Creation in UNIX

W4118: OS Overview. Junfeng Yang

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Process Management! Goals of this Lecture!

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

UNIX input and output

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

File I/0. Advanced Programming in the UNIX Environment

SOFTWARE ARCHITECTURE 3. SHELL

CS 201. Files and I/O. Gerson Robboy Portland State University

Goals of this Lecture

628 Lecture Notes Week 4

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Design Overview of the FreeBSD Kernel CIS 657

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

CS2028 -UNIX INTERNALS

Objectives. Chapter 2: Operating-System Structures. 2.1 Operating System Services

CSE 333 SECTION 3. POSIX I/O Functions

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Operating Systemss and Multicore Programming (1DT089)

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Operating System Labs. Yuanbin Wu

CSE 410: Systems Programming

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Figure 1 Ring Structures

Basic OS Progamming Abstrac7ons

CSE 410: Systems Programming

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Basic OS Progamming Abstrac2ons

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

IPC and Unix Special Files

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

CS240: Programming in C

CS 3733 Operating Systems

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

CSE 120 Principles of Operating Systems

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 25

CSC209 Review. Yeah! We made it!

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

FILE SYSTEMS, PART 2. CS124 Operating Systems Fall , Lecture 24

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Operating systems. Lecture 7

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Logical disks. Bach 2.2.1

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Virtual Memory: Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

UNIX System Calls. Sys Calls versus Library Func

CSE 153 Design of Operating Systems Fall 2018

CSCI0330 Intro Computer Systems Doeppner. Project C-Shell. Due: November 1, 2017 at 11:59pm

UNIX System Programming

CS450/550 Operating Systems

Chapter 2: System Structures

Lecture Topics. Announcements. Today: Operating System Overview (Stallings, chapter , ) Next: Processes (Stallings, chapter

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Understanding the Design of Virtual Memory. Zhiqiang Lin

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Memory Management - Demand Paging and Multi-level Page Tables

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Creating a Shell or Command Interperter Program CSCI411 Lab

Operating System Structure

CS 5460/6460 Operating Systems

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Operating System Structure

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

CSCI0330 Intro Computer Systems Doeppner. Project C-Shell. Due: October 31, 2018 at 11:59pm. 1 Introduction 2

Transcription:

OS COMPONENTS OVERVIEW OF UNIX FILE I/O CS124 Operating Systems Fall 2017-2018, Lecture 2

2 Operating System Components (1) Common components of operating systems: Users: Want to solve problems by using computer hardware OS may support only one user at a time, or many concurrent users, depending on system requirements Some systems usually have no users, so they have an extremely minimal UI e.g. automobile engine computers User(s) Applications Operating System Computer Hardware

3 Operating System Components (2) Common components of operating systems: Applications allow users to solve problems with the computer s resources Applications rely on the OS to manage those resources System Applications Some apps are provided Services by the operating system Services for providing and managing system resources Command shells (e.g. sh, csh, zsh, bash) GUI programs (X-server, system config tools, etc.) User(s) Applications Command-Line Support Operating System Computer Hardware GUI

4 Operating System Components (3) Common components of operating systems: Applications allow users to solve problems with the computer s resources Applications rely on the OS to manage those resources System Applications User apps are designed Services to solve specific problems e.g. text editors, compilers, web servers e.g. web browsers, word processors, spreadsheets User(s) Applications User Applications Command-Line Support Operating System GUI Computer Hardware

5 Operating System Components (4) Common components of operating systems: The OS itself can provide many different facilities Not every OS will provide all of these facilities Most obvious facility: System Applications program execution Services Load and run programs Optionally, ability to perform runtime linking if the OS supports shared libraries Program Handle program termination Execution (possibly with errors!) Pass along signals, etc. User(s) Applications User Applications Command-Line Support Operating System Computer Hardware GUI

6 Operating System Components (4) Common components of operating systems: Another obvious facility: resource allocation Resources to manage: Processor(s) particularly if OS supports multitasking System Applications Main memory Services Filesystem/external storage Other devices/peripherals Filesystems: OS usually supports several Program Execution Filesystems different filesystems Resource May also require periodic Allocation maintenance User(s) Applications User Applications Command-Line Support Operating System Computer Hardware GUI

7 Operating System Components (5) Common components of operating systems: Disks and many other peripheral devices require specific interactions to function properly I/O subsystem provides System Applications facilities to control computer hardware devices Services Often interact via I/O ports Do not want apps to do this! Usually modularized by Program the use of device drivers Execution Filesystems Present a clean abstraction Resource Allocation I/O Services for the rest of the OS to use User(s) Applications User Applications Command-Line Support Operating System Computer Hardware GUI

8 Operating System Components (6) Common components of operating systems: Many components of OS require communication Collaborating processes need to share information Called Inter-Process System Applications Communication (IPC) Services Many mechanisms: pipes, shared memory, messagepassing, local sockets, etc. Some processes need to Program communicate with other Execution Filesystems computer systems Resource Allocation I/O Services Many kinds of networking User(s) Applications User Applications Command-Line Support Operating System Communication Computer Hardware GUI

9 Operating System Components (7) Common components of operating systems: Some OSes record resource usage data Accounting facility Purpose: systems that bill users based on CPU System Applications access time, storage, Services networking Less common to bill for CPU time; very common in past Now, very common to bill Program for storage and network use Execution Filesystems Resource Allocation User(s) Applications User Applications Command-Line Support Operating System I/O Services Communication Accounting GUI Computer Hardware

10 Operating System Components (8) Common components of operating systems: All OSes must handle various errors that occur Varies widely, depending on what hardware can detect Common errors: Hard disk is full, or broken Filesystem is corrupt System Applications Services Memory errors A program behaves in an invalid way Program Printer has no paper or ink Execution Filesystems Less common errors: Processor failure, etc. Resource Allocation I/O Services User(s) Applications User Applications Command-Line Support Operating System Communication Accounting Computer Hardware GUI Error Detection

11 Operating System Components (9) Common components of operating systems: OSes must prevent many different kinds of abuses OS must protect itself from malicious programs Apps are not allowed to System Applications directly access operating Services system code or data (Computer hardware must provide this capability ) All interactions with OS Program Execution Filesystems are performed via Resource I/O Services system calls Allocation User(s) Applications User Applications Command-Line Support Operating System System Calls Communication Accounting GUI Protection Security Error Detection Computer Hardware

12 Operating System Components (10) Common components of operating systems: Operating system must also protect processes from each other A process should not be allowed to access another System Applications process data, unless this is specifically allowed by Services the process Again, this requires specific support from the computer hardware Program Execution Resource Allocation User(s) Applications User Applications Command-Line Support Operating System System Calls Filesystems I/O Services Communication Accounting GUI Protection Security Error Detection Computer Hardware

13 Protection and Security Will talk much more about computer hardware in future Two main features on computer processors allow operating systems to provide protection and security Feature 1: multiple processor operating modes The processor physically enforces different constraints on programs operating in different modes Minimal requirement: Kernel mode (a.k.a. protected mode, privileged mode, etc.) allows a program full access to all processor capabilities and operations User mode (a.k.a. normal mode) only allows a program to use a restricted subset of processor capabilities The kernel of the operating system is the part of the OS that runs in kernel mode The OS may have [many] other components running in user mode

14 Protection and Security (2) Some processors provide more than two operating modes Called hierarchical protection domains or protection rings Higher-privilege rings can also access lower-privilege operations and data IA32 provides four operating modes Level 0 = kernel mode; level 3 = user mode Support for multiple protection levels is ubiquitous, even in mobile devices e.g. ARMv7 processors in modern smartphones have 8 different protection levels for different scenarios Level 3 Level 2 Level 1 Level 0

15 Protection and Security (3) Feature 2: virtual memory The processor maps virtual addresses to physical addresses using a page table The memory management unit (MMU) performs this translation Translation Lookaside Buffers (TLBs) cache page table entries to avoid memory access overhead when translating addresses Only the kernel can manipulate the MMU s configuration, etc. Again, will discuss virtual memory much more in the future CPU mov 307, %ebx Virtual Address VPN TLB MMU PTE Physical Address Data Main Memory Page Table PTE

16 Protection and Security (4) Virtual memory allows OS to give each process its own isolated address space Processes have identical memory layouts, simplifying compilation, linking and loading Regions of memory can also be restricted to kernel-mode access only, or allow user-mode access Called kernel space and user space If user-mode code tries to access kernel space, processor notifies the OS Only kernel can manipulate this config! Different for each process Identical for each process 0xc0000000 %esp 0x40000000 brk 0x08048000 0 Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Memory mapped region for shared libraries Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Forbidden Kernel virtual memory Process virtual memory

17 Protection and Security (5) The OS must track certain details for each process e.g. process memory mapping e.g. the process scheduling configuration and behavior A process can t be allowed to access these details directly! Just as with global kernel state, allowing direct access would open security holes Process must ask the kernel to manipulate this state on its behalf Example: Console and file IO Different for each process Identical for each process 0xc0000000 %esp 0x40000000 brk 0x08048000 0 Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Memory mapped region for shared libraries Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Forbidden Kernel virtual memory Process virtual memory

18 Console and File I/O You run a program on a Windows or UNIX system The OS sets up certain basic facilities for your program to use Standard input/output/error streams What printf() and scanf() use by default! Standard input/output/error streams can be from: The console/terminal Redirected to/from disk files Your program sees the contents of a disk file on its standard input What your program writes on standard output goes to a file on disk Redirected to/from another process! Your program sees output of another process on its standard input Your program s standard output is fed to another process standard input

19 UNIX File/Console IO All input/output is performed with UNIX system functions: ssize_t read(int filedes, void *buf, size_t nbyte) ssize_t write(int filedes, const void *buf, size_t nbyte) Attempt to read or write nbyte bytes to file specified by filedes Actual number of bytes read or written is returned by the function EOF indicated by 0 return-value; errors indicated by values < 0 The user program requests that the kernel reads or writes up to nbyte bytes, on behalf of the process read() and write() are system calls Frequently takes a long time (milliseconds or microseconds) Kernel often initiates the request, then context-switches to another process until I/O subsystem fires an interrupt to signal completion

20 UNIX File/Console IO (2) filedes is a file descriptor A nonnegative integer value that represents a specific file or device Processes can have multiple open files Each process open files are recorded in an array of pointers Array elements point to file structs describing the open file, e.g. the process current read/write offset within the file filedes is simply an index into this array (Each process has a cap on total # of open files) files[0] files[1] files[2] files[3] Every process has this data structure, but processes are not allowed to directly manipulate it! The kernel maintains this data structure on behalf of each process offset v_ptr offset v_ptr

21 UNIX File/Console IO (3) Individual file structs reference the actual details of how to interact with the file Allows OS to support many kinds of file objects, not just disk files files[0] files[1] files[2] files[3] offset v_ptr offset v_ptr file_ops filename path size i_node file_ops is a struct containing function-pointers for common operations supported by all file types, e.g. struct file_operations { ssize_t (*read)(file *f, void *buf, size_t nb); ssize_t (*write)(file *f, void *buf, size_t nb);... };

22 UNIX File/Console IO (4) Individual file structs reference the actual details of how to interact with the file Allows OS to support many kinds of file objects, not just disk files Kernel can easily read and write completely different file types using indirection // Example kernel code for read(filedes,buf,nbyte) file *f = files[filedes]; files[0] files[1] files[2] files[3] offset v_ptr offset v_ptr res = f->v_ptr->file_ops->read(f, buf, nbyte); file_ops filename path size i_node

23 UNIX File/Console IO (5) Levels of indirection also allow multiple processes to have the same file open Each process has its own read/write offset for the file Operations are performed against the same underlying disk file Process A Kernel Data Process B Kernel Data files[0] files[1] files[2] files[3] files[0] files[1] files[2] files[3] files[4] files[5] offset v_ptr offset v_ptr Global Kernel Data file_ops filename path size i_node

24 UNIX Standard I/O When a UNIX process is initialized by the OS, standard input/output/error streams are set up automatically Almost always: File descriptor 0 = standard input File descriptor 1 = standard output File descriptor 2 = standard error For sake of compatibility, always use constants defined in unistd.h standard header file STDIN_FILENO = file descriptor of standard input STDOUT_FILENO = file descriptor of standard output STDERR_FILENO = file descriptor of standard error

25 UNIX Standard I/O and Command Shells Programs usually don t care about where stdin and stdout go, as long as they work Command shells care very much! grep Allow < logfile.txt > output.txt Shell sets grep s stdin to read from logfile.txt Shell sets grep s stdout to write to the file output.txt If output.txt already exists, it is truncated Once stdin and stdout are properly set, grep is invoked: argc = 2, argv = {"grep", "Allow", NULL}

26 UNIX Command Shell Operation UNIX command shells generally follow this process: 1. Wait for a command to be entered on the shell s standard input (usually entered by a user on the console, but not always!) 2. Tokenize the command into an array of tokens 3. If tokens[0] is an internal shell command (e.g. history or export) then handle the internal command, then go back to 1. 4. Otherwise, fork() off a child process to execute the program. wait() for the child process to terminate, then go back to 1. Child process: 1. If the parsed command specifies any redirection, modify stdin/stdout/stderr based on the command, and remove these tokens from the tokenized command 2. execve() the program specified in tokens[0], passing tokens as the program s arguments 3. If we got here, execution failed (e.g. file not found)! Report error.

27 Command Shell and Child Process How does the child process output to the command shell s standard output? How does it get the shell s stdin? When a UNIX process is forked, it is a near-identical copy of the parent process Only differences: process ID and parent process ID Specifically, the child process has all the same files open as the parent process And they have the exact same file descriptors

28 Command Shell and Child Process (2) When child process reads stdin and writes stdout/stderr, it writes the exact same files that the command-shell has as stdin/stdout/stderr Command Shell Child Process files[0] files[1] files[2] files[0] files[1] files[2] offset offset v_ptr offset v_ptr v_ptr offset offset v_ptr offset v_ptr v_ptr Global Kernel Data file_ops filename file_ops path filename size file_ops path filename size i_node path size i_node i_node

29 Command Shell and Child Process (3) If command redirects e.g. output to a file, clearly can t have the command-shell process do it before forking Would work fine for the child process, but the command-shell s I/O state would be broken for subsequent commands Command Shell Child Process files[0] files[1] files[2] files[0] files[1] files[2] offset offset v_ptr offset v_ptr v_ptr offset offset v_ptr offset v_ptr v_ptr Global Kernel Data file_ops filename file_ops path filename size file_ops path filename size i_node path size i_node i_node

30 Command Shell and Child Process (4) Child process must set up stdin, stdout, and stderr before it executes the actual program How does a process change what file is referenced by a given file descriptor? Process must ask the kernel to modify the file descriptors Command Shell Child Process files[0] files[1] files[2] files[0] files[1] files[2] offset offset v_ptr offset v_ptr v_ptr offset offset v_ptr offset v_ptr v_ptr Global Kernel Data file_ops filename file_ops path filename size file_ops path filename size i_node path size i_node i_node

31 Manipulating File Descriptors UNIX provides two system calls: dup() and dup2() int dup(int filedes) Duplicates the specified file descriptor, returning files[0] a new, previously unused file descriptor files[1] files[2] Note that the internal file struct files[3] is not duplicated, only the pointer files[4] to the file struct! Implication: Reads, writes and seeks through both file descriptors affect a single shared file-offset value Even though the one file has two descriptors, should call close() on each descriptor Remember: each process has a maximum number of open files (Kernel won t free the file struct until it has no more references) offset v_ptr

32 Manipulating File Descriptors (2) int dup2(int filedes, int filedes2) Duplicates the specified file descriptor into the descriptor specified by filedes2 If filedes2 is already an open file, it is closed before dup2() duplicates filedes (Unless filedes == filedes2, in which case nothing is closed) This function allows the command-shell s child process to redirect standard input and output e.g. to replace stdout with a file whose descriptor is in fd: dup2(fd, STDOUT_FILENO); As before, the file descriptor that was duplicated should be closed to keep from leaking descriptors close(fd);

33 Manipulating File Descriptors (3) Previous example: grep Allow < logfile.txt > output.txt After command shell forks off a child process, the child can execute code like this, before it starts grep: int in_fd, out_fd; in_fd = open("logfile.txt", O_RDONLY); dup2(in_fd, STDIN_FILENO); /* Replace stdin */ close(in_fd); out_fd = open("output.txt", O_CREAT O_TRUNC O_WRONLY, 0); dup2(out_fd, STDOUT_FILENO); /* Replace stdout */ close(out_fd);

34 Next Time Operating system architectural approaches Overview of computer hardware and interface with OS