Operating System Labs. Yuanbin Wu

Similar documents
Operating System Labs. Yuanbin Wu

UNIX System Programming

File I/0. Advanced Programming in the UNIX Environment

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

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

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

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

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

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

CSE 333 SECTION 3. POSIX I/O Functions

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

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

Chapter 3. File I/O. System Programming 熊博安國立中正大學資訊工程學系

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

CS240: Programming in C

Operating systems. Lecture 7

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

Goals of this Lecture

I/O Management! Goals of this Lecture!

Naked C Lecture 6. File Operations and System Calls

I/O Management! Goals of this Lecture!

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

Files. Eric McCreath

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Recitation 8: Tshlab + VM

UNIX System Calls. Sys Calls versus Library Func

CSE 333 SECTION 3. POSIX I/O Functions

Input and Output System Calls

Standard I/O in C, Computer System and programming in C

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

CSC 271 Software I: Utilities and Internals

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

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

UNIX System Overview E. Im

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

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

CSE 410: Systems Programming

25.2 Opening and Closing a File

The course that gives CMU its Zip! I/O Nov 15, 2001

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Section 3: File I/O, JSON, Generics. Meghan Cowan

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

COMP 2355 Introduction to Systems Programming

Process Management! Goals of this Lecture!

File I/O - Filesystems from a user s perspective

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

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

File System. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

File Descriptors and Piping

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Process Creation in UNIX

OPERATING SYSTEMS: Lesson 2: Operating System Services

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

2009 S2 COMP File Operations

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

File IO and command line input CSE 2451

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

File and Console I/O. CS449 Spring 2016

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

Chapter 10. The UNIX System Interface

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

All the scoring jobs will be done by script

Inter-Process Communication

CS240: Programming in C

Files and Directories

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

Input/Output. Input/Output and Files. Input/Output. I/O System Objectives

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

CMPSC 311- Introduction to Systems Programming Module: Input/Output

Input/Output and the Operating Systems

Summer June 15, 2010

Operating System Labs. Yuanbin Wu

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

File I/O. Preprocessor Macros

All the scoring jobs will be done by script

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Lecture 21 Systems Programming in C

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

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

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

File Handling. Reference:

File and Console I/O. CS449 Fall 2017

Computer Architecture and Assembly Language. Practical Session 5

C PROGRAMMING. Characters and Strings File Processing Exercise

Transcription:

Operating System Labs Yuanbin Wu cs@ecnu

Annoucement Next Monday (28 Sept): We will have a lecture @ 4-302, 15:00-16:30 DON'T GO TO THE LABORATORY BUILDING! TA email update: ecnucchuang@163.com ecnucchuang@126.com

Operating System Labs Introduction of I/O operations Project 1 Sorting

Operating System Labs Manipulate I/O System call File descriptor No buffering Standard library FILE object Buffering

Operating System Labs Manipulate I/O System call File descriptor Standard library FILE object Buffer/non-buffer

I/O System Calls 5 basic system calls open(), read(), write(), lseek(), close() I/O without buffering File sharing understand descriptor Other dup(), fcntl(), sync(), fsync(), ioctl()

I/O System Calls File descriptor Allocated when open a file ID of the file in the process Default 0 (STDIN_FILENO): standard input 1 (STDOUT_FILENO): standard output 3 (STDERR_FILENO): standard error

I/O System Calls Open files: # include <fcntl.h> int open(const char *pathname, int o_flag, ); Return value Success: file descriptor Failed: -1 o_flag: O_RDONLY, O_WRONLY, O_RWWR Options: O_APPEND, O_CREAT, O_TRUNC,...

I/O System Calls Open files File descriptors: the smallest one available Examples int main (int argc, char **argv) { int fd = open( foo, O_RDONLY); printf( %d, fd); } int main (int argc, char **argv) { close(0); int fd = open( foo, O_RDONLY); printf( %d, fd); }

I/O System Calls Open files STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO opened by the OS when creating a process

I/O System Calls Close files # include <unistd.h> int close(int filedes); Return Success: 0 Failed: -1

I/O System Calls File Position # include <unistd.h> off_t lseek(int filedes, off_t offset, int whence); Current file offset : An offset (in byte) to the beginning of the file whence: SEEK_SET, SEEK_CUR, SEEK_END

Read files Return: I/O System Calls # include <unistd.h> int read(int filedes, void *buf, size_t nbytes); Success: number of bytes read (0, if EOF) Failed: -1 Return < size EOF Read from terminal (stdin), one line...

I/O System Calls Write files # include <unistd.h> int write(int filedes, const void *buf, size_t nbytes); Return: Success: number of bytes write Failed: -1

I/O System Calls I/O without buffer No (user space) buffer read(), write(): no buffer in user space Do have buffer in kernel space Let's do some coding Buffering do matter! printf, scanf in standard I/O library are buffered

I/O System Calls File descripter revisit file file status inode Process current offset file size fd file pointer inode access time 0 data block 1 2 file file status 3 current offset inode 4. inode file size access time data block

Process A fd file pointer 0 A, B open the same file 1 2 3 4. file file status current offset inode inode file size access time data block fd 0 1 2 3 4. Process B file pointer file file status current offset inode

I/O System Calls File sharing Automic operations Example: open( file, O_WRONLY O_APPEND) Two process A, B run the same code, what will happen? if (lseek(fd, O, SEEK_END) < 0) perror( lseek ); if (write(fd, buf, 100) < 100) perro( write );

I/O System Calls Duplicate file descriptor # include <unistd.h> int dup2(int fd, int fd2); set fd2 point to the same file of fd Return Success: fd Failed: -1

// fd 0 has been closed dup2(3, 0); fd file pointer 0 NULL 1 2 3 4. Process A file file status current offset inode inode file size access time data block a file with multiple file descriptors

I/O System Calls Other system calls sync() / fsync(): enque the kernel buffer fcntl(): change file (opened) attributes ioctl(): other methods

I/O System Calls Summary File descriptor open, close, read, write, lseek, dup File sharing

Operating System Labs Manipulate I/O System call File descriptor No buffering Standard library FILE object Buffering

Standard I/O Library #include <stdio.h> FILE object (structure) Buffering Formatted I/O

Standard I/O Library Recall: #include <stdio.h> void foo() { printf( bar\n ); } printf() fprintf() malloc() atoi() User application Library Functions (Glibc) write(), reads(), mmap() System Calls Kernel

Standard I/O Library # include <fcntl.h> int main (int argc, char **argv) { int fd = open( foo, O_RDONLY); } # include <stdio.h> int main (int argc, char **argv) { FILE* fp = fopen( foo, r ); } Stream and FILE object A wrapper of file descriptor More information: buffer, error info.

Standard I/O Library Buffering stdio provide a standard I/O buffer (user space) Three types of buffering Full buffered Performs I/O when the buffer is full Line buffered Performs I/O when encounter a newline Unbuffered Performs I/O immediately, no buffer

Standard I/O Library Three types of buffering: cases Standard error is unbuffered A stream is line buffered if it refers to terminal device, otherwise full buffered Write standard I/O buffer to disc: # include <stdio.h> int fflush(file *fp);

Standard I/O Library Open streams # include <stdio.h> FILE *fopen(const char* path, const char * type); Type: r, w, a... Return Failed: NULL

Standard I/O Library Character-at-a-time I/O # include <stdio.h> int getc(file *fp); int fgetc(file *fp); int putc(file *fp); int fputc(file *fp);

Standard I/O Library Line-at-a-time I/O # include <stdio.h> char* fgets(char *buf, int n, FILE *fp); char* gets(char *buf); int fputs(char *str, FILE *fp); int puts(char *str);

Standard I/O Library Direct I/O # include <stdio.h> size_t fread(void *ptr, size_t size, size_t, nobj, FILE *fp); size_t fwrite(void *ptr, size_t size, size_t, nobj, FILE *fp);

Standard I/O Library Standard I/O efficiency Recall: buffering in system calls Let's do some coding again

Standard I/O Library Formatted I/O printf, fprintf, scanf

Standard I/O Library Summary #include <stdio.h> FILE object (structure) Buffering Formatted I/O

Introduction of I/O Operations Summary System call File descriptor No buffering Standard library FILE object Buffering

Sorting Project 1

Annoucement Next Monday: We will have a lecture @ 4-302, 15:00-16:30 DON'T GO TO THE LABORATORY BUILDING! TA email update: ecnucchuang@163.com ecnucchuang@126.com