Test1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
char buf[128];
memset(buf,0x00,sizeof(buf));
int len = read(STDIN_FILENO,buf,sizeof(buf));
write(STDOUT_FILENO,buf,len);
return 0;
}
Test2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
int main(int argc,char *argv[])
{
if(argc < 3)
{
printf("inupt error!");//了解printf刷新缓冲的机制
sleep(10);
return 0;
}
int len;
int fd_src,fd_dest;
fd_src = open(argv[1],O_RDONLY);
fd_dest = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0777);
char buf[1024];
while(len = read(fd_src,buf,sizeof(buf)))
{
write(fd_dest,buf,len);
}
close(fd_src);
close(fd_dest);
printf("success!@!\n");
return 0;
}
每一个文件流指针都对应着一个缓冲区指针,指向8192B的空间,这是为了提高读写效率,避免频繁的读写磁盘。
每次读写的内容都放在8192的缓冲区中,等待以下几个条件中的一个达到,刷新缓冲区。
刷新缓冲区的方法
1. \n
2. fflush
3. 缓冲区满
4. 程序正常退出
注:\n只能刷新终端文件缓冲区(stdin stdout stderr)
文件描述符的一些信息
lili@lili-linux:~$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3823
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3823
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
lili@lili-linux:~$ cat /proc/sys/fs/file-max
97384
lili@lili-linux:~$ cat /usr/include/asm-generic/errno-base.h
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */