3.1. File and Buffered IO
3.1. File and Buffered IO
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
• 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
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
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);