0% found this document useful (0 votes)
58 views25 pages

3.1. File and Buffered IO

The document discusses low-level I/O in Linux, including kernel system calls like open(), read(), write(), and close() that operate on file descriptors. It also covers standard/buffered I/O functions from the C library like fopen(), fread(), fwrite(), and FILE pointers. Finally, it mentions concepts like non-blocking I/O, partial writes, seek operations, and reading/writing file metadata with stat().

Uploaded by

Alimohd Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views25 pages

3.1. File and Buffered IO

The document discusses low-level I/O in Linux, including kernel system calls like open(), read(), write(), and close() that operate on file descriptors. It also covers standard/buffered I/O functions from the C library like fopen(), fread(), fwrite(), and FILE pointers. Finally, it mentions concepts like non-blocking I/O, partial writes, seek operations, and reading/writing file metadata with stat().

Uploaded by

Alimohd Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Low Level I/O

in LINUX
Low level I/O
• Standard/Buffered I/O - C library
– printf/scanf/gets/puts/getc/putc
– fopen/fread/fwrite/fseek/fclose/fgets/fputc
– FILE* , file pointer

• Kernel – low level functions


– open/read/write/close/lseek
– File descriptor
• (1024 per process , can be get/set through getrlimit/setrlimit)
Kernel System Calls
• Linux Process Model
• Process Image
UNIX Process State Transition Diagram
open()

int open(const char *pathname, int flags, mode_t mode);


– Flags: O_RDONLY, O_WRONLY, or O_RDWR , O_APPEND,
O_ASYNC (Enable signal-driven I/O)
– Mode: S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH .. etc
R = read, W =write , X =execute

Owner of New File?


Permissions on new file?
Create()
ssize_t read (int fd, void *buf, size_t len);
Non Blocking Read
• If file was opened with O_NONBLOCK then
check EAGAIN

• Other Errors?
• Read size Limits?
ssize_t write (int fd, const void *buf, size_t count);
Partial writes
0_file_timestamp.c
char* get_timestamp ()
% ./0_file_timestamp tsfile
{ % cat tsfile
time_t now = time (NULL); Thu Feb 1 23:25:20 2001
return asctime (localtime (&now)); % ./ 0_file_timestamp tsfile
} % cat tsfile
int main (int argc, char* argv[]) Thu Feb 1 23:25:20 2001
Thu Feb 1 23:25:47 2001
{
char* filename = argv[1];
char* timestamp = get_timestamp ();
int fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666);
size_t length = strlen (timestamp);
write (fd, timestamp, length);
close (fd);
return 0;
}
write
• Append Mode?
• Other possible errors?
• Size limit?
• Kernel writing process?
hint : Transferring file to USB

• What is Direct I/O ?


Seek
• off_t lseek (int fd, off_t pos, int origin);
– 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.
Creating large file with lseek
int main (int argc, char* argv[]) % ./lseek-huge bigfile 15
{ % ls -l bigfile
-rw-r----- 1 samuel samuel ….
int zero = 0;
const int megabyte = 1024 * 1024;
char* filename = argv[1];
size_t length = (size_t) atoi (argv[2]) * megabyte;
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 (fd, &zero, 1);
close (fd);
return 0;
}
Positional Read/Write
ssize_t pread (int fd, void *buf, size_t count, off_t pos);

ssize_t pwrite (int fd, const void *buf, size_t count, off_t pos);
Reading Assignment
• How linux/unix kernel implements I/O?
– VFS
– Page Cache
– Page Writeback
Buffered/Standard I/O
• Why kernel buffer I/O in kernel space?
• Why buffering at user space?
• Block size?
– 512,1024,2k,4k

_IONBF (Unbuffered) , _IOLBF (Line-buffered) , _IOFBF (Block-buffered)


int setvbuf (FILE *stream, char *buf, int mode, size_t size);
Standard C I/O Functions
• #include <stdio.h>
• File Pointer (FILE *fp)
FILE * fopen (const char *path, const char *mode);
r, r+, w, w+, a, a+ , append ‘b’ for binary files

FILE * fdopen (int fd, const char *mode);

int fclose (FILE *stream);


int fcloseall (void);
Standard C I/O Functions
int fgetc (FILE *stream);
int ungetc (int c, FILE *stream);
int fputc (int c, FILE *stream);

char * fgets (char *str, int size, FILE *stream);


int fputs (const char *str, FILE *stream);

size_t fread (void *buf, size_t size, size_t nr, FILE *stream);
size_t fwrite (void *buf, size_t size, size_t nr, FILE *stream);

int fflush (FILE *stream); // fsync( ) kernel to file


Standard C I/O Functions
int fseek (FILE *stream, long offset, int whence);
void rewind (FILE *stream);
int fsetpos (FILE *stream, fpos_t *pos);

long ftell (FILE *stream);


int fgetpos (FILE *stream, fpos_t *pos);
Assignment
• Write a program to copy file at backup folder.
– Image jpeg/png
– Compressed zip/rar
– Word file
Concurrent I/O Calls
• Thread safety
• Manual file locking and unlocking
– void flockfile (FILE *stream);
– void funlockfile (FILE *stream);
– int ftrylockfile (FILE *stream);
• How to test?
– Assignments after multithreading topic
Unlocked Stream Operations
#define _GNU_SOURCE
#include <stdio.h>
int fgetc_unlocked (FILE *stream);
char *fgets_unlocked (char *str, int size, FILE *stream);
size_t fread_unlocked (void *buf, size_t size, size_t nr, FILE *stream);
int fputc_unlocked (int c, FILE *stream);
int fputs_unlocked (const char *str, FILE *stream);
size_t fwrite_unlocked (void *buf, size_t size, size_t nr,FILE *stream);
int fflush_unlocked (FILE *stream);
int feof_unlocked (FILE *stream);
int ferror_unlocked (FILE *stream);
int fileno_unlocked (FILE *stream);
void clearerr_unlocked (FILE *stream);
Files and Their Metadata
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat (const char *path, struct stat *buf);


int fstat (int fd, struct stat *buf);
int lstat (const char *path, struct stat *buf);
struct stat
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* permissions */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* last access time */
time_t st_mtime; /* last modification time */
time_t st_ctime; /* last status change time */
};

You might also like