You are on page 1of 3

EX NO: 4

DATE: I/O SYSTEM CALLS OF UNIX

AIM:
To create, open, rename and delete a file using I/O system calls of UNIX.

THEORY:

I/O System Calls

The basic I/O System Calls of Unix are open, create, close, read, lseek, write,
rename and unlink.

Open System Call


The open system call is used for opening the file if the file already exists
or creates a file if the file does not exists before. The prototype is

#include <fcntl.h>
int open(const char *path, int oflag);

The return value is the descriptor, -1 if the file could not be opened. The first parameter is
path name of the file to be opened and the second parameter is the opening mode
specified by bitwise ORing one or more of the following values
Value Meaning
O_RDONLY Open for reading only
O_WRONLY Open for writing only
O_RDWR Open for reading and writing
O_APPEND Open at end of file for writing
O_CREAT Create the file if it doesn't already exist
O_EXCL If set and O_CREAT set will cause open () to fail if the file already exists
O_TRUNC Truncate file size to zero if it already exists

Creat System Call


The creat() system call is used to create and open a file. The prototype is
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int creat(const char *path, mode_t mode);

36a
If the path parameter refers to a file that already exists the file size is truncated to zero but
it is otherwise unchanged. The parameter mode refers to the initial access rights of the
freshly created file; the same symbolic values associated with chmod () should be used.
The return value from creat () is a file descriptor that can be used to write to the file, even
if, surprisingly, the access rights specified by mode indicate read only.

Read System Call

The read() system call is used to read data from a file or other object identified
by a file descriptor. The prototype is
#include <sys/types.h>
size_t read(int fildes, char *buf, size_t nbytes);

fildes is the descriptor, buf is the base address of the memory area into which the data is
read and nbytes is the maximum amount of data to read. The return value is the actual
amount of data read from the file. The pointer is incremented by the amount of data read.
Note that buf is only the base address for data to be read to, the size of the area needs to
be separately communicated by the nbytes parameter. An attempt to read beyond the end
of a file results in a return value of zero. If the end of file is encountered while reading a
file then it is likely that the return value will be less than nbytes. The data is transferred
without any alteration, modification or processing.

Close System Call

The close() system call is used to close files. The prototype is


#include <unistd.h>
int close(int fildes);

There is little to say about this function. When a process terminates, all the files
associated with it are closed anyway. Thus it is easy to get into the habit of not bothering
to close files. This is a mistake as open files do consume resources and all normal systems
impose a limit on the number of files that a process can hold open, this can be
encountered if files are opened and not closed in a loop.

Write System Call

The write() system call is used to write data to a file or other object identified by a
file descriptor. The prototype is
#include <sys/types.h>
size_t write(int fildes, char *buf, size_t nbytes);

fildes is the file descriptor, buf is the base address of the area of memory that data is
copied from, nbytes is the amount of data to copy. The return value is the actual amount
of data written, if this differs from nbytes then something has gone wrong

36a
Lseek System Call

Whenever a read () or write () operation is performed on a file, the position in the


file at which reading or writing starts is determined by the current value of the read/write
pointer. The value of the read/write pointer is often called the offset. It is always
measured in bytes from the start of the file. The lseek () system call allows programs to
manipulate this directly so providing the facility for direct access to any part of the file. It
is, of course, entirely the responsibility of applications to determine the required new
value of the offset; this will usually indicate the start of a data record and be determined
by index/table look-up or a technique such as hashing.

It has three parameters and the prototype is

Symbolic value Meaning


SEEK_SET set pointer to value of offset
set the pointer to its current value plus the value of offset which may, of
SEEK_CUR
course, be negative
SEEK_END set the pointer to the size of the file plus the value of offset

Rename System Call

The rename () system call renames a file. The prototype is


#include <stdio.h>
int rename(const char *old, const char *new);

This will return –1 in case of failure that may happen if the file to be renamed does not
exists. It returns 0, if the file is successfully renamed.

Unlink System Call

This system call is used to delete a file using unlink () whilst the file is still
open, the file will disappear from directory listings as soon as it is called. The unlink ()
function removes a link to a file. If path names a symbolic link, unlink () removes the
symbolic link named by path and does not affect any file or directory named by the
contents of the symbolic link. Otherwise, unlink () removes the link named by the
pathname pointed to by path and decrements the link count of the file referenced by the
link. It returns 0, if the file is successfully removed. The prototype is

#include <unistd.h>
int unlink(const char *path);

36a

You might also like