System Calls and Standard I/O



Similar documents
Operating Systems. Privileged Instructions

Lecture 24 Systems Programming in C

System Calls Related to File Manipulation

LOW LEVEL FILE PROCESSING

Networks. Inter-process Communication. Pipes. Inter-process Communication

Illustration 1: Diagram of program function and data flow

Lab 4: Socket Programming: netcat part

CSC 2405: Computer Systems II

Giving credit where credit is due

Operating Systems and Networks

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Programmation Systèmes Cours 7 IPC: FIFO

Tutorial on Socket Programming

Jorix kernel: real-time scheduling

Module 816. File Management in C. M. Campbell 1993 Deakin University

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

File Management. Chapter 12

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Lecture 16: System-Level I/O

System calls. Problem: How to access resources other than CPU

Chapter I/O and File System

Sources: On the Web: Slides will be available on:

Operating Systems CSE 410, Spring File Management. Stephen Wagner Michigan State University

Unix System Calls. Dept. CSIE

Operating System Structure

Linux Driver Devices. Why, When, Which, How?

Linux Kernel Architecture

/* File: blkcopy.c. size_t n

UNIX File Management (continued)

File Handling. What is a file?

Chapter 12 File Management. Roadmap

Chapter 12 File Management

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

UNIX Sockets. COS 461 Precept 1

W4118 Operating Systems. Junfeng Yang

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

Grundlagen der Betriebssystemprogrammierung

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Chapter 6, The Operating System Machine Level

The C Programming Language course syllabus associate level

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

1. Computer System Structure and Components

Chapter 11 I/O Management and Disk Scheduling

Have both hardware and software. Want to hide the details from the programmer (user).

Introduction. What is an Operating System?

Lab 2 : Basic File Server. Introduction

Chapter 12 File Management

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

IPC. Semaphores were chosen for synchronisation (out of several options).

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Shared Memory Introduction

PHP Debugging. Draft: March 19, Christopher Vickery

Operating System Structures

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

File Management. COMP3231 Operating Systems. Kevin Elphinstone. Tanenbaum, Chapter 4

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Embedded Systems Design Course Applying the mbed microcontroller

Operating System Overview. Otto J. Anshus

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Leak Check Version 2.1 for Linux TM

Betriebssysteme KU Security

How To Write Portable Programs In C

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

COS 318: Operating Systems. I/O Device and Drivers. Input and Output. Definitions and General Method. Revisit Hardware

Device Management Functions

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Tutorial on C Language Programming

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Lecture 22: C Programming 4 Embedded Systems

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Shared Memory Segments and POSIX Semaphores 1

Programming Interface. for. Bus Master IDE Controller. Revision 1.0

Simple C Programs. Goals for this Lecture. Help you learn about:

Audit Trail Administration

The Plan Today... System Calls and API's Basics of OS design Virtual Machines

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Operating System Components and Services

CS161: Operating Systems

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

Implementing and testing tftp

Example of Standard API

64 Bit File Access. 1.0 Introduction. 2.0 New Interfaces. 2.1 New User Visible Types. Adam Sweeney

1. Introduction to the UNIX File System: logical vision

Network Programming with Sockets. Process Management in UNIX

CS222: Systems Programming

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

COS 318: Operating Systems

ELEC 377. Operating Systems. Week 1 Class 3

Using the CoreSight ITM for debug and testing in RTX applications

COS 318: Operating Systems. File Layout and Directories. Topics. File System Components. Steps to Open A File

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

Application-Level Debugging and Profiling: Gaps in the Tool Ecosystem. Dr Rosemary Francis, Ellexus

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

Distributed File Systems

Transcription:

System Calls and Standard I/O Professor Jennifer Rexford http://www.cs.princeton.edu/~jrex 1 Goals of Today s Class System calls o How a user process contacts the Operating System o For advanced services that may require special privilege Standard I/O library o Generic I/O support for C programs o A smart wrapper around I/O-related system calls o Stream concept, line-by-line input, formatted output,... 2 1

System Calls 3 Communicating With the OS signals User Process Operating System systems calls System call o Request to the operating system to perform a task o that the process does not have permission to perform Signal o Asynchronous notification sent to a process o to notify the process of an event that has occurred 4 2

Processor Modes The OS must restrict what a user process can do o What instructions can execute o What portions of the address space are accessible Supervisor mode (or kernel mode) o Can execute any instructions in the instruction set Including halting the processor, changing mode bit, initiating I/O o Can access any memory location in the system Including code and data in the OS address space User mode o Restricted capabilities Cannot execute privileged instructions Cannot directly reference code or data in OS address space o Any such attempt results in a fatal protection fault Instead, access OS code and data indirectly via system calls 5 Main Categories of System Calls File system o Low-level file I/O o E.g., creat, open, read, write, lseek, close Multi-tasking mechanisms o Process control o E.g., fork, wait, exec, exit, signal, kill Inter-process communication o E.g., pipe, dup, dup2 Unix has a few hundred system calls o See man 2 intro or /usr/include/syscall.h 6 3

System Calls Method for user process to invoke OS services Application OS User Process File System creat, open, close, read, write, lseek Called just like a function o Essentially a protected function call o That transfers control to the OS and back 7 Implementing a System Call System calls are often implemented using traps o OS gains control through trap o Switches to supervisor mode Which call? o Performs the service 1: exit o Switches back to user mode 2: fork o Gives control back to user 3: read 4: write 5: open movl $1, %eax 6: close int $0x80 Trap to the OS System-call specific arguments are put in registers 8 4

Main UNIX System Calls for Files Creat: int creat(char *pathname, mode_t mode); o Create a new file and assign a file descriptor Open: int open(char *pathname, int flags, mode_t mode); o Open the file pathname and return a file descriptor Close: int close(int fd); o Close a file descriptor fd Read: int read(int fd, void *buf, int count); o Read up to count bytes from fd, into the buffer at buf Write:int write(int fd, void *buf, int count); o Writes up to count bytes into fd, from the buffer at buf Lseek: int lseek(int fd, int offset, int whence); o Assigns the file pointer to a new value by applying an offset 9 Example: UNIX open() System Call Converts a path name into a file descriptor o int open(const char *pathname, int flags, mode_t mode); Arguments o Pathname: name of the file o Flags: bit flags for O_RDONLY, O_WRONLY, O_RDWR o Mode: permissions to set if file must be created Returns o Integer file descriptor (or a -1 if an error) Performs a variety of checks o E.g., whether the process is entitled to access the file 10 5

Example: UNIX read() System Call Converts a path name into a file descriptor o int read(int fd, void *buf, int count); Arguments o File descriptor: integer descriptor returned by open() o Buffer: pointer to memory to store the bytes it reads o Count: maximum number of bytes to read Returns o Number of bytes read Value of 0 if nothing more to read Value of -1 if an error Performs a variety of checks o Whether file has been opened, whether reading is okay 11 Standard I/O Library 12 6

Standard I/O Library Portability o Generic I/O support for C programs o Specific implementations for various host OSes o Invokes the OS-specific system calls for I/O Abstractions for C programs o Streams o Line-by-line input o Formatted output Additional optimizations o Buffered I/O o Safe writing user OS Appl Prog Stdio Library File System 13 Layers of Abstraction User process Operating System Appl Prog Stdio Library File System Storage Driver FILE * stream int fd hierarchical file system variable-length segments disk blocks Disk 14 7

Stream Abstraction Any source of input or destination for output o E.g., keyboard as input, and screen as output o E.g., files on disk or CD, network ports, printer port, Accessed in C programs through file pointers o E.g., FILE *fp1, *fp2; o E.g., fp1 = fopen( myfile.txt, r ); Three streams provided by stdio.h o Streams stdin, stdout, and stderr Typically map to keyboard, screen, and screen o Can redirect to correspond to other streams E.g., stdin can be the output of another program E.g., stdout can be the input to another program 15 Sequential Access to a Stream Each stream has an associated file position o Starting at beginning of file (if opened to read or write) o Or, starting at end of file (if opened to append) file file Read/write operations advance the file position o Allows sequencing through the file in sequential manner Support for random access to the stream o Functions to learn current position and seek to new one 16 8

Example: Opening a File FILE *fopen( myfile.txt, r ) o Open the named file and return a stream o Includes a mode, such as r for read or w for write Creates a FILE data structure for the file o File descriptor, mode, status, buffer, o Assigns fields and returns a pointer Opens or creates the file, based on the mode o Write ( w ): create file with default permissions o Read ( r ): open the file as read-only o Append ( a ): open or create file, and seek to the end 17 Example: Formatted I/O int fprintf(fp1, Number: %d\n, i) o Convert and write output to stream in specified format int fscanf(fp1, FooBar: %d, &i) o Read from stream in format and assign converted values Specialized versions o printf( ) is just fprintf(stdout, ) o scanf( ) is just fscanf(stdin, ) 18 9

Example: A Simple getchar() int getchar(void) { static char c; if (read(0, &c, 1) == 1) return c; else return EOF; } Read one character from stdin o File descriptor 0 is stdin o &c points to the buffer o 1 is the number of bytes to read Read returns the number of bytes read o In this case, 1 byte means success 19 Making getchar() More Efficient Poor performance reading one byte at a time o Read system call is accessing the device (e.g., a disk) o Reading one byte from disk is very time consuming o Better to read and write in larger chunks Buffered I/O o Read a large chunk from disk into a buffer Dole out bytes to the user process as needed Discard buffer contents when the stream is closed o Similarly, for writing, write individual bytes to a buffer And write to disk when full, or when stream is closed Known as flushing the buffer 20 10

Better getchar() with Buffered I/O int getchar(void) { static char base[1024]; static char *ptr; static int cnt = 0; if (cnt--) return *ptr++; persistent variables base } cnt = read(0, base, sizeof(base)); if (cnt <= 0) return EOF; ptr = base; ptr return getchar(); But, many functions may read (or write) the stream 21 Details of FILE in stdio.h (K&R 8.5) #define OPEN_MAX 20 /* max files open at once */ typedef struct _iobuf { int cnt; /* num chars left in buffer */ char *ptr; /* ptr to next char in buffer */ char *base; /* beginning of buffer */ int flag; /* open mode flags, etc. */ char fd; /* file descriptor */ } FILE; extern FILE _iob[open_max]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2]) 22 11

A Funny Thing About Buffered I/O int main() { printf( Step 1\n ); sleep(10); printf( Step2\n ); } Run a.out > out.txt & and then tail -f out.txt o To run a.out in the background, outputting to out.txt o And then to see the contents on out.txt Neither line appears till ten seconds have elapsed o Because the output is being buffered o Add fflush(stdout) to flush the output buffer o fclose() also flushes the buffer before closing 23 Challenges of Writing Write system call o int write(int fd, void *buf, int count); o Writes up to count bytes into fd, from the buffer at buf Problem: might not write everything o Can return a number less than count o E.g., if the file system ran out of space Solution: safe_write o Try again to write the remaining bytes o Produce an error if it impossible to write more 24 12

Safe-Write Code int safe_write(int fd, char *buf, int nbytes) { int n; char *p = buf; char *q = buf + nbytes; while (p < q) { if ((n = write(fd, p, (q-p)*sizeof(char))) > 0) p += n/sizeof(char); else perror( safe_write: ); } return nbytes; } p p q 25 Summary of System Calls and Stdio Standard I/O library provides simple abstractions o Stream as a source or destination of data o Functions for manipulating files and strings Standard I/O library builds on the OS services o Calls OS-specific system calls for low-level I/O o Adds features such as buffered I/O and safe writing Powerful examples of abstraction o User programs can interact with streams at a high level o Standard I/O library deals with some more gory details o Only the OS deals with the device-specific details 26 13