Hardware Reference
In-Depth Information
System call Meaning
creat(name, mode) Create a file; mode specifies the protection mode
unlink(name) Delete a file (assuming that there is only 1 link to it)
open(name, mode) Open or create a file and return a file descriptor
close(fd) Close a file
read(fd, buffer, count) Read count bytes into buffer
write(fd, buffer, count) Write count bytes from buffer
lseek(fd, offset, w)
Move the file pointer as required by offset and w
stat(name, buffer)
Return information about a file
chmod(name, mode)
Change the protection mode of a file
fcntl(fd, cmd, ...)
Do various control operations such as locking (part of) a file
Figure 6-35. The principal UNIX file system calls.
count telling how much data to transmit. Lseek is used to position the file pointer,
making random access to files possible.
Stat returns information about a file, including its size, time of last access,
owner, and more. Chmod changes the protection mode of a file, for example, al-
lowing or forbidding users other than the owner from reading it. Finally, fcntl does
various miscellaneous operations on a file, such as locking or unlocking it.
Figure 6-36 illustrates how the major file I/O calls work. This code is minimal
and does not include the necessary error checking. Before entering the loop, the
program opens an existing file, data , and creates a new file, newf . Each call returns
a file descriptor, infd , and outfd , respectively. The second parameters to the two
calls are protection bits specifying that the files are to be read and written, re-
spectively. Both calls return a file descriptor. If either open or creat fails, a neg-
ative file descriptor is returned, telling that the call failed.
/* Open the file descriptors. */
infd = open(
, 0);
outfd = creat( ′′ newf ′′ , ProtectionBits);
′′
data
′′
/* Copy loop. */
do {
count = read(infd, buffer, bytes);
if (count > 0) write(outfd, buffer, count);
} while (count > 0);
/* Close the files. */
close(infd);
close(outfd);
Figure 6-36. A program fragment for copying a file using the UNIX system calls.
This fragment is in C because Java hides the low-level system calls and we are
trying to expose them.
Search WWH ::




Custom Search