You are on page 1of 21

SYSTEM

PROGRAMMING
LECTURE # 4
SYSTEM CALL & FILE I/O

Course Code: CS 312


Course Instructor: Dr. Sarah Iqbal

1
AGENDA FOR TODAY

 How to invoke system calls with a wrapper functions


 How to invoke system calls without a wrapper functions
 File I/O Model
 Linux file structure & directories

2
FILE I/O MODEL
 One of the distinguishing features of the I/O model on UNIX systems is the
concept of universality of I/O. This means that the same system calls (open(),
read(), write(), close(), and so on) are used to perform I/O on all types of files,
including devices.
 The kernel translates the application’s I/O requests into appropriate filesystem
or device-driver operations that perform I/O on the target file or device. Thus,
a program employing these system calls will work on any type of file.
 The kernel essentially provides one file type: a sequential stream of bytes,
which, in the case of disk files, disks, and tape devices, can be randomly
accessed using the lseek() system call.
 Many applications and libraries interpret the newline character as terminating
one line of text and commencing another.
 UNIX systems have no end-of-file character; the end of a file is detected by a
read that returns no data. 3
FILE DESCRIPTORS

 The I/O system calls refer to open files using a file descriptor, a (usually small)
nonnegative integer.
 A file descriptor is typically obtained by a call to open(), which takes a
pathname argument specifying a file upon which I/O is to be performed.
 Normally, a process inherits three open file descriptors when it is started by the
shell: descriptor 0 is standard input, the file from which the process takes its
input; descriptor 1 is standard output, the file to which the process writes its
output; and descriptor 2 is standard error, the file to which the process writes
error messages and notification of exceptional or abnormal conditions.
 In an interactive shell or program, these three descriptors are normally
connected to the terminal.
 In the stdio library, these descriptors correspond to the file streams stdin,
stdout, and stderr.
4
THE STDIO LIBRARY

 Toperform file I/O, C programs typically employ I/O


functions contained in the standard C library.
 Thisset of functions, referred to as the stdio library,
includes fopen(), fclose(), scanf(), printf(), fgets(),
fputs(), and so on. The stdio functions are layered on
top of the I/O system calls (open(), close(), read(),
write(), and so on).

5
YOUR FIRST LINUX C PROGRAM

6
LINUX FILE STRUCTURE

 In Linux, almost everything is a file. This means that, in general,


programs can use disk files, serial ports, printers, and other
devices in the same way they would use a file.
 Mainly you need to use only five basic functions: open, close,
read, write, and ioctl.
 Directories are also a special sorts of files. In modern UNIX
versions, including Linux, even the superuser may not write to
them directly.
 All users ordinarily use the high-level opendir/readdir interface to
read directories without needing to know the system-specific
details of directory implementation.
7
DIRECTORIES
 As well as its contents, a file has a name and some properties, or
“administrative information”; that is, the file’s creation/modification date
and its permissions. The properties are stored in the file’s inode, a special
block of data in the file system that also contains the length of the file and
where on the disk it’s stored. The system uses the number of the file’s
inode; the directory structure just names the file for our benefit.
 A directory is a file that holds the inode numbers and names of other files.
Each directory entry is a link to a file’s inode; remove the filename and you
remove the link. (You can see the inode number for a file by using ln –i.)
Using the ln command, you can make links to the same file in different
directories.
 When you delete a file all that happens is that the directory entry for the
file is removed and the number of links to the file goes down by one. The
data for the file is possibly still available through other links to the same file.
When the number of links to a file (the number after the permissions in ls -l)
reaches zero, the inode and the data blocks it references are then no 8
longer in use and are marked as free.
DIRECTORIES

 Files are arranged in directories, which may also contain subdirectories.


These form the familiar file system hierarchy.

 The /home directory is itself a subdirectory of the root directory, /, which


sits at the top of the hierarchy and contains all of the system’s files in
subdirectories. The root directory normally includes /bin for system
programs (“binaries”), /etc for system configuration files, and /lib for
system libraries.

 Files that represent physical devices and provide the interface to those
devices are conventionally found in a directory called /dev.
9
FILES AND DEVICES

 Even hardware devices are very often represented (mapped) by files.


 Three important device files found in both UNIX and Linux are
/dev/console, /dev/tty, and /dev/null.
 /dev/console: This device represents the system console. Error messages
and diagnostics are often sent to this device.
 /dev/tty: The special file /dev/tty is an alias (logical device) for the
controlling terminal (keyboard and screen, or window) of a process, if it
has one.
 /dev/null: The /dev/null file is the null device. All output written to this
device is discarded. An immediate end of file is returned when the device
is read, and it can be used as a source of empty files by using the cp
command. Unwanted output is often redirected to /dev/null.
10
SYSTEM CALLS AND DEVICE DRIVERS
 You can access and control files and devices using a small number of
functions. These functions, known as system calls, are provided by UNIX
(and Linux) directly, and are the interface to the operating system itself.
 At the heart of the operating system, the kernel, are a number of device
drivers. These are a collection of low-level interfaces for controlling system
hardware.
 For example, a low-level hard disk device driver will only write whole
numbers of disk sectors at a time, but will be able to access any desired
disk block directly, because the disk is a random access device.
 To provide a similar interface, device drivers encapsulate all of the
hardware-dependent features. Idiosyncratic features of the hardware are
usually available through the ioctl (for I/O control) system call.
 Device files in /dev are used in the same way; they can be opened, read,
written, and closed. For example, the same open call used to access a 11
regular file is used to access a user terminal, a printer, or a tape drive.
SYSTEM CALLS AND DEVICE DRIVERS…

The low-level functions used to access the device drivers, the


system calls, include:
 open: Open a file or device
 read: Read from an open file or device
 write: Write to a file or device
 close: Close the file or device
 ioctl: Pass control information to a device driver
 Theioctl system call is used to provide some necessary
hardware-specific control (as opposed to regular input and
output), so its use varies from device to device.
12
LIBRARY FUNCTIONS
One problem with using low-level system calls directly for input and output is that
they can be very inefficient. Why? Well:
 There’s a performance penalty in making a system call. System calls are
therefore expensive compared to function calls because Linux has to switch
from running your program code to executing its own kernel code and back
again. It’s a good idea to keep the number of system calls used in a program
to a minimum and get each call to do as much work as possible, for example,
by reading and writing large amounts of data rather than a single character
at a time.
 The hardware has limitations that can impose restrictions on the size of data
blocks that can be read or written by the low-level system call at any one
time. For example, tape drives often have a block size, say 10k, to which they
can write. So, if you attempt to write an amount that is not an exact multiple
of 10k, the drive will still advance the tape to the next 10k block, leaving gaps
on the tape.
To provide a higher-level interface to devices and disk files, a Linux distribution (and
UNIX) provides a number of standard libraries. These are collections of functions that 13
you can include in your own programs to handle these problems.
LIBRARY FUNCTIONS

14
LOW-LEVEL FILE ACCESS

Each running program, called a process, has a number of file


descriptors associated with it. These are small integers that you can
use to access open files or devices. When a program starts, it
usually has three of these descriptors already opened. These are:

 0: Standard input
 1: Standard output
 2: Standard error

15
WRITE SYSTEM CALL

16
READ SYSTEM CALL

17
OPEN SYSTEM CALL

18
OPEN SYSTEM CALL

19
CLOSE SYSTEM CALL

20
Reading material:

The Linux Programming interface: A Linux and UNIX® System


Programming Handbook by Michael KerrisK
Chapter 2: FUNDAMENTAL CONCEPTS, Section 2.5
Beginning Linux® Programming 4th Edition By Neil Matthew &
Richard Stones
Chapter 3: Working with Files

21

You might also like