Input and Output System Calls

Similar documents
File I/0. Advanced Programming in the UNIX Environment

Operating System Labs. Yuanbin Wu

read(2) There can be several cases where read returns less than the number of bytes requested:

Outline. Relationship between file descriptors and open files

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

UNIX System Programming

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

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

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSE 410: Systems Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

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

which maintain a name to inode mapping which is convenient for people to use. All le objects are

Process Management! Goals of this Lecture!

CS240: Programming in C

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

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

CSE 333 SECTION 3. POSIX I/O Functions

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

Operating System Labs. Yuanbin Wu

UNIX System Calls. Sys Calls versus Library Func

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSC 271 Software I: Utilities and Internals

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

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

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

UNIX input and output

UNIX System Overview E. Im

Operating systems. Lecture 7

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

Naked C Lecture 6. File Operations and System Calls

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

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

File Descriptors and Piping

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

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

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

CSE 333 SECTION 3. POSIX I/O Functions

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

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

All the scoring jobs will be done by script

Data and File Structures Chapter 2. Basic File Processing Operations

Programmation Systèmes Cours 8 Synchronization & File Locking

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

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

Chapter 10. The UNIX System Interface

Network Socket Programming - 1 BUPT/QMUL

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

Files and Directories

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

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

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

Summer June 15, 2010

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

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

All the scoring jobs will be done by script

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

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

OPERATING SYSTEMS: Lesson 2: Operating System Services

Recitation 8: Tshlab + VM

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

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

Inter-Process Communication

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

CPSC 410/611: File Management. What is a File?

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

Lecture 21 Systems Programming in C

CSE 410: Systems Programming

File I/O - Filesystems from a user s perspective

T4-Input/Output Management

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Socket programming in C

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

Inter-process Communication using Pipes

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

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

File I/O. Preprocessor Macros

Processes. Non-examples (implemented as one process): Why processes? - emacs text editor - firefox web browser

CS350 Operating Systems

CS140 Operating Systems

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Final Exam Questions (document version 1.1) WITH SELECTED SOLUTIONS

Computer Science & Engineering Department I. I. T. Kharagpur

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

What did we talk about last time? Selection Loops Lab 3

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

Processes COMPSCI 386

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Transcription:

Chapter 2 Input and Output System Calls Internal UNIX System Calls & Libraries Using C --- 1011 OBJECTIVES Upon completion of this unit, you will be able to: Describe the characteristics of a file Open and close a file Read and write data to and from a file Change the read/write position in a file Control a file Chapter 2-1 Chapter 2-1

WHAT IS A FILE? z A file is a contiguous sequence of bytes. z No format imposed by the operating system z Each byte is individually addressable in a disk file. z Automatic expansion as data is written to a disk file z End-of-file indication is not part of data. z A file is also a uniform interface to external devices. Chapter 2-2 OPENING A FILE open(2) NAME SYNOPSIS open --- open for reading or writing #include <fcntl.h> int open (path, oflag [, mode ] ) char *path; int oflag, mode; RETURN VALUE success --- nonnegative file descriptor failure --- -1 is returned and errno is set Chapter 2-3 Chapter 2-2

open(2) FLAGS O_RDONLY --- reading only O_WRONLY --- writing only O_RDWR --- reading and writing O_NDELAY --- non-blocking O_APPEND --- positions at the end for all writes O_CREAT --- creates a non-existent file O_TRUNC --- discard all data O_EXCL --- ensure creating non-existent file O_SYNC --- wait for physical write Chapter 2-4 OPENING A FILE --- EXAMPLES z open a file for reading acctfd = open ( account, O_RDONLY) ; z open a file for writing file = TMPFILE ; fd = open ( file, O_WRONLY O_CREAT O_TRUNC, 0600) ; z open a file for appending logfd = open ( /sys/log, O_WRONLY O_APPEND O_CREAT, 0600) ; z open a file for reading and writing fdin = open ( argv[1], O_RDWR) ; z create a new file for writing if ( ( fdout = open ( TMPFILE, O_WRONLY O_CREAT O_EXCL, 0600 ) ) = = -1 ) perror ( TMPFILE ) ; Chapter 2-5 Chapter 2-3

FILE TABLES file descriptor table (inside user area) system file table system inode table Chapter 2-6 CLOSING A FILE close(2) NAME SYNOPSIS close --- close a file descriptor int close ( fildes ) int fildes ; RETURN VALUE success --- 0 failure --- -1 and errno is set Chapter 2-7 Chapter 2-4

read(2) NAME READ FROM A FILE read --- read from a file SYNOPSIS int read ( fildes, buf, nbyte ) int fildes ; char *buf ; unsigned nbytes ; RETURN VALUE success --- the number of bytes read failure --- -1 and errno is set Chapter 2-8 write(2) NAME WRITE TO A FILE write --- write to file SYNOPSIS int write ( fildes, buf, nbyte ) int fildes ; char *buf ; unsigned nbyte ; RETURN VALUE success --- the number of bytes written. failure --- -1 and errno is set Chapter 2-9 Chapter 2-5

COPY INPUT TO OUTPUT --- EXAMPLE 1 #include <stdio.h> 2 3 main( ) 4 { 5 char buf [ BUFSIZ ] ; 6 int n ; 7 8 while ( ( n = read ( 0, buf, BUFSIZ ) ) > 0 ) 9 write ( 1, buf, n ) ; 10 exit ( 0 ) ; 11 } Chapter 2-10 COPY FILE --- EXAMPLE 1 #include <stdio.h> 2 #include <fcntl.h> 3 #define PMODE 0644 4 5 main ( argc, argv ) 6 int argc ; 7 char *argv [ ] ; 8 { 9 int fdin, fdout, n ; 10 char buf [ BUFSIZ ] ; 11 12 if ( argc!= 3 ) { 13 fprintf ( stderr, Usage : %s filein fileout \n, 14 argv [0] ) ; 15 exit (1) ; 16 } Chapter 2-11 Chapter 2-6

17 if ( ( fdin = open ( argv [1], O_RDONLY ) ) = = -1 ) { 18 perror ( argv [1] ) ; 19 exit (2) ; 20 } 21 if ( ( fdout = open ( argv [2], O_WRONLY O_CREAT 22 O_TRUNC, PMODE ) ) = = -1 ) { 23 perror ( argv [2] ) ; 24 exit (3) ; 25 } 26 27 while ( ( n = read ( fdin, buf, BUFSIZ ) ) > 0 ) 28 write ( fdout, buf, n ) ; 29 30 exit (0) ; 31 } Chapter 2-12 MOVE READ/WRITE FILE POSITION lseek(2) NAME failure --- lseek --- move read/write file position SYNOPSIS long lseek ( fildes, offset, whence ) int fildes ; long offset ; int whence; RETURN VALUE success --- location in bytes from beginning of the file -1 and errno is set Chapter 2-13 Chapter 2-7

lseek(2) --- EXAMPLE z rewind z append lseek ( fd, 0L, 0 ) ; lseek ( fd, 0L, 2 ) ; z update read ( fd, (char *) &record, sizeof (record) ) ; /* update */ lseek ( fd, (long) -sizeof (record), 1 ) ; write ( fd, (char *) &record, sizeof (record) ) ; z record position loc = lseek ( fd, 0L, 1 ) ; z increase file lseek ( fd, (long) MAX * sizeof (record), 2 ) ; write ( fd, (char *) &record, sizeof (record) ) ; Chapter 2-14 CREATING AN EMPLOYEE FILE --- EXAMPLE 1 #include <fcntl.h> 2 #include <stdio.h> 3 #include employee.h 4 5 main ( argc, argv ) /* create employee file */ 6 int argc ; 7 char *argv [ ] ; 8 { 9 int fd, open ( ), getpid ( ) ; 10 struct employee record ; 11 12 if ( argc < 2 ) { 13 fprintf ( stderr, Usage : %s file\n, argv [0] ) ; 14 exit (1); 15 } Chapter 2-15 Chapter 2-8

16 if ( ( fd = open ( argv [1], O_WRONLY O_CREAT 17 O_EXCL, 0640 ) ) = = -1 ) { 18 perror ( argv [1] ) ; 19 exit (2) ; 20 } 21 for ( ; ; ) { 22 printf ( Enter employee name <SPACE> salary : ); 23 scanf ( %s, record.name ) ; 24 if ( record.name [0] = =. ) 25 break ; 26 scanf ( %d, &record.salary ) ; 27 record.pid = getpid ( ) ; 28 write ( fd, (char *) &record, sizeof ( record ) ) ; 29 } 30 close ( fd ) ; 31 exit (0) ; 32 } Chapter 2-16 QUERYING THE EMPLOYEE FILE --- EXAMPLE 1 #include <fcntl.h> 2 #include employee.h 3 4 main ( argc, argv ) 5 int argc ; 6 char *argv [ ] ; 7 { 8 int fd, recnum ; 9 struct employee record ; 10 11 if ( ( fd = open ( argv [1], O_RDONLY ) ) = = -1 ) { 12 perror ( argv [1] ) ; 13 exit (2); 14 } Chapter 2-17 Chapter 2-9

15 for ( ; ; ) { 16 printf ( \n Enter record number : ) ; 17 scanf ( %d, &recnum ) ; 18 if ( recnum < 0 ) 19 break ; 20 lseek ( fd, (long) recnum*sizeof ( record ), 0 ) ; 21 if ( read ( fd, (char *) &record, 22 sizeof ( record ) ) > 0 ) 23 printf ( Employee : %s \t Salary : %d \n, 24 record.name, record.salary ) ; 25 else 26 printf ( Record %d not found \n, recnum ) ; 27 } 28 close ( fd ) ; 29 exit (0) ; 30 } Chapter 2-18 DUPLICATE A FILE DESCRIPTOR dup(2) NAME SYNOPSIS dup --- duplicate an open file descriptor int dup ( fildes ) int fildes ; RETURN VALUE success --- a nonnegative file descriptor is returned failure --- -1 is returned and errno is set Chapter 2-19 Chapter 2-10

dup(2) --- FILE TABLES file descriptor table system file table system inode table Chapter 2-20 INPUT/OUTPUT REDIRECTION --- EXAMPLE 1 #include <fcntl.h> 2 #include <stdio.h> 3 4 main ( argc, argv ) /* demonstrate dup (2) */ 5 int argc ; 6 char *argv [ ] ; 7 { 8 9 close (1); 10 if ( open (argv [1], O_WRONLY 11 O_CREAT O_TRUNC, 0644 ) = = -1 ) { 12 perror ( argv [1] ) ; 13 exit (1) ; 14 } 15 Chapter 2-21 Chapter 2-11

16 close (2) ; 17 if ( dup (1) = = -1 ) { 18 perror ( argv [0] ) ; 19 exit (2) ; 20 } 21 22 printf ( first line to stdout ( uses fd 1 ) \n ) ; 23 fprintf ( stderr, first line to srderr ( uses fd 2 ) \n ) ; 24 printf ( second line to stdout \n ) ; 25 fprintf ( stderr, second line to stderr \n ) ; 26 27 } Chapter 2-22 FILE CONTROL fcntl(2) NAME fcntl --- file control SYNOPSIS #include <fcntl.h> int fcntl ( fildes, cmd, arg ) int fildes, cmd ; int arg ; or struct flock *arg ; RETURN VALUE success --- depends on cmd failure --- -1 and errno is set Chapter 2-23 Chapter 2-12

fcntl(2) COMMANDS z int arg ; F_DUPFD --- duplicate a file descriptor F_GETFD --- get the close-on-exec flag F_SETFD --- set the close-on-exec flag F_GETFL --- get the file status flags F_SETFL --- set the file status flags Chapter 2-24 ADDING EMPLOYEE RECORDS --- EXAMPLE 1 #include <fcntl.h> 2 #include employee 3 #define DUMMY 0 4 5 main ( argc, argv ) 6 int argc ; 7 char *argv [ ] ; 8 { 9 struct employee record ; 10 int fd, open( ), flags, fcntl( ), pid, getpid( ) ; 11 12 if ( ( fd = open ( argv[1], O_RDWR ) ) = = -1 ) { 13 perror ( argv[1] ) ; 14 exit (1) ; 15 } 16 /* 17 * other data processing here žöof 18 fj jš æbú*/ Chapter 2-25 Chapter 2-13

19 if ( ( flags = fcntl ( fd, F_GETFL, DUMMY ) ) = = -1 ) { 20 perror (argv[1]) ; 21 exit (2) ; 22 } 23 flags!= O_APPEND ; 24 fcntl ( fd, F_SETFL, flags ) ; 25 pid = getpid( ) ; 26 for ( ; ; ) { 27 printf ( /nenter employee name : ) ; 28 scanf ( %s, record.name) ; 29 if ( record.name[0] = =. ) 30 break ; 31 printf ( Enter employee salary : ) ; 32 scanf ( %d, &record.salary) ; 33 record.pid = pid ; 34 write ( fd, (char *) &record, sizeof (record) ) ; 35 } 36 close (fd) ; 37 } Chapter 2-26 OPENING TERMINAL FILE --- EXAMPLE O_NDELAY flag 1 #include <stdio.h> 2 #include <fcntl.h> 3 4 main ( ) 5 { 6 int fd, flags, open( ), fcntl( ) ; 7 int i, n ; 8 char line[bufsiz] ; 9 10 if ( ( fd = open ( /dev/tty, O_RDONLY 11 O_NDELAY ) ) = = -1 ) { 12 perror ( /dev/tty ) ; 13 exit (2); 14 } Chapter 2-27 Chapter 2-14

15 printf ( Enter your PIN within five seconds : \n> ) ; 16 sleep (5) ; 17 if (read (fd, line, BUFSIZ) = = 0) { 18 printf ( \nsorry\n ) ; 19 exit (1) ; 20 } 21 22 flags = fcntl ( fd, F_GETFL, 0 ) ; 23 flags &= ~ O_NDELAY ; /* turn off delay flag */ 24 fcntl ( fd, F_SETFL, flags ) ; 25 printf ( Enter your bank account number : \n> ) ; 26 read ( fd, line, BUFSIZ ) ; 27 28 /* 29 *.... data processing is performed here.. 30 */ 31 } Chapter 2-28 SUMMARY open (2) --- open for reading or writing close (2) --- close a file descriptor read (2) --- read from a file write (2) --- write to file lseek (2) --- move read / write file position fcntl (2) --- file control Chapter 2-29 Chapter 2-15

STANDARD INPUT/OUTPUT LIBRARY FUNCTIONS z Functions are easier to use z Functions provide more services z Increases efficiency of programs z Increases portability of programs Chapter 2-30 STANDARD INPUT/OUTPUT INCLUDE FILE < stdio.h >... 2 #ifndef _NFILE 3 #define _NFILE 20 4 5 #if u370 6 #define BUFSIZ 4096 7 #endif 8 #if vax u3b u3b5 9 #define BUFSIZ 1024 10 #endif 11 #if pdp11 12 #define BUFSIZ 512 13 #endif... Chapter 2-31 Chapter 2-16

... 18 typedef struct { 19 #if vax u3b u3b5 20 int _cnt ; 21 unsigned char *_ptr ; 22 #else 23 unsigned char *_ptr ; 24 int _cnt ; 25 #endif 26 unsigned char *_base ; 27 char _flag ; 28 char _file ; 29 } FILE ;... 61 #define getc (p) ( - - (p) -> _cnt < 0? _filbuf (p) : (int) *(p) -> _ptr++ ) 62 #define putc (x, p) ( - - (p) -> _cnt < 0? 63 _flsbuf ( (unsigned char) (x), (p) ) : 64 (int) (*(p) -> _ptr++ = (unsigned char) (x) ) )... 73 extern FILE _iob [ _NFILE ] ; Chapter 2-32 STANDARD I/O PACKAGE Text fpute ( X, fd ) ; Data fp UNI FILE _file = 3 _base = 107604 _cnt = 1021 _ptr = 407607 BUFSIZ User Area Fd [3] System I/O Cont Chapter 2-33 Chapter 2-17