Chinaunix首页 | 论坛 | 博客
  • 博客访问: 204542
  • 博文数量: 16
  • 博客积分: 2001
  • 博客等级: 大尉
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-29 16:25
文章分类
文章存档

2011年(1)

2009年(1)

2008年(14)

我的朋友

分类: C/C++

2008-10-23 15:17:07

08.10.20
write
#include
size_t write(int fildes, const void *buf, size_t nbytes);

read
#include
size_t read(int fildes, void* buf, size_t nbytes);

open
#include
#include
#include
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
oflags
O_RDONLY  Open for read-only
O_WRONLY  Open for write-only
O_RDWR   Open for reading and writing
O_APPEND  Place written data at the end of the file
O_TRUNC   Set the length of the file to zero, discarding existing contents
O_CREAT   Creates the file, if necessary, with permissions given in mode
O_EXCL   Used with O_CREAT, ensure that the caller creates the file.

mode
S_IRUSR  Read permission, owner
S_IWUSR  Write permission, owner
S_IXUSR  Execute permission, owner
S_IRGRP  Read permission, group
S_IWGRP  Write permission, group
S_IXGRP  Execute permission, group
S_IROTH  Read permission, others
S_IWOTH  Write permission, others
S_IXOTH  Execute permission, others
umask
   The umask is a system variable that encodes a mask for file
permissions to be used when a file is created.
   You can change the variable by executing the umask command
to supply a new value.
close
#include
int close(int fildes);

ioctl
#include
int ioctl(int fildes, int cmd, ...);

note
   system call is time consuming.
e.g.
[copy_system.c]
#include
#include
#include
#include
int main()
{
  char c;
  int  in, out;
  in  = open("file.in", O_RDONLY);
  out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
  while (read(in, &c, 1) == 1)
    write(out, &c, 1);
  close(in);
  close(out);
  exit(0);
}
Note that the #include line must come first, because
it defines flags regarding POSIX compliance that may affect other
include files.
$ TIMEFORMAT="" time ./copy_system
4.67user 146.90system 2:32.57elapsed 99%CPU
[copy_block.c]
#include
#include
#include
#include
int main()
{
  char block[1024];
  int  in, out;
  int  nread;
  in = open("file.in", O_RDONLY);
  out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
  while ((nread = read(in, block, sizeof(block))) > 0)
    write(out, block, nread);
  exit(0);
}
$ TIMEFORMAT="" time ./copy_block
0.00user 0.02system 0:00.04elapsed 78%CPU

08.10.22
lseek
#include
#include
off_t lseek(int fildes, off_t offset, int whence);
whence can be one of the following:
SEEK_SET: offset is an absolute position
SEEK_CUR: offset is relative to the current position
SEEK_END: offset is relative to the end of the file
lseek returns the offset measured in bytes from the
beginning of the file that the file pointer is set to,
or -1 on failure.
fstat stat lstat
#include
#include
#include
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
  The fstat system call returns status information about
the file associated asssociated with an open file descriptor.
members of the structure,stat.
stat Member  Description
st_mode   Fil permissions and file-type information
st_ino   The inode associated with the file
st_dev   The device the file resides on
st_uid   The user identity of the file owner
st_gid   The group identity of the file owner
st_atime  The time of last access
st_ctime  The time of file last change
st_mtime  The time of last modification to contents
st_nlink  The number of hards links to the file
File-type flags include
S_IFBLK: Entry is a block special device
S_IFDIR: Entry is a directory
S_IFCHR: Entry is a character special device
S_IFIFO: Entry is a FIFO(named pipe)
S_IFREG: Entry is a regular file
S_IFLNK: Entry is a symbolic link
S_ISUID: Entry has setUID on execution
S_ISGID: Entry has setGID on execution
Masks to interpret the st_mode flags include
S_IFMT: File type
S_IRWXU: User read/write/execute permissions
S_IRWXG: Group read/write/execute permissions
S_IRWXO: Others' read/write/execute permissions
some macros defined to help with determining file types
S_ISBLK: Test for block special file
S_ISCHR: Test for character special file
S_ISDIR: Test for directory
S_ISFIFO: Test for FIFO
S_ISREG: Test for regular file
S_ISLNK: Test for symbolic link
example
  test that a file doesn't represent a directory and
has execute permission set for the owner but no other
permissions, you can use the following test:
struct stat statbuf;
mode_t modes;
stat("filename", &statbuf);
modes = statbuf.st_mode;
if (!S_ISDIR(modes) && (modes & S_IRWXU) == S_IXUSR)
   ...

dup
#include
int dup(int fildes);
int dup2(int fildes, int fildes2);

fprintf print sprintf format
Format       Argument  |Output|
%10s       "Hello"  |     Hello|
%-10s         "Hello"  |Hello     |
%10d          1234  |      1234|
%-10d         1234              |1234      |
%010d         1234              |0000001234|
%10.4f        12.34             |00012.3400|
%*s           10,"Hello"        |     Hello|
ferror feof clearerr
#include
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);
ferror function tests the error indicator for a stream
and returns nonzero if it's set, but zero otherwise.
feof tests the end-of-file indicator within a stream and
returns nonzero if it is set, zero otherwise. Use it like
this:
   if (feof(some_stream))
        /* we're at the end */
clearerr function clears the end-of-file an error indicators
for the stream to whick stream points.
fileno fdopen
#include
int fileno(FILE *stream);
FILE *fdopen(int fildes, const char *mode);
fileno determine which low-level file descriptor is being used for
a file stream, it returns the file descriptor for a given stream, or
-1 on failure.
fdopen function creat a new file stream based on an already-opened file
descriptor.
chmod
   change the permissions on a file or directory using the chmod system call.
#include
int chmod(const char *path, mode_t mode);

chown
  a superuser can change the owner of a file using the chown system call.
#include
#include
int chown(const char *path, uid_t owner, gid_t group);

unlink, link, symlink
#include
int unlink(const char *path);
int link(const char *path1, const char *path2);
int symlink(const char *path1, const char *path2);
unlink removes the directory entry for a file and decrements
the link count for it. It returns 0 if the unlinking was successful,
-1 on an error. You must have write and execute permissions in the
directory where the file has its directory entry for this call to
function.
   If the count reaches zero and no process has the file open, the file
is deleted.
The link system call creates a new link to an existing file, path1. The
new directory entry is specified by path2.
You can create symbolic links using the symlibk system call in a similar
fashion. Note that symbolic links to a file do not increment a file's
reference count an so do not prevent the file from being effectively deleted
as normal (hard) links do.

mkdir rmdir
#include
#include
int mkdir(const char *path, mode_t mode);
mkdir system call is used for creating directories.
#include
int rmdir(const char *path);
rmdir system call removes directories, but only if they are
empty.
chdir getcwd
#include
int chdir(const char* path);
As you use the cd command in the shell to change directory, so a
program can use the chdir system call.
#include
char *getcwd(char *buf, size_t size);
getcwd function writes the name of the current directory
into the given buffer, buf. it return NULL if the directory
name would exceed the size of the buffer (an ERANGE error),
given as the parameter size. It returns buf on success.
getcwd may also return NULL if the directory is removed (EINVAL)
or permissions changed (EACCESS) while the program is running.

opendir
#include
#include
DIR *opendir(const char *name);
opendir function opens a directory and establishes a directory stream. If
successful, it returns a pointer to a DIR structure to be used for reading
directory entries.
   opendir return a null pointer on failure.

readdir
#include
#include
struct dirent *readdir(DIR *dirp);
readir function returns a pointer to a struture detailing the next
directory entry in the directory stream dirp. Succeesive calls to
readdir return further directory entries. On error, and at the end
of the directory, readdir returns NULL.
dirent structure containing directory entry details includes the
following entries:
ino_t d_ino: The inode of the file
char  d_name[]: The name of the file

telldir
#include
#include
long int telldir(DIR *dirp);
telldir function returns a value that records the current position
in a directory stream. You can use this in subsequent calls to seekdir
to reset a directory scan to the current position.

seekdir
#include
#include
void seekdir(DIR *dirp, long int loc);
seekdir function sets the directory entry pointer in the
directory stream given by dirp. The value of loc, used
to set the position, should have been obtained from a prior
call to telldir.

closedir
#include
#incldue
int closedir(DIR *dirp);
closedir closes a directory stream and frees up the resources
associated with it. It returns 0 on success and -1 if there is
an error.

08.10.22
errno
EPERM:Operation not permitted
ENOENT:No such file or directory
EINTR:Interrupted system call
EIO:I/O Error
EBUSY:Device or resource busy
EEXIST:File exists
EINVAL:Invalid argument
EMFILE:Too many open files
ENODEV:No such device
EISDIR:Is a directory
ENOTDIR:Isn't a directory
strerror
#include
char *strerror(int errnum);
strerrror function maps an error number into a string describing the type of error that
has occurred. This can be useful for logging error conditions.
perror
#include
void perror(const char *s);
perror maps the current error, as reported in errno, into a string and prints
it on the standard error stream. It's preceded by the message given in the string
s (if not NULL), followed by a colon and a space.
e.g.
   perror("program");
might give:
   program: Too many open files
/proc
/proc/cpuinfo
/proc/meminfo
/proc/version
/proc/net/socketstat
/proc/sys/fs/file-max
The subdirectories of /proc that have numeric names are used to provide
access to information about running programs. each process has a unique
identifier: a number between 1 and about 32,000.
fcntl
#include
int fcntl(int fildes, int cmd);
int fcntl(int fildes, int cmd, long arg);
by using fcntl system call, we can perform several miscellaneous operations on
open file descriptors, including duplicating them, getting and setting file des-
criptor flags, getting and setting file status flags, and managing advisory file
locking.
The various operations are selected by different values of the command parameter
cmd, as defined in fcntl.h. Depending on the command chosen, the system call will
require a third parameter, arg:
fcntl(fildes, F_DUPFD, newfd)
    returns a new file descriptor with a numerical value equal to or greater than
the integer newfd.
fcntl(fildes, F_GETFD)
    This call returns the file descriptor flags as defined in
fcntl.h
fcntl(fildes, F_SETFD, flags)
    This call is used to set the file descriptor flags, usually
just FD_CLOEXEC.

fcntl(fildes, F_GETFL) fcntl(fildes, F_SETFL, flags)
    These calls are used, respectively, to get and set the file
status flags and access modes. You can extract the file access
modes by using the mask O_ACCMODE.
mmap
   mmap (for memory map) function sets up a segment of memory that
can be read or written by two or more programs. Changes made by one
program are seen by the others.
   You can use the same facility to manipulate files. You can make
the entire contents of a disk file look like an array in memory. If
the file consists of records that can be described by C structures,
you can update the file using structure array accesses.
#include
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
mmap function creates a pointer to a region of memory associated with the contents
of the file accessed through an open file descriptor.
you can alter the start of the file data that is accessed by the shared segment by
passing the off parameter. The open file descriptor is passed as fildes. The amount
of data that can be accessed (that is, the length of the memory segment) is set via
the len parameter.
you can use the addr parameter to request a particular memory address. If it's zero,
the resulting pointer is allocated automatically.
The prot parameter is used to set access permissions for the memory segment. This is
a bitwise OR of the following constant values:
PROT_READ: The segment can be read
PROT_WRITE: The segment can be written
PROT_EXEC: The segment can be executed
PROT_NONE: The segment can't be accessed
the flags parameter controls how changes made to the segment by the
program are reflected elsewhere; these options are displayed in the
following table.
MAP_PRIVATE  The segment is private, changes are local
MAP_SHARED  The segment changes are made in the file
MAP_FIXED  The segment must be at the given address, addr
msync
#include
int msync(void *addr, size_t len, int flags);
msync functions causes the changes in part or all of the memory segment
to be written back to (or read from) the mapped file.
The part of the segment to be updated is given by the passed start address,
addr, and length, len. The flags parameter controls how the update should
be performed using the options shown in the following table.
MS_ASYNC         Perform asynchronous writes
MS_SYNC     Perform sysnchronous writes
MS_INVALIDATE    Read data back in from the file
munmap
#include
int munmap(void *addr, size_t len);
munmap function releases the memory segment.
#include
#include
#include
#include
#include
typedef struct {
  int integer;
  char string[24];
}RECORD;
#define NRECORDS (100)
int main()
{
  RECORD record, *mapped;
  int i, f;
  FILE *fp;
  fp = fopen("records.dat", "w+");
  for (i = 0; i < NRECORDS; i++) {
    record.integer = i;
    sprintf(record.string, "RECORD-%d", i);
    fwrite(&record, sizeof(record), 1, fp);
  }
  fclose(fp);
  fp = fopen("records.dat", "r+");
  fseek(fp, 43*sizeof(record), SEEK_SET);
  fread(&record, sizeof(record), 1, fp);
  record.integer = 143;
  sprintf(record.string, "RECORD-%d", record.integer);
 
  fseek(fp, 43*sizeof(record), SEEK_SET);
  fwrite(&record, sizeof(record), 1, fp);
  fclose(fp);
  f = open("records.dat", O_RDWR);
  mapped = (RECORD*)mmap(0, NRECORDS*sizeof(record),
    PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);
  mapped[43].integer = 243;
  sprintf(mapped[43].string, "RECORD-%d", mapped[43].integer);
  msync((void*)mapped, NRECORDS*sizeof(record), MS_ASYNC);
  munmap((void*)mapped, NRECORDS*sizeof(record));
  close(f);
  exit(0);
}
阅读(648) | 评论(0) | 转发(0) |
0

上一篇:Emacs学习笔记1

下一篇:srilm 阅读文档3

给主人留下些什么吧!~~