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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSC 271 Software I: Utilities and Internals

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

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

UNIX System Calls. Sys Calls versus Library Func

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

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

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

All the scoring jobs will be done by script

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

Operating System Labs. Yuanbin Wu

Chapter 10. The UNIX System Interface

Lecture 21 Systems Programming in C

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

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

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

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

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

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

UNIX System Programming

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

All the scoring jobs will be done by script

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

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

Preview. lseek System Calls. A File Copy 9/18/2017. An opened file offset can be explicitly positioned by calling lseek system call.

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

Operating systems. Lecture 7

Summer June 15, 2010

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

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

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Files. Eric McCreath

OPERATING SYSTEMS: Lesson 2: Operating System Services

Operating System Labs. Yuanbin Wu

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

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

CSE 333 SECTION 3. POSIX I/O Functions

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

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

Naked C Lecture 6. File Operations and System Calls

Shared Memory Memory mapped files

Operating System Labs. Yuanbin Wu

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Lecture 23: System-Level I/O

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

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

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

Files and Directories Filesystems from a user s perspective

CSE 410: Systems Programming

1 System & Activities

structs as arguments

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

File I/0. Advanced Programming in the UNIX Environment

Recitation 8: Tshlab + VM

Chapter 13 Systems Programming. Systems Programming. Error Handling. Error Handling. Example: File I/O. Example: File I/O

File and Directories. Advanced Programming in the UNIX Environment

Operating System Labs. Yuanbin Wu

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

프로세스간통신 (Interprocess communication) i 숙명여대창병모

CSE 333 SECTION 3. POSIX I/O Functions

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

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

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

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

Linux C C man mkdir( ) mkdir Linux man mkdir mkdir( ) mkdir mkdir( )

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

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

CS240: Programming in C

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

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

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

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

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

File Systems. q Files and directories q Sharing and protection q File & directory implementation

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

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

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

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

C I/O Library. stdout, stderr, stdin

Input and Output System Calls

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

UNIX input and output

CSCI 4061: Files, Directories, Standard I/O

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

39. File and Directories

Unix System Calls. Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University

Lecture 3. Review of C Programming Tools Unix File I/O System Calls

CS , Spring Sample Exam 3

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

System-Level I/O Nov 14, 2002

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

Transcription:

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

More System Calls Directory and File System Management s = mkdir(name, mode) Create a new directory s = rmdir(name) s = link(name, name) s = unlink(name) s = mount(special, name, flag) s = umount(special) Miscellaneous s = chdir(dirname) s = chmod(name, mode) s = kill(pid, signal) Remove an empty directory Create a new entry, name, pointing to name Remove a directory entry Mount a file system Unmount a file system Change the working directory Change a file s protection bits Send a signal to a process seconds = time(&seconds) Get the elapsed time since January 1, 1970 Copyright : University of Illinois CS 241 Staff 2

Example (open()) #include <fcntl.h> #include <errno.h> Argument: string Output: the string, a colon, and a description of the error condition stored in errno int main() { int fd; fd = open("foo.txt", O_RDONLY O_CREAT); if (fd == -1){ perror("program"); exit(1); printf("%d\n", fd); Copyright : University of Illinois CS 241 Staff 3

Example (close()) #include <fcntl.h> main(){ int fd1; if(( fd1 = open( foo.txt", O_RDONLY)) < 0){ perror("c1"); exit(1); if (close(fd1) < 0) { perror("c1"); exit(1); printf("closed the fd.\n"); Copyright : University of Illinois CS 241 Staff 4

Example (close()) #include <fcntl.h> main(){ int fd1; if(( fd1 = open( foo.txt", O_RDONLY)) < 0){ perror("c1"); exit(1); if (close(fd1) < 0) { perror("c1"); exit(1); printf("closed the fd.\n"); After close, can you still use the file descriptor? Why do we need to close a file? Copyright : University of Illinois CS 241 Staff 5

Example (read()) #include <fcntl.h> main() { char *c; int fd, sz; c = (char *) malloc(100 * sizeof(char)); fd = open( foo.txt", O_RDONLY); if (fd < 0) { perror("r1"); exit(1); sz = read(fd, c, 10); printf("called read(%d, c, 10). returned that %d bytes were read.\n", fd, sz); c[sz] = '\0'; printf("those bytes are as follows: %s\n", c); close(fd); Copyright : University of Illinois CS 241 Staff 6

Example (write()) #include <fcntl.h> main() { int fd, sz; fd = open("out3", O_RDWR O_CREAT O_APPEND, 0644); if (fd < 0) { perror("r1"); exit(1); sz = write(fd, "cs241\n", strlen("cs241\n")); printf("called write(%d, \"cs241\\n\", %d). it returned %d\n", fd, strlen("cs241\n"), sz); close(fd); Copyright : University of Illinois CS 241 Staff 7

File: Statistics #include <sys/stat.h> int stat(const char* name, struct stat* buf); Get information about a file Returns: 0 on success -1 on error, sets errno Parameters: name: Path to file you want to use Absolute paths begin with /, relative paths do not buf: Statistics structure off_t st_size: Size in bytes mode_t st_mode: protection time_t st_mtime: Date of last modification Also int fstat(int filedes, struct stat *buf); Copyright : University of Illinois CS 241 Staff 8

Useful Macros: File types Is file a symbolic link Is file a block device S_ISLNK(st_mode) S_ISBLK(st_mode) Is file a regular file Is file a FIFO S_ISREG(st_mode) S_ISFIFO(st_mode) Is file a character device Is file a unix socket S_ISSOCK(st_mode) S_ISCHR(st_mode) Copyright : University of Illinois CS 241 Staff 9

Useful Macros: File Modes S_IRWXU(st_mode) read, write, execute, owner S_IRUSR(st_mode) read permission, owner S_IWUSR(st_mode) write permission, owner S_IXUSR(st_mode) execute, owner S_IRGRP(st_mode) read permission, group S_IRWXO(st_mode) read, write, execute, others Copyright : University of Illinois CS 241 Staff 10

Example - (stat()) #include <unistd.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> int main(int argc, char **argv) { struct stat filestat; if(argc!= 2) return 1; if(stat(argv[1], &filestat) < 0) return 1; printf("information for %s\n",argv[1]); printf("---------------------------\n"); printf("file Size: \t\t%d bytes\n", filestat.st_size); printf("number of hard links: \t%d\n", filestat.st_nlink); printf("file inode number: \t\t%d\n", filestat.st_ino); Copyright : University of Illinois CS 241 Staff 11

Example - (stat()) printf("file Permissions: \t"); printf( (S_ISDIR(fileStat.st_mode))? "d" : "-"); printf( (filestat.st_mode & S_IRUSR)? "r" : "-"); printf( (filestat.st_mode & S_IWUSR)? "w" : "-"); printf( (filestat.st_mode & S_IXUSR)? "x" : "-"); printf( (filestat.st_mode & S_IRGRP)? "r" : "-"); printf( (filestat.st_mode & S_IWGRP)? "w" : "-"); printf( (filestat.st_mode & S_IXGRP)? "x" : "-"); printf( (filestat.st_mode & S_IROTH)? "r" : "-"); printf( (filestat.st_mode & S_IWOTH)? "w" : "-"); printf( (filestat.st_mode & S_IXOTH)? "x" : "-"); printf("\n\n"); printf("the file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode))? "is" : "is not"); return 0; Copyright : University of Illinois CS 241 Staff 12

Example - (stat()) Information for testfile.sh --------------------------- File Size: 36 bytes Number of hard links: 1 File inode number: 180055 File Permissions: -rwxr-xr-x The file is not a symbolic link Copyright : University of Illinois CS 241 Staff 13

File: Seek #include <unistd.h> off_t lseek(int fd, off_t offset, int whence); Explicitly set the file offset for the open file Return: Where the file pointer is the new offset, in bytes, from the beginning of the file -1 on error, sets errno, file pointer remains unchanged Parameters: fd: file descriptor offset: indicates relative or absolute location whence: How you would like to use lseek SEEK_SET, set file pointer to offset bytes from the beginning of the file SEEK_CUR, set file pointer to offset bytes from current location SEEK_END, set file pointer to offset bytes from the end of the file Copyright : University of Illinois CS 241 Staff 14

File: Seek Examples Random access Jump to any byte in a file Move to byte #16 newpos = lseek(fd, 16, SEEK_SET); Move forward 4 bytes newpos = lseek(fd, 4, SEEK_CUR); Move to 8 bytes from the end newpos = lseek(fd, -8, SEEK_END); Copyright : University of Illinois CS 241 Staff 15

Example (lseek()) c = (char *) malloc(100 * sizeof(char)); fd = open( foo.txt", O_RDONLY); if (fd < 0) { perror("r1"); exit(1); sz = read(fd, c, 10); printf("we have opened in1, and called read(%d, c, 10).\n", fd); c[sz] = '\0'; printf("those bytes are as follows: %s\n", c); i = lseek(fd, 0, SEEK_CUR); printf("lseek(%d, 0, SEEK_CUR) returns that the current offset is %d\n\n", fd, i); printf("now, we seek to the beginning of the file and call read(%d, c, 10)\n", fd); lseek(fd, 0, SEEK_SET); sz = read(fd, c, 10); c[sz] = '\0'; printf("the read returns the following bytes: %s\n", c); Copyright : University of Illinois CS 241 Staff 16

Stream Processing - fscanf() int scanf(const char *format,... ); Read from the standard input stream stdin Stores read characters in buffer pointed to by s. Return Number of successfully matched and assigned input items EOF on error int fscanf(file *stream, const char *fmt,... ); Read from the named input stream int sscanf(const char *s, const char *fmt,... ); Read from the string s Copyright : University of Illinois CS 241 Staff 17

Example: (scanf()) Input: 56789 56a72 #include <stdio.h> int main() { int i; float x; What are i, x, and name after the call to scanf()? char name[50]; scanf("%2d%f %[0123456789]", &i, &x, name); Copyright : University of Illinois CS 241 Staff 18