SlideShare a Scribd company logo
UNIT I: FILE I/O
Systems Programming
• Systems programming involves the development of the
individual pieces of software that allow the entire
system to function as a single unit.
• Systems programming involves many layers such as the
operating system (OS), firmware, and the development
environment.
• System programming concepts are varies based on
operating systems.
• Example:
– Windows OSWindows System programming
– Linux OSLinux System programming
– MAC OSMac System Programming
Cornerstones of System Programming
in LINUX
• system calls:
– System programming starts and ends with system
calls
– System calls are function invocations made from
user space—your text editor, favorite game, and
so on to requests a service from the kernel( core
part of OS)
Cornerstones of System Programming
in LINUX
• the C library:
– On modern Linux systems, the C library is provided
by GNU libc, abbreviated glibc.
– The GNU C Library project provides the core
libraries for the GNU system
– GNU is an operating system that is free software
Cornerstones of System Programming
in LINUX
• The C Compiler
– In Linux, the standard C compiler is provided by
the GNU Compiler Collection (gcc).
– A compiler is a special program that processes
statements written in a particular programming
language and turns them into machine language
or "code" that a computer's processor uses.
APIs & ABIs
• API
– It stands for Application Programming Interface
– It is a software intermediary that allows two applications to talk
to each other.
– It is based on source code
• ABI
– It stands for Application binary interface
– an ABI defines the binary interface between two or more pieces
of software on a particular architecture
– It defines how an application interacts with itself, how an
application interacts with the kernel, and how an application
interacts with libraries.
– It is based on machine code/object code
Opening Files
• A file is opened and a file descriptor is obtained
with the open() system call.
• Syntax:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open (const char *name, int flags);/opens existing file
int open (const char *name, int flags, mode_t mode);/it
will create a new file if it does not exist/
Header Files
Opening Files
• int open (const char *name, int flags);
File name with path Specifies mode
int open (const char *name, int flags, mode_t mode);
File name with path O_CREAT Specifies mode
Opening Files
• The following are some of values for flag and
mode parameters:
1.O_RDONLY-----Open for reading only.
2.O_WRONLY-----Open for writing only.
3.O_RDWR---------Open for reading and writing.
4. O_CREAT---------Create the file if it doesn't exist.
5.O_EXCL----When used with O_CREAT, if the file already
exists it is an error and the open() will fail
6. O_APPEND------Open the file in append mode
Opening Files
• Example program:
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
int main()
{
// if file does not have in directory
// then file foo.txt is created.
int errno;
int fd = open("foo.txt", O_RDONLY);
printf("fd = %d/n", fd);
if (fd ==-1)
{
// print which type of error have in a code
printf("Error Number % dn", errno);
// print program detail "Success or failure"
perror("Program");
}
return 0;
}
Header files
Main function
Opening Files
• Fd:
– It stands for file descriptor
– File descriptor is integer that uniquely identifies an opened
file.
– A file descriptor is a non-negative integer, generally
represented in the C programming language as the type int
• Most of the functionsdeclared are in
the <fcntl.h> header ,<stdio.h> and <errno.h>
• Return 0 is the statement which returns 0 on the
success after finishing the program execution
and returning a non-zero number means failure
Opening Files
• Linux is the multi-user operating system which
can be accessed by many users simultaneously.
• But this raises security concerns as an unsolicited
user can corrupt, change or remove crucial data.
• For effective security, Linux divides authorization
into 2 levels:
– Ownership
– Permission
Opening Files
• Ownership:
– Every file and directory on your Unix/Linux system is
assigned 3 types of owner, given below:
• User
– A user is the owner of the file. By default, the person who created
a file becomes its owner. Hence, a user is also sometimes called
an owner.
• Group
– A user- group can contain multiple users. All users belonging to a
group will have the same access permissions to the file.
– Suppose you have a project where a number of people require
access to a file.
– Instead of manually assigning permissions to each user, you could
add all users to a group, and assign group permission to file such
that only this group members and no one else can read or modify
the files.
• Other
– Any other user who has access to a file
– Practically, it means everybody else.
Opening Files
• Permissions
– Every file and directory in your UNIX/Linux system
has following 3 permissions defined for all the 3
owners discussed above.
– The three permissions are
• Read:This permission give you the authority to open
and read a file
• Write:The write permission gives you the authority to
modify the contents of a file.
• Execute:This permission give you the authority to
execute a file
Opening Files
• Example for file permission:
Opening Files
• The creat() Function/System call:
– Create and open a file
• Syntax:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat (const char *name, mode_t mode);
Header files
Name of file
Mode
Opening Files
• Return Values and Error Codes
– Both open() and creat() return a file descriptor on
success.
– On error, both return −1, and set errno to an
appropriate error value
Reading via read()
• The mechanism used for reading is the read()
system call.
• Syntax:
#include <unistd.h>
ssize_t read (int fd, void *buf, size_t len);
File descriptor Address of
first Byte
Length or count
Reading via read()
• When read() returns successfully, its return
value is the number of bytes actually read and
placed in the buffer.
• If len(third parameter) is zero, read returns
zero and has no other results.
• On success a non-negative integer is returned
indicating the number of bytes actually read.
• Otherwise, a -1 is returned.
Reading via read()
• A call to read() can result in many possibilities:
– The call returns a value equal to len
– The call returns a value less than len, but greater
than zero(if any interrupt occurs during read)
– The call returns 0. This indicates EOF. There is
nothing to read.
– The call blocks because no data is currently
available
– The call returns −1, and errno is set to EINTR or
EAGAIN.
Reading via read()
Nonblocking Reads
• By default, read() waits until at least one byte is
available to return to the application; this default
is called "blocking" mode.
• Alternatively, individual file descriptors can be
switched to "non-blocking" mode, which means
that a read() on a slow file will return
immediately, even if no bytes are available.
• This is called nonblocking I/O; it allows
applications to perform I/O, potentially on
multiple files, without ever blocking
Reading via read()
Other Error Values
• Possible errno values after a failure on read() include:
– EBADF
• The given file descriptor is invalid or is not open for reading.
– EFAULT
• The pointer provided by buf is not inside the calling process’s
address space.
– EINVAL
• The file descriptor is mapped to an object that does not allow
reading.
– EIO
• A low-level I/O error occurred.
Reading via read()
Size Limits on read()
• The size_t and ssize_t types are used for read()
• The size_t type is used for storing values used to
measure size in bytes.
• The ssize_t type is a signed version of size_t (the
negative values are used to connote errors).
• the maximum value of an ssize_t is SSIZE_MAX
which is 2,147,483,647 bytes on a 32- bit
machine
Writing with write()
• The most basic and common system call used
for writing is write().
• It writes data from a buffer declared by the
user to a given device, such as a file
• This is the primary way to output data from a
program by directly using a system call
• Syntax:
#include <unistd.h>
ssize_t write (int fd, const void *buf, size_t count);
File descriptor
Destination
Source Length of the data to
be written
Writing with write()
• On success, the number of bytes written is
returned.
• On error, −1 is returned and errno is set
appropriately
• Partial writes:
– a successful write() may transfer fewer than count
bytes.
– Such partial writes can occur for various reasons
• If there was insufficient space on the disk device to write all
of the requested bytes
• Because of interrupt
Writing with write()
• Append Mode:
– When fd is opened in append mode (via O_APPEND),
writes do not occur at the file descriptor’s current file
position. Instead, they occur at the current end of the
file.
• Nonblocking Writes
– When fd is opened in nonblocking mode (via
O_NONBLOCK), and the write as issued would
normally block, the write() system call returns −1 and
sets errno to EAGAIN
– The request should be reissued later
Writing with write()
• Other Error Codes
– EBADF:The given file descriptor is not valid or is
not open for writing
– EFAULT:The pointer provided by buf points outside
of the process’s address space
– EINVAL:The given file descriptor is mapped to an
object that is not suitable for writing
– ENOSPC:The filesystem backing the given file
descriptor does not have sufficient space.
Writing with write()
• Size Limits on write()
– If count is larger than SSIZE_MAX, the results of the call to
write() are undefined
– A call to write() with a count of zero results in the call
returning immediately with a return value of 0.
• Behavior of write()
– when a user-space application issues a write() system call,
the Linux kernel performs a few checks and then simply
copies the data into a buffer.
– Later, in the background,the kernel gathers up all of the
dirty buffers, which are buffers that contain data newer
than what is on disk, sorts them optimally, and writes
them out to disk
Synchronized I/O
User issues write() to write some
data which contains three lines to file
called Sample.txt located in your z
drive
Kernel or OS accept the request and collect data from
write
Time frame1:data is collected to buffer
Time Frame2:data is collected to buffer
Time Frame 3:data is Collected to buffer
And then
It will sort the data in buffers and in optimal time the
consolidated data will be moved to sample.txt located
in your z drive
request
Process
Synchronized I/O
• By default, the Linux kernel writes data to disk
asynchronously.
• Writes are buffered (cached) in memory, and
written to the storage device at the optimal
time.
• The Synchronous I/O provides some functions
to ensure that all operations are finish before
they return.
Synchronized I/O
• Sync System call:
– The sync system call forces an immediate write of all
cached data to disk but it doesn’t wait to complete.
– This call initiates the process of committing all buffers
to disk.
• Syntax:
sync [OPTION] [FILE]...
• Examples:
1. sync -d: This option sync only file data not
metadata
2. sync -f: This option will sync the file systems
which contains the files.
Synchronized I/O
fsync() and fdatasync()
• Syntax of fsync()
#include <unistd.h>
int fsync (int fd);
• The call to above function ensures that all dirty data
associated with the file mapped by the file descriptor
fd are written back to disk.
• The file descriptor fd must be open for writing.
• The call writes back both data and metadata, such as
creation timestamps and other attributes
• It will not return until the hard drive says that the data
and metadata are on the disk
• Metadata summarizes basic information about data.
Synchronized I/O
fsync() and fdatasync()
• Syntax for fdatasync()
#include <unistd.h>
int fdatasync (int fd);
• A call to fdata sync() will flush a file’s size,
since you need that to read the file correctly.
• The call does not guarantee that nonessential
metadata is synchronized to disk, and is
therefore potentially faster
Synchronized I/O
O_SYNC Flag and O_RSYNC
• O_SYNC requires that any write operations
block until all data and all metadata have been
written to persistent storage.
• The O_RSYNC flag specifies that only normal
data be synchronized after each write
operation, not metadata.
Direct I/O
• The Linux kernel, like any modern operating system
kernel, implements a complex layer of caching,
buffering, and I/O management between devices and
applications.
• A high-performance application may wish to bypass
this layer of complexity and perform its own I/O
management.
• Providing the O_DIRECT flag to open() instructs the
kernel to minimize the presence of I/O management.
• When this flag is provided, I/O will initiate directly from
user-space buffers to the device, bypassing the page
cache
Direct I/O
• All I/O will be synchronous; operations will not
return until completed.
• For an I/O operation to be performed as direct
I/O, it must meet certain alignment criteria.
• The alignment constraints are usually determined
by the disk driver, the disk controller, and the
system memory management hardware and
software.
• If a request fails to meet the alignment
constraints for direct I/O, the request is
performed as data synchronous I/O.
Closing Files
• After a program has finished working with a file
descriptor, it can unmap the file descriptor from the
associated file via the close() system call:
#include <unistd.h>
int close (int fd);
• A call to close() unmaps the open file descriptor fd and
disassociates the file from the process.
• It is a common mistake to not check the return value of
close().
• There are a handful of possible errno values on failure.
Other than EBADF the most important error value is
EIO, indicating a low-level I /O error probably unrelated
to the actual close
Seeking with lseek()
• lseek is a system call that is used to change
the location of the read/write pointer of a file
descriptor.
• Syntax:
#include <sys/types.h>
#include <unistd.h>
off_t lseek (int fd, off_t pos, int origin);
Seeking with lseek()
• SEEK_CUR
– The current file position of fd is set to its current value
plus pos, which can be negative, zero, or positive. A
pos of zero returns the current file position value.
• SEEK_END
– The current file position of fd is set to the current
length of the file plus pos, which can be negative,
zero, or positive. A pos of zero sets the offset to the
end of the file.
• SEEK_SET
– The current file position of fd is set to pos. A pos of
zero sets the offset to the beginning of the file.
Seeking with lseek()
Error Values
• EBADF:The given file descriptor does not refer to
an open file descriptor
• EINVAL:The value given for origin is not one of
SEEK_SET, SEEK_CUR, or SEEK_END, or the
resulting file position would be negative.
• EOVERFLOW:The resulting file offset cannot be
represented in an off_t.
• ESPIPE:The given file descriptor is associated with
an unseekable object, such as a pipe,FIFO, or
socket.
Positional Reads and Writes
• In lieu of using lseek(), Linux provides two variants of
the read() and write() system calls
• Both receive the file position from which to read or write
• Upon completion, they do not update the file position
• The read form is called pread():
#define _XOPEN_SOURCE 500
#include <unistd.h>
ssize_t pread (int fd, void *buf, size_t count, off_t pos);
• This call reads up to count bytes into buf from the file
descriptor fd at file position pos.
Positional Reads and Writes
• The write form is called pwrite():
#define _XOPEN_SOURCE 500
#include <unistd.h>
ssize_t pwrite (int fd, const void *buf, size_t count, off_t pos);
• This call writes up to count bytes from buf to the file
descriptor fd at file position pos.
Truncating Files
• Linux provides two system calls for truncating the
length of a file, both of which are defined and required
(to varying degrees) by various POSIX standards.
• They are:
#include <unistd.h>
#include <sys/types.h>
int ftruncate (int fd, off_t len);
• and:
#include <unistd.h>
#include <sys/types.h>
int truncate (const char *path, off_t len);
Truncating Files
• The most common use of these system calls is to
truncate a file to a size smaller than its current
length
• The ftruncate() system call operates on the file
descriptor given by fd, which must be open for
writing
• The truncate() system call operates on the
filename given by path, which must be writable.
• Both return 0 on success. On error, they return −1
and set errno as appropriate
Multiplexed I/O
• Multiplexed I/O allows an application to
concurrently block on multiple file descriptors and
receive notification when any one of them
becomes ready to read or write without blocking.
• With I/O multiplexing, we call select or poll and
block in one of these two system calls, instead of
blocking in the actual I/O system call.
Multiplexed I/O
• Select is a system call and application programming
interface (API) in Unix-like and POSIX-compliant
operating systems for examining the status of file
descriptors of open input/output channels
• Syntax:
int select (int n,
fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout);
Multiplexed I/O
Argument Description
n This is an integer one more than the maximum of any file
descriptor in any of the sets
*readfds file descriptors to be checked for being ready to read
*writefds file descriptors to be checked for being ready to Write
*exceptfds file descriptors to be checked for error conditions
*timeout specifies a maximum interval to wait for the selection to
complete
Multiplexed I/O
• Poll system call:
– Unlike select(), with its inefficient three bitmask-based
sets of file descriptors, poll() employs a single array of
nfds pollfd structures, pointed to by fds
• Syntax:
#include <poll.h>
int poll (struct pollfd *fds, nfds_t nfds, int timeout);
It contains the set events
which have to checked for
the availbility
Number of fds
Time limit
Multiplexed I/O
• Structure of pollfd *fds
#include <poll.h>
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events to watch */
short revents; /* returned events witnessed */
};
Kernel Internals
• The kernel subsystem consists of :
– the virtual filesystem (VFS),
– the page cache, and
– page writeback
• The virtual filesystem, occasionally also called a virtual
file switch, is a mechanism of abstraction that allows
the Linux kernel to call filesystem functions and
manipulate filesystem data without knowing—or even
caring about—the specific type of filesystem being
used.
• Linux file system is generally a built-in layer of a Linux
operating system used to handle the data management
of the storage. It helps to arrange the file on the disk
storage. It manages the file name, file size, creation
date, and much more information about a file.
Kernel Internals
• The Page Cache:
– The page cache is an in-memory store of recently
accessed data from an on-disk filesystem.
– Disk access is painfully slow, particularly relative to
today’s processor speeds.
– Storing requested data in memory allows the
kernel to fulfill subsequent requests for the same
data from memory, avoiding repeated disk access.
Kernel Internals
• Page Writeback
– Eventually the dirty buffers need to be committed to
disk, synchronizing the on-disk files with the data in
memory. This is known as writeback. It occurs in two
situations:
• When free memory shrinks below a configurable threshold,
dirty buffers are written back to disk so that the now-clean
buffers may be removed, freeing memory.
• When a dirty buffer ages beyond a configurable threshold,
the buffer is written back to disk. This prevents data from
remaining dirty indefinitely.

More Related Content

What's hot

Bootloaders
BootloadersBootloaders
Bootloaders
Anil Kumar Pugalia
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
Anil Kumar Pugalia
 
Linux process management
Linux process managementLinux process management
Linux process managementRaghu nath
 
Linux Programming
Linux ProgrammingLinux Programming
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
Emertxe Information Technologies Pvt Ltd
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
Houcheng Lin
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
Wave Digitech
 
Linux file system
Linux file systemLinux file system
Linux file system
Midaga Mengistu
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
Macpaul Lin
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
Linaro
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
Anil Kumar Pugalia
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
Liran Ben Haim
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
Tushar B Kute
 

What's hot (20)

Bootloaders
BootloadersBootloaders
Bootloaders
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Linux process management
Linux process managementLinux process management
Linux process management
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 

Similar to Linux System Programming - File I/O

Introduction to System Calls
Introduction to System CallsIntroduction to System Calls
Introduction to System Calls
Vandana Salve
 
Systems Programming - File IO
Systems Programming - File IOSystems Programming - File IO
Systems Programming - File IO
HelpWithAssignment.com
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
NiharikaDubey17
 
MODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxMODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptx
ManasaPJ1
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
YourHelper1
 
Linux Systems Programming: File Handling
Linux Systems Programming: File HandlingLinux Systems Programming: File Handling
Linux Systems Programming: File Handling
RashidFaridChishti
 
Data file handling
Data file handlingData file handling
Data file handling
Saurabh Patel
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
Shashwat Shriparv
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
Hatem Abd El-Salam
 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1
Pavan Illa
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Linux week 2
Linux week 2Linux week 2
Linux week 2
Vinoth Sn
 
Poking The Filesystem For Fun And Profit
Poking The Filesystem For Fun And ProfitPoking The Filesystem For Fun And Profit
Poking The Filesystem For Fun And Profit
ssusera432ea1
 
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Ahmed El-Arabawy
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
艾鍗科技
 
redhat_by_Cbitss.ppt
redhat_by_Cbitss.pptredhat_by_Cbitss.ppt
redhat_by_Cbitss.ppt
VikrantSChohaan
 
Linux: Everyting-as-a-service
Linux: Everyting-as-a-serviceLinux: Everyting-as-a-service
Linux: Everyting-as-a-service
Rohit Sansiya
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Ahmed El-Arabawy
 

Similar to Linux System Programming - File I/O (20)

Introduction to System Calls
Introduction to System CallsIntroduction to System Calls
Introduction to System Calls
 
Systems Programming - File IO
Systems Programming - File IOSystems Programming - File IO
Systems Programming - File IO
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 
MODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxMODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptx
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
Linux Systems Programming: File Handling
Linux Systems Programming: File HandlingLinux Systems Programming: File Handling
Linux Systems Programming: File Handling
 
Data file handling
Data file handlingData file handling
Data file handling
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Linux week 2
Linux week 2Linux week 2
Linux week 2
 
Poking The Filesystem For Fun And Profit
Poking The Filesystem For Fun And ProfitPoking The Filesystem For Fun And Profit
Poking The Filesystem For Fun And Profit
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
redhat_by_Cbitss.ppt
redhat_by_Cbitss.pptredhat_by_Cbitss.ppt
redhat_by_Cbitss.ppt
 
Linux: Everyting-as-a-service
Linux: Everyting-as-a-serviceLinux: Everyting-as-a-service
Linux: Everyting-as-a-service
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
 

Recently uploaded

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 

Recently uploaded (20)

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 

Linux System Programming - File I/O

  • 2. Systems Programming • Systems programming involves the development of the individual pieces of software that allow the entire system to function as a single unit. • Systems programming involves many layers such as the operating system (OS), firmware, and the development environment. • System programming concepts are varies based on operating systems. • Example: – Windows OSWindows System programming – Linux OSLinux System programming – MAC OSMac System Programming
  • 3. Cornerstones of System Programming in LINUX • system calls: – System programming starts and ends with system calls – System calls are function invocations made from user space—your text editor, favorite game, and so on to requests a service from the kernel( core part of OS)
  • 4. Cornerstones of System Programming in LINUX • the C library: – On modern Linux systems, the C library is provided by GNU libc, abbreviated glibc. – The GNU C Library project provides the core libraries for the GNU system – GNU is an operating system that is free software
  • 5. Cornerstones of System Programming in LINUX • The C Compiler – In Linux, the standard C compiler is provided by the GNU Compiler Collection (gcc). – A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses.
  • 6. APIs & ABIs • API – It stands for Application Programming Interface – It is a software intermediary that allows two applications to talk to each other. – It is based on source code • ABI – It stands for Application binary interface – an ABI defines the binary interface between two or more pieces of software on a particular architecture – It defines how an application interacts with itself, how an application interacts with the kernel, and how an application interacts with libraries. – It is based on machine code/object code
  • 7. Opening Files • A file is opened and a file descriptor is obtained with the open() system call. • Syntax: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (const char *name, int flags);/opens existing file int open (const char *name, int flags, mode_t mode);/it will create a new file if it does not exist/ Header Files
  • 8. Opening Files • int open (const char *name, int flags); File name with path Specifies mode int open (const char *name, int flags, mode_t mode); File name with path O_CREAT Specifies mode
  • 9. Opening Files • The following are some of values for flag and mode parameters: 1.O_RDONLY-----Open for reading only. 2.O_WRONLY-----Open for writing only. 3.O_RDWR---------Open for reading and writing. 4. O_CREAT---------Create the file if it doesn't exist. 5.O_EXCL----When used with O_CREAT, if the file already exists it is an error and the open() will fail 6. O_APPEND------Open the file in append mode
  • 10. Opening Files • Example program: #include<stdio.h> #include<fcntl.h> #include<errno.h> int main() { // if file does not have in directory // then file foo.txt is created. int errno; int fd = open("foo.txt", O_RDONLY); printf("fd = %d/n", fd); if (fd ==-1) { // print which type of error have in a code printf("Error Number % dn", errno); // print program detail "Success or failure" perror("Program"); } return 0; } Header files Main function
  • 11. Opening Files • Fd: – It stands for file descriptor – File descriptor is integer that uniquely identifies an opened file. – A file descriptor is a non-negative integer, generally represented in the C programming language as the type int • Most of the functionsdeclared are in the <fcntl.h> header ,<stdio.h> and <errno.h> • Return 0 is the statement which returns 0 on the success after finishing the program execution and returning a non-zero number means failure
  • 12. Opening Files • Linux is the multi-user operating system which can be accessed by many users simultaneously. • But this raises security concerns as an unsolicited user can corrupt, change or remove crucial data. • For effective security, Linux divides authorization into 2 levels: – Ownership – Permission
  • 13. Opening Files • Ownership: – Every file and directory on your Unix/Linux system is assigned 3 types of owner, given below: • User – A user is the owner of the file. By default, the person who created a file becomes its owner. Hence, a user is also sometimes called an owner. • Group – A user- group can contain multiple users. All users belonging to a group will have the same access permissions to the file. – Suppose you have a project where a number of people require access to a file. – Instead of manually assigning permissions to each user, you could add all users to a group, and assign group permission to file such that only this group members and no one else can read or modify the files. • Other – Any other user who has access to a file – Practically, it means everybody else.
  • 14. Opening Files • Permissions – Every file and directory in your UNIX/Linux system has following 3 permissions defined for all the 3 owners discussed above. – The three permissions are • Read:This permission give you the authority to open and read a file • Write:The write permission gives you the authority to modify the contents of a file. • Execute:This permission give you the authority to execute a file
  • 15. Opening Files • Example for file permission:
  • 16. Opening Files • The creat() Function/System call: – Create and open a file • Syntax: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int creat (const char *name, mode_t mode); Header files Name of file Mode
  • 17. Opening Files • Return Values and Error Codes – Both open() and creat() return a file descriptor on success. – On error, both return −1, and set errno to an appropriate error value
  • 18. Reading via read() • The mechanism used for reading is the read() system call. • Syntax: #include <unistd.h> ssize_t read (int fd, void *buf, size_t len); File descriptor Address of first Byte Length or count
  • 19. Reading via read() • When read() returns successfully, its return value is the number of bytes actually read and placed in the buffer. • If len(third parameter) is zero, read returns zero and has no other results. • On success a non-negative integer is returned indicating the number of bytes actually read. • Otherwise, a -1 is returned.
  • 20. Reading via read() • A call to read() can result in many possibilities: – The call returns a value equal to len – The call returns a value less than len, but greater than zero(if any interrupt occurs during read) – The call returns 0. This indicates EOF. There is nothing to read. – The call blocks because no data is currently available – The call returns −1, and errno is set to EINTR or EAGAIN.
  • 21. Reading via read() Nonblocking Reads • By default, read() waits until at least one byte is available to return to the application; this default is called "blocking" mode. • Alternatively, individual file descriptors can be switched to "non-blocking" mode, which means that a read() on a slow file will return immediately, even if no bytes are available. • This is called nonblocking I/O; it allows applications to perform I/O, potentially on multiple files, without ever blocking
  • 22. Reading via read() Other Error Values • Possible errno values after a failure on read() include: – EBADF • The given file descriptor is invalid or is not open for reading. – EFAULT • The pointer provided by buf is not inside the calling process’s address space. – EINVAL • The file descriptor is mapped to an object that does not allow reading. – EIO • A low-level I/O error occurred.
  • 23. Reading via read() Size Limits on read() • The size_t and ssize_t types are used for read() • The size_t type is used for storing values used to measure size in bytes. • The ssize_t type is a signed version of size_t (the negative values are used to connote errors). • the maximum value of an ssize_t is SSIZE_MAX which is 2,147,483,647 bytes on a 32- bit machine
  • 24. Writing with write() • The most basic and common system call used for writing is write(). • It writes data from a buffer declared by the user to a given device, such as a file • This is the primary way to output data from a program by directly using a system call • Syntax: #include <unistd.h> ssize_t write (int fd, const void *buf, size_t count); File descriptor Destination Source Length of the data to be written
  • 25. Writing with write() • On success, the number of bytes written is returned. • On error, −1 is returned and errno is set appropriately • Partial writes: – a successful write() may transfer fewer than count bytes. – Such partial writes can occur for various reasons • If there was insufficient space on the disk device to write all of the requested bytes • Because of interrupt
  • 26. Writing with write() • Append Mode: – When fd is opened in append mode (via O_APPEND), writes do not occur at the file descriptor’s current file position. Instead, they occur at the current end of the file. • Nonblocking Writes – When fd is opened in nonblocking mode (via O_NONBLOCK), and the write as issued would normally block, the write() system call returns −1 and sets errno to EAGAIN – The request should be reissued later
  • 27. Writing with write() • Other Error Codes – EBADF:The given file descriptor is not valid or is not open for writing – EFAULT:The pointer provided by buf points outside of the process’s address space – EINVAL:The given file descriptor is mapped to an object that is not suitable for writing – ENOSPC:The filesystem backing the given file descriptor does not have sufficient space.
  • 28. Writing with write() • Size Limits on write() – If count is larger than SSIZE_MAX, the results of the call to write() are undefined – A call to write() with a count of zero results in the call returning immediately with a return value of 0. • Behavior of write() – when a user-space application issues a write() system call, the Linux kernel performs a few checks and then simply copies the data into a buffer. – Later, in the background,the kernel gathers up all of the dirty buffers, which are buffers that contain data newer than what is on disk, sorts them optimally, and writes them out to disk
  • 29. Synchronized I/O User issues write() to write some data which contains three lines to file called Sample.txt located in your z drive Kernel or OS accept the request and collect data from write Time frame1:data is collected to buffer Time Frame2:data is collected to buffer Time Frame 3:data is Collected to buffer And then It will sort the data in buffers and in optimal time the consolidated data will be moved to sample.txt located in your z drive request Process
  • 30. Synchronized I/O • By default, the Linux kernel writes data to disk asynchronously. • Writes are buffered (cached) in memory, and written to the storage device at the optimal time. • The Synchronous I/O provides some functions to ensure that all operations are finish before they return.
  • 31. Synchronized I/O • Sync System call: – The sync system call forces an immediate write of all cached data to disk but it doesn’t wait to complete. – This call initiates the process of committing all buffers to disk. • Syntax: sync [OPTION] [FILE]... • Examples: 1. sync -d: This option sync only file data not metadata 2. sync -f: This option will sync the file systems which contains the files.
  • 32. Synchronized I/O fsync() and fdatasync() • Syntax of fsync() #include <unistd.h> int fsync (int fd); • The call to above function ensures that all dirty data associated with the file mapped by the file descriptor fd are written back to disk. • The file descriptor fd must be open for writing. • The call writes back both data and metadata, such as creation timestamps and other attributes • It will not return until the hard drive says that the data and metadata are on the disk • Metadata summarizes basic information about data.
  • 33. Synchronized I/O fsync() and fdatasync() • Syntax for fdatasync() #include <unistd.h> int fdatasync (int fd); • A call to fdata sync() will flush a file’s size, since you need that to read the file correctly. • The call does not guarantee that nonessential metadata is synchronized to disk, and is therefore potentially faster
  • 34. Synchronized I/O O_SYNC Flag and O_RSYNC • O_SYNC requires that any write operations block until all data and all metadata have been written to persistent storage. • The O_RSYNC flag specifies that only normal data be synchronized after each write operation, not metadata.
  • 35. Direct I/O • The Linux kernel, like any modern operating system kernel, implements a complex layer of caching, buffering, and I/O management between devices and applications. • A high-performance application may wish to bypass this layer of complexity and perform its own I/O management. • Providing the O_DIRECT flag to open() instructs the kernel to minimize the presence of I/O management. • When this flag is provided, I/O will initiate directly from user-space buffers to the device, bypassing the page cache
  • 36. Direct I/O • All I/O will be synchronous; operations will not return until completed. • For an I/O operation to be performed as direct I/O, it must meet certain alignment criteria. • The alignment constraints are usually determined by the disk driver, the disk controller, and the system memory management hardware and software. • If a request fails to meet the alignment constraints for direct I/O, the request is performed as data synchronous I/O.
  • 37. Closing Files • After a program has finished working with a file descriptor, it can unmap the file descriptor from the associated file via the close() system call: #include <unistd.h> int close (int fd); • A call to close() unmaps the open file descriptor fd and disassociates the file from the process. • It is a common mistake to not check the return value of close(). • There are a handful of possible errno values on failure. Other than EBADF the most important error value is EIO, indicating a low-level I /O error probably unrelated to the actual close
  • 38. Seeking with lseek() • lseek is a system call that is used to change the location of the read/write pointer of a file descriptor. • Syntax: #include <sys/types.h> #include <unistd.h> off_t lseek (int fd, off_t pos, int origin);
  • 39. Seeking with lseek() • SEEK_CUR – The current file position of fd is set to its current value plus pos, which can be negative, zero, or positive. A pos of zero returns the current file position value. • SEEK_END – The current file position of fd is set to the current length of the file plus pos, which can be negative, zero, or positive. A pos of zero sets the offset to the end of the file. • SEEK_SET – The current file position of fd is set to pos. A pos of zero sets the offset to the beginning of the file.
  • 40. Seeking with lseek() Error Values • EBADF:The given file descriptor does not refer to an open file descriptor • EINVAL:The value given for origin is not one of SEEK_SET, SEEK_CUR, or SEEK_END, or the resulting file position would be negative. • EOVERFLOW:The resulting file offset cannot be represented in an off_t. • ESPIPE:The given file descriptor is associated with an unseekable object, such as a pipe,FIFO, or socket.
  • 41. Positional Reads and Writes • In lieu of using lseek(), Linux provides two variants of the read() and write() system calls • Both receive the file position from which to read or write • Upon completion, they do not update the file position • The read form is called pread(): #define _XOPEN_SOURCE 500 #include <unistd.h> ssize_t pread (int fd, void *buf, size_t count, off_t pos); • This call reads up to count bytes into buf from the file descriptor fd at file position pos.
  • 42. Positional Reads and Writes • The write form is called pwrite(): #define _XOPEN_SOURCE 500 #include <unistd.h> ssize_t pwrite (int fd, const void *buf, size_t count, off_t pos); • This call writes up to count bytes from buf to the file descriptor fd at file position pos.
  • 43. Truncating Files • Linux provides two system calls for truncating the length of a file, both of which are defined and required (to varying degrees) by various POSIX standards. • They are: #include <unistd.h> #include <sys/types.h> int ftruncate (int fd, off_t len); • and: #include <unistd.h> #include <sys/types.h> int truncate (const char *path, off_t len);
  • 44. Truncating Files • The most common use of these system calls is to truncate a file to a size smaller than its current length • The ftruncate() system call operates on the file descriptor given by fd, which must be open for writing • The truncate() system call operates on the filename given by path, which must be writable. • Both return 0 on success. On error, they return −1 and set errno as appropriate
  • 45. Multiplexed I/O • Multiplexed I/O allows an application to concurrently block on multiple file descriptors and receive notification when any one of them becomes ready to read or write without blocking. • With I/O multiplexing, we call select or poll and block in one of these two system calls, instead of blocking in the actual I/O system call.
  • 46. Multiplexed I/O • Select is a system call and application programming interface (API) in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels • Syntax: int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  • 47. Multiplexed I/O Argument Description n This is an integer one more than the maximum of any file descriptor in any of the sets *readfds file descriptors to be checked for being ready to read *writefds file descriptors to be checked for being ready to Write *exceptfds file descriptors to be checked for error conditions *timeout specifies a maximum interval to wait for the selection to complete
  • 48. Multiplexed I/O • Poll system call: – Unlike select(), with its inefficient three bitmask-based sets of file descriptors, poll() employs a single array of nfds pollfd structures, pointed to by fds • Syntax: #include <poll.h> int poll (struct pollfd *fds, nfds_t nfds, int timeout); It contains the set events which have to checked for the availbility Number of fds Time limit
  • 49. Multiplexed I/O • Structure of pollfd *fds #include <poll.h> struct pollfd { int fd; /* file descriptor */ short events; /* requested events to watch */ short revents; /* returned events witnessed */ };
  • 50. Kernel Internals • The kernel subsystem consists of : – the virtual filesystem (VFS), – the page cache, and – page writeback • The virtual filesystem, occasionally also called a virtual file switch, is a mechanism of abstraction that allows the Linux kernel to call filesystem functions and manipulate filesystem data without knowing—or even caring about—the specific type of filesystem being used. • Linux file system is generally a built-in layer of a Linux operating system used to handle the data management of the storage. It helps to arrange the file on the disk storage. It manages the file name, file size, creation date, and much more information about a file.
  • 51. Kernel Internals • The Page Cache: – The page cache is an in-memory store of recently accessed data from an on-disk filesystem. – Disk access is painfully slow, particularly relative to today’s processor speeds. – Storing requested data in memory allows the kernel to fulfill subsequent requests for the same data from memory, avoiding repeated disk access.
  • 52. Kernel Internals • Page Writeback – Eventually the dirty buffers need to be committed to disk, synchronizing the on-disk files with the data in memory. This is known as writeback. It occurs in two situations: • When free memory shrinks below a configurable threshold, dirty buffers are written back to disk so that the now-clean buffers may be removed, freeing memory. • When a dirty buffer ages beyond a configurable threshold, the buffer is written back to disk. This prevents data from remaining dirty indefinitely.