系统调用IO-11-read,write,lseek及mycpy的实现

发布时间 2023-06-14 22:12:37作者: 一代枭雄

1. 概述

read

NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>
       //从fd中读,读到buf中去,读count个字节
       ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION
       read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

       If  count  is  zero, read() returns zero and has no other results.  If count is greater than SSIZE_MAX, the result is unspeci‐
       fied.

RETURN VALUE
       //如果成功,那么返回成功读取到的字节数;如果读到文件尾了,那么返回的是0个字节
       On success, the number of bytes read is returned (zero indicates end of file),
       //失败,返回-1,
       On error, -1 is returned, and errno is set appropriately. 

write

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>
       //将buf中的内容,写到fd中去,数据的个数为count个字节
       ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
       write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
RETURN VALUE
       On success, the number of bytes written is returned (zero indicates nothing was written).  On error, -1 is returned, and errno
       is set appropriately.

lseek

NAME
       lseek - reposition read/write file offset

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       off_t lseek(int fd, off_t offset, int whence);

DESCRIPTION
       The  lseek()  function  repositions  the offset of the open file associated with the file descriptor fd to the argument offset
       according to the directive whence as follows:

       SEEK_SET
              The offset is set to offset bytes.

       SEEK_CUR
              The offset is set to its current location plus offset bytes.

       SEEK_END
              The offset is set to the size of the file plus offset bytes.

       The lseek() function allows the file offset to be set beyond the end of the file (but this does not change  the  size  of  the
       file).   If  data  is later written at this point, subsequent reads of the data in the gap (a "hole") return null bytes ('\0')
       until data is actually written into the gap.

RETURN VALUE
       Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file.
       Otherwise, a value of (off_t) -1 is returned and errno is set to indicate the error.