Skip Navigation Links | |
Exit Print View | |
Programming Interfaces Guide Oracle Solaris 11.1 Information Library |
2. Session Description Protocol API
Selecting Advisory or Mandatory Locking
Cautions About Mandatory Locking
Setting and Removing Record Locks
8. Programming With XTI and TLI
10. Transport Selection and Name-to-Address Mapping
11. Real-time Programming and Administration
Files that are organized as a sequence of data are called regular files. Regular files can contain ASCII text, text in some other binary data encoding, executable code, or any combination of text, data, and code.
A regular file is made up of the following components:
Control data, which is called the inode. This data includes the file type, the access permissions, the owner, the file size, and the location of the data blocks.
File contents: a nonterminated sequence of bytes.
The Oracle Solaris operating system provides the following basic forms of file input/output interfaces:
The traditional, raw style of file I/O is described in Basic File I/O.
The standard I/O buffering provides an easier interface and improved efficiency to an application run on a system without virtual memory. In an application running in a virtual memory environment, such as on the SunOS operating system, standard file I/O is outdated.
The memory mapping interface is described in Memory Management Interfaces. Mapping files is the most efficient form of file I/O for most applications run under the SunOS platform.
The following interfaces perform basic operations on files and on character I/O devices.
Table 5-1 Basic File I/O Interfaces
|
The following code sample demonstrates the use of the basic file I/O interface. read(2) and write(2) both transfer no more than the specified number of bytes, starting at the current offset into the file. The number of bytes actually transferred is returned. The end of a file is indicated on a read(2) by a return value of zero.
Example 5-1 Basic File I/O Interface
#include <fcntl.h> #define MAXSIZE 256 main() { int fd; ssize_t n; char array[MAXSIZE]; fd = open ("/etc/motd", O_RDONLY); if (fd == -1) { perror ("open"); exit (1); } while ((n = read (fd, array, MAXSIZE)) > 0) if (write (1, array, n) != n) perror ("write"); if (n == -1) perror ("read"); close (fd); }
When you are done reading or writing a file, always call close(2). Do not call close(2) for a file descriptor that was not returned from a call to open(2).
File pointer offsets into an open file are changed by using read(2), write(2), or by calls to lseek(2). The following example demonstrates the uses of lseek.
off_t start, n; struct record rec; /* record current offset in start */ start = lseek (fd, 0L, SEEK_CUR); /* go back to start */ n = lseek (fd, -start, SEEK_SET); read (fd, &rec, sizeof (rec)); /* rewrite previous record */ n = lseek (fd, -sizeof (rec), SEEK_CUR); write (fd, (char *&rec, sizeof (rec));
The following table lists the tasks performed by advanced file I/O interfaces.
Table 5-2 Advanced File I/O Interfaces
|
See syscall Provider in Oracle Solaris 11.1 Dynamic Tracing Guide for related information.
The file system control interfaces listed in the following table enable the control of various aspects of the file system.
Table 5-3 File System Control Interfaces
|