Example (taken from man open
in Linux):
#include <fcntl.h>
...
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *pathname = "/tmp/file";
...
fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
...
Another example – creating a lock file:
fd = open("/var/run/myprog.pid", O_WRONLY | O_CREAT | O_EXCL, 0644);
Redundant: creat(path, mode)
is equivalent to open(path,
O_WRONLY|O_CREAT|O_TRUNC, mode)
And it should have been called create
, says Ken Thompson
int close(int fildes);
off_t lseek(int fildes, off_t offset, int whence);
ssize_t read(int fildes, void *buf, size_t nbyte);
Note:
Number of bytes read may be less than the requested nbyte
read()
may block forever on a “slow” read from pipes, FIFOs (aka named
pipes), sockets, or keyboard
For sockets, read(socket, buf, nbyte)
is equivalent to
recv(socket, buf, nbyte, 0)
recv(int socket, void *buffer, size_t length, int flags)
ssize_t write(int fildes, const void *buf, size_t nbyte);
Note:
Number of bytes written may be less than the requested nbyte – ex) filling up a disk
write()
may block forever on a “slow” write into pipes, FIFOs, or
sockets
For sockets, write(socket, buf, nbyte)
is equivalent to
send(socket, buf, nbyte, 0)
send(int socket, const void *buffer, size_t length, int flags)
If the file was opened with O_APPEND
flag, the file offset gets set
to the end of the file prior to each write
Kernel data structures for open files
Two independent processes with the same file open
Kernel data structures after dup(1)
Sharing of open files between parent and child after fork
Last updated: 2019–01–28