Download presentation
1
Linux IPC: Pipes and File I/O
CE 513
2
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>
3
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); }
4
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); }
5
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;
6
Screenshot: pipe.c
7
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.
8
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.
9
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); }
10
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
11
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
12
Basic I/O system calls File descriptor Basic I/O
open/creat, close, read, write, lseek dup/dup2 fcntl ioctl
13
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
14
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); }
15
Basic I/O open/creat, close, read, write, lseek dup/dup2 fcntl ioctl
16
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);
17
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)
18
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
19
Parameter “mode” “mode”: specifies the permissions to use in case a new file is created.
20
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
21
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;
22
close function Close a file descriptor #include <unistd.h>
int close(int fd); (Return: 0 if success; -1 if failure)
23
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);
24
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)); }
25
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; }
26
Screenshot: append timestamp
27
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. */
28
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;
29
Screenshot: hexdump.c
30
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; }
31
Screenshot: create-large-file.c
32
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
33
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
34
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”);
35
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”.
36
ioctl function Control devices #include <sys/ioctl.h>
int ioctl(int d, int request, ...);
37
Other system calls Handling file attributes Handling directory
stat/fstat/lstat, ... Handling directory
38
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)
39
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*/ };
40
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
41
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”);
42
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)
43
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)
44
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)
45
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);
46
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)
47
Handling directories mkdir/rmdir chdir/fchdir, getcwd Read a directory
opendir/closedir readdir telldir seekdir
48
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);
49
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)
50
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.
51
Read a directory Data structures Operations DIR, struct dirent
opendir/closedir readdir telldir seekdir
52
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 */
53
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);
54
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);
55
Standard I/O library File stream Standard I/O functions
56
File stream Buffered I/O Stream and “FILE” structure FILE* fp;
Predefined pointer: stdin, stdout, stderr
57
Standard I/O functions
Stream open/close Stream read/write Stream reposition Stream flush
58
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.
59
Stream open/close (cont’d)
Close a stream #include <stdio.h> int fclose(FILE *fp); (Return: 0 if success; -1 if failure)
60
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.
61
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)
62
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.
63
Output of a line of string
fputs, puts functions #include <stdio.h> int fputs(const char *s, FILE *stream); int puts(const char *s);
64
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.)
65
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)
66
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, ...);
67
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, ...);
68
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);
69
Flush a stream #include <stdio.h> int fflush(FILE *stream);
70
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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.