Linux IPC: Pipes and File I/O

Slides:



Advertisements
Similar presentations
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Advertisements

Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Kernel File Interface operating systems (or programming I/O in Unix)
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O   4. Standard I/O Library 5. Files and Directories 6. System Data.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
1 UNIX Systems Programming Interprocess communication.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
File IO and command line input CSE 2451 Rong Shi.
Files and Directories File types stat functions for file information
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
1 System-Level I/O. 2 Outline Unix I/O Reading File Metadata Sharing Files & I/O redirection Robust I/O Standard I/O Suggested Reading –10.1~10.3, 10.5~10.7,
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
Advanced Programming in the UNIX Environment Hop Lee.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
By C. Shing ITEC Dept Radford University
Unix Programming Environment
Operating Systems Moti Geva
Lecture 31: Introduction to File System
Systems Programming.
Lecture 11 File input/output
Week 12 - Wednesday CS222.
Copyright ©: University of Illinois CS 241 Staff
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
LINUX programming Unit-3 PPT Slides
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
Text and Binary File Processing
File Input and Output.
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
Henning Schulzrinne Columbia University
CSE 333 – Section 3 POSIX I/O Functions.
FILE I/O File Descriptors I/O Efficiency File Sharing
Operating System Hebrew University Spring 2009
Module 12 Input and Output
File I/O & UNIX System Interface
Week 12 - Wednesday CS222.
Professor Jodi Neely-Ritz University of Florida
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Linux IPC: Pipes and File I/O CE 513

Pipes Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2]) : creates a pipe and returns two file descriptors, fildes[0] and fildes[1] for reading and writing Example: Using a Pipe to Communicate with a Child Process The program forks a child process. After closing the read end of the pipe, parent process starts writing strings to the pipe. After closing the write end of the pipe, the child reads strings from the pipe. Include these first for the C Language! #include <stdlib.h> #include <stdio.h> #include <unistd.h>

Writer /* Write COUNT copies of MESSAGE to STREAM, pausing for a second between each. */ void writer (const char* message, int count, FILE* stream) { for (; count > 0; --count) { /* Write the message to the stream, and send it off immediately. */ fprintf (stream, "%s\n", message); fflush (stream); /* Snooze a while. */ sleep (1); }

Reader /* Read random strings from the stream as long as possible. */ void reader (FILE* stream) { char buffer[1024]; /* Read until we hit the end of the stream. fgets reads until either a newline or the end-of-file. */ while (!feof (stream) && !ferror (stream) && fgets (buffer, sizeof (buffer), stream) != NULL) fputs (buffer, stdout); }

Parent Writes & Child Reads int main () { int fds[2]; pid_t pid; /* Create a pipe and place the read and write file descriptors in that array */ pipe (fds); /* Fork a child process. */ pid = fork (); if (pid == (pid_t) 0) { FILE* stream; /* This is the child process. Close the write end of the file descriptor. */ close (fds[1]); /* Convert the read file descriptor to a FILE object, and read from it. */ stream = fdopen (fds[0], "r"); reader (stream); close (fds[0]); } else { /* This is the parent process. */ /* Close the read end of the file descriptor. */ /* Convert the write file descriptor to a FILE object, and write to it. */ stream = fdopen (fds[1], "w"); writer ("Hello, world.", 5, stream); } return 0;

Screenshot: pipe.c

popen and pclose To send data to or receive data from a program being run in a subprocess. The popen and pclose functions facilitates this paradigm by eliminating the need to harnes pipe, fork, dup2, exec, and fdopen.

Example: popen & pclose The call to popen creates a child process executing the sort command, The second argument, “w”, indicates that this process wants to write to the child process. The return value from popen is one end of a pipe; the other end is connected to the child process’s standard input. When the writing is done, pclose closes the child process’s stream and waits for the process to terminate, and returns its status value.

Example: popen & pclose #include <stdio.h> #include <unistd.h> int main () { FILE* stream = popen ("sort", "w"); fprintf (stream, "This is a test.\n"); fprintf (stream, "Hello, world.\n"); fprintf (stream, "My dog has fleas.\n"); fprintf (stream, "This program is great.\n"); fprintf (stream, "One fish, two fish.\n"); return pclose (stream); }

File types and structure regular file character special file block special file fifo socket symbolic link directory File structure Byte stream; no particular internal structure

Unbuffered I/O & buffered I/O read/write ->System calls File descriptor Not in ANSI C, but in POSIX.1 and XPG3 Buffered I/O Implemented in standard I/O library Stream -> a pointer to FILE

Basic I/O system calls File descriptor Basic I/O open/creat, close, read, write, lseek dup/dup2 fcntl ioctl

File descriptor File descriptor General steps of file operation A short non-negative integer int fd; (in <unistd.h>) STDIN_FILENO (0), STDOUT_FILENO (1), STDERR_FILENO (2) General steps of file operation open-read/write-[lseek]-close

Example /* a rudimentary example program */ #include <fcntl.h> main() { int fd, nread; char buf[1024]; /*open file “data” for reading */ fd = open(“data”, O_RDONLY); /* read in the data */ nread = read(fd, buf, 1024); /* close the file */ close(fd); }

Basic I/O open/creat, close, read, write, lseek dup/dup2 fcntl ioctl

Error handling UNIX’s style strerror & perror Return value “errno” variable( defined in /usr/include/errno.h) extern int errno; strerror & perror #include <string.h> char *strerror(int errnum); #include <stdio.h> void perror(const char *msg);

open/creat function Open and possibly create a file or device #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); (Return: a new file descriptor if success; -1 if failure)

Parameter “flags” “flags”: file access mode One of O_RDONLY, O_WRONLY or O_RDWR which request opening the file read-only, write-only or read/write, respectively, bitwise-or’d with zero or more of the following: ( All defined in /usr/include/fcntl.h) O_APPEND: the file is opened in append mode O_TRUNC: to truncate the opened file, if it previously existed. Data written to the file descriptor will replace previous contents of the file. O_CREAT: If the file does not exist it will be created. O_EXCL: When used with O_CREAT, if the file already exists it is an error and the open will fail. … “creat” function: equivalent to open with flags equal to O_CREAT|O_WRONLY|O_TRUNC

Parameter “mode” “mode”: specifies the permissions to use in case a new file is created.

The value of parameter “mode” Read, write and execute by others S_IRWXO 00007 Execute by others S_IXOTH 00001 Write by others S_IWOTH 00002 Read by others S_IROTH 00004 Read, write and execute by group S_IRWXG 00070 Execute by group S_IXGRP 00010 Write by group S_IWGRP 00020 Read by group S_IRGRP 00040 Read, write and execute by owner S_IRWXU(00700) Execute by owner S_IXUSR(00100) Write by owner S_IWUSR(00200) Read by owner S_IRUSR(00400) Permission Mode

Example: Open file #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> int main (int argc, char* argv[]) { /* The path at which to create the new file. */ char* path = argv[1]; /* The permissions for the new file. */ mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; /* Create the file. */ int fd = open (path, O_WRONLY | O_EXCL | O_CREAT, mode); if (fd == -1) { /* An error occurred. Print an error message and bail. */ perror ("open"); return 1; } return 0;

close function Close a file descriptor #include <unistd.h> int close(int fd); (Return: 0 if success; -1 if failure)

read/write function Read from a file descriptor #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); Write to a file descriptor ssize_t write(int fd, const void *buf, size_t count);

Example: Append a timestamp to a file #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <time.h> #include <unistd.h> /* Return a character string representing the current date and time. */ char* get_timestamp () { time_t now = time (NULL); return asctime (localtime (&now)); }

Example: Append a timestamp to a file int main (int argc, char* argv[]) { /* The file to which to append the timestamp. */ char* filename = argv[1]; /* Get the current timestamp. */ char* timestamp = get_timestamp (); /* Open the file for writing. If it exists, append to it; otherwise, create a new file. */ int fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666); /* Compute the length of the timestamp string. */ size_t length = strlen (timestamp); /* Write the timestamp to the file. */ write (fd, timestamp, length); /* All done. */ close (fd); return 0; }

Screenshot: append timestamp

Example: Hexdump a file #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> int main (int argc, char* argv[]) { unsigned char buffer[16]; size_t offset = 0; size_t bytes_read; int i; /* Open the file for reading. */ int fd = open (argv[1], O_RDONLY); /* Read from the file, one chunk at a time. Continue until read "comes up short", that is, reads less than we asked for. This indicates that we’ve hit the end of the file. */

Example: Hexdump a file do { /* Read the next line’s worth of bytes. */ bytes_read = read (fd, buffer, sizeof (buffer)); /* Print the offset in the file, followed by the bytes themselves. */ printf ("0x%06x : ", offset); for (i = 0; i < bytes_read; ++i) printf ("%02x ", buffer[i]); printf ("\n"); /* Keep count of our position in the file. */ offset += bytes_read; } while (bytes_read == sizeof (buffer)); /* All done. */ close (fd); return 0;

Screenshot: hexdump.c

Example: lseek #include <fcntl.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> // This can create a very LARGE file !!!! int main (int argc, char* argv[]){ int zero = 0; const int megabyte = 1024 * 1024; char* filename = argv[1]; size_t length = (size_t) atoi (argv[2]) * megabyte; /* Open a new file. */ int fd = open (filename, O_WRONLY | O_CREAT | O_EXCL, 0666); /* Jump to 1 byte short of where we want the file to end. */ lseek (fd, length - 1, SEEK_SET); /* Write a single 0 byte. */ write (fd, &zero, 1); /* All done. */ close (fd); return 0; }

Screenshot: create-large-file.c

lseek function Reposition read/write file offset #include <sys/types.h> #include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); (Return: the resulting offset location if success; -1 if failure) The directive “whence”: SEEK_SET: the offset is set to “offset” bytes SEEK_CUR: the offset is set to its current location plus “offset” bytes SEEK_END: the offset is set to the size of the file plus “offset” bytes

dup/dup2 function Duplicate a file descriptor File sharing: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); (Return: the new file descriptor if success; -1 if failure) File sharing: After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description and thus share file offset and file status flags

File sharing and Atomic operation Possible problem Example1: (Append data to a file) if (lseek(fd, 0L, SEEK_END) < 0) err_sys(“lseek error”); if (write(fd, buf, 100) != 100) err_sys(“write error”); Example2: (create a file) if ((fd = open(pathname, O_WRONLY)) < 0) if (errno == ENOENT) { if ((fd = creat(pathname, mode)) < 0) err_sys(“creat error”); } else err_sys(“open error”);

fcntl function Manipulate a file descriptor #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd, struct flock *lock); The operation is determined by “cmd”.

ioctl function Control devices #include <sys/ioctl.h> int ioctl(int d, int request, ...);

Other system calls Handling file attributes Handling directory stat/fstat/lstat, ... Handling directory

stat/fstat/lstat functions Get file status #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *file_name, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *file_name, struct stat *buf); (Return: 0 if success; -1 if failure)

struct stat struct stat { mode_t st_mode; /*file type & mode*/ ino_t st_ino; /*inode number (serial number)*/ dev_t st_rdev; /*device number (file system)*/ nlink_t st_nlink; /*link count*/ uid_t st_uid; /*user ID of owner*/ gid_t st_gid; /*group ID of owner*/ off_t st_size; /*size of file, in bytes*/ time_t st_atime; /*time of last access*/ time_t st_mtime; /*time of last modification*/ time_t st_ctime; /*time of lat file status change*/ long st_blksize; /*Optimal block size for I/O*/ long st_blocks; /*number 512-byte blocks allocated*/ };

Test macros for file types Defined in <sys/stat.h> socket S_ISSOCK() symbolic link S_ISLNK() fifo S_ISFIFO() block special file S_ISBLK() character special file S_ISCHAR() directory S_ISDIR() regular file S_ISREG() File type Macro

File permission (cont’d) Saved-text bit (sticky bit) S_ISVTX(01000) Set group ID on execution S_ISGID(02000) Set user ID on execution S_ISUID(04000) Operation st_mode Illustrating set-uid/set-gid/saved-text bit e.g. passwd(1) command, /tmp Example: testing file permission if (buf.st_mode & S_IRUSR) printf(“readable by owner”); else printf(“unreadable by owner”);

access function Parameter “mode” Example #include <unistd.h> int access(const char *pathname, int mode); (Return: 0 if success; -1 if failure) Parameter “mode” R_OK, W_OK, X_OK, F_OK Example access.c (in APUE)

chmod/fchmod functions Change permissions of a file #include <sys/types.h> #include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fildes, mode_t mode); (Return: 0 if success; -1 if failure)

chown/fchown/lchown functions Change ownership of a file #include <sys/types.h> #include <unistd.h> int chown(const char *path, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *path, uid_t owner, gid_t group); (Return: 0 if success; -1 if failure)

link/unlink functions Create a new link to (make a new name for) a file. #include <unistd.h> int link(const char *oldpath, const char *newpath); (Return: 0 if success; -1 if failure) Delete a name and possibly the file it refers to. int unlink(const char *pathname);

symlink/readlink Create a symbolic link (named newpath which contains the sting “oldpath”) #include <unistd.h> int symlink(const char *oldpath, const char *newpath); (Return: 0 if success; -1 if failure) Read value of a symbolic link int readlink(const char *path, char *buf, size_t bufsiz); (Return: the count of characters placed in the buffer if success; -1 if failure)

Handling directories mkdir/rmdir chdir/fchdir, getcwd Read a directory opendir/closedir readdir telldir seekdir

mkdir/rmdir functions #include <sys/stat.h> #include <sys/types.h> int mkdir(const char *pathname, mode_t mode); (Return: 0 if success; -1 if failure) #include <unistd.h> int rmdir(const char *pathname);

chdir/fchdir functions Change working directory #include <unistd.h> int chdir(const char *path); int fchdir(int fd); (Return: 0 if success; -1 if failure)

getcwd function Get working directory #include <unistd.h> char *getcwd(char *buf, size_t size); The getcwd() function copies an absolute pathname of the current working directory to the array pointed to by buf, which is of length size.

Read a directory Data structures Operations DIR, struct dirent opendir/closedir readdir telldir seekdir

Folder Data structures DIR The data type of directory stream objects in <dirent.h> typedef struct __dirstream DIR; struct dirent Directory item Defined in <dirent.h> ino_t d_ino; /* inode number */ char d_name[NAME_MAX + 1]; /* file name */

Directory Operations #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); int closedir(DIR *dir); struct dirent *readdir(DIR *dir); off_t telldir(DIR *dir); // return current location in directory stream void seekdir(DIR *dir, off_t offset);

A directory scanning program DIR *dp; struct dirent *entry; if ( (dp = opendir(dir)) == NULL ) err_sys(…); while ( (entry = readdir(dp)) != NULL ) { lstat(entry->d_name, &statbuf); if ( S_ISDIR(statbuf.st_mode) ) … else } closedir(dp);

Standard I/O library File stream Standard I/O functions

File stream Buffered I/O Stream and “FILE” structure FILE* fp; Predefined pointer: stdin, stdout, stderr

Standard I/O functions Stream open/close Stream read/write Stream reposition Stream flush

Stream open/close Open a stream Parameter “mode” #include <stdio.h> FILE *fopen(const char *filename, const char *mode); int fclose(FILE *stream); Parameter “mode” “r”: Open text file for reading. “w”: Truncate file to zero length or create text file for writing. “a”: Open for appending. “r+”: Open for reading and writing. “w+”: Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. “a+”: Open for reading and appending. The file is created if does not exist.

Stream open/close (cont’d) Close a stream #include <stdio.h> int fclose(FILE *fp); (Return: 0 if success; -1 if failure)

Input of a character getc, fgetc, getchar functions #include <stdio.h> int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void); Reads the next character from a stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. Three functions: ferro, feof, clearerr ungetc function: push a character back to a stream.

Output of a character putc, fputc, putchar functions #include <stdio.h> int putc(int c, FILE *fp); int fputc(int c, FILE *fp); int putchar(int c); (Return: the character if success; -1 if failure)

Input of a line of string fgets, gets functions #include <stdio.h> char *fgets(char *s, int size, FILE *stream); char *gets(char *s); fgets: reads in at most size-1 characters from stream and stores them into the buffer pointed by s. Reading stops after an EOF or a new line. A ‘\0’ character is stored at the end of the buffer.

Output of a line of string fputs, puts functions #include <stdio.h> int fputs(const char *s, FILE *stream); int puts(const char *s);

Binary stream input/output fread/fwrite functions #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); (Return: the number of a items successfully read or written.)

Binary stream input/output (cont’d) Application: Read/write a binary array: float data[10]; if ( fwrite(&data[2], sizeof(float), 4, fp) != 4 ) err_sys(“fwrite error”); Read/write a structure struct { short count; long total; char name[NAMESIZE]; }item; if ( fwrite(&item, sizeof(item), 1, fp) != 1)

Formatted I/O scanf, fscanf, sscanf functions #include <stdio.h> int scanf(const char *format, ...); int fscanf(FILE *stream, const char *format, ...); int sscanf(const char *str, const char *format, ...);

Formatted I/O (cont’d) printf, fprintf, sprintf functions #include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, ...); int sprintf(char *str, const char *format, ...);

Reposition a stream fseek, ftell, rewind functions #include <stdio.h> int fseek(FILE *stream, long int offset, int whence); long ftell(FILE *stream); void rewind(FILE *stream); fgetpos, fsetpos functions ( Introduced in ANSI C) int fgetpos(FILE *fp, fpos_t *pos); int fsetpos(FILE *fp, const fpos_t *pos);

Flush a stream #include <stdio.h> int fflush(FILE *stream);

Stream buffering operations Three types of buffering block buffered (fully buffered) line buffered unbuffered setbuf, setvbuf functions #include <stdio.h> void setbuf(FILE *stream, char *buf); int setvbuf(FILE *stream, char *buf, int mode, size_t size);