一、open
简单来说,open建立了一条到文件或设备的访问路径。如果调用成功,它则返回一个可以被read、write和其他系统调用使用的文件描述符。这个文件描述符是惟一的,它不会与其他任何运行中的进程共享,如果两个程序同时打开同一个文件,他们会分别得到两个不同的文件描述符。如果他们都对文件进行写操作,那么它们会各写各的,它们分别接着上次离开的位置继续往下写。它们的数据不会交织在一起,而是彼此互相覆盖。两个程序对文件的读写位置(偏移量)不同。可以通过使用文件锁功能来防止冲突。
open系统调用执行成功是返回一个文件描述符,调用失败返回-1
open系统调用的原型:
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
/*
path:要访问的文件路径
oflags:访问方式
mode:当访问方式为O_CREAT时,通过mode来设置文件权限
*/
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
示例:
# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
int main()
{
int fd = open("/home/FUJIA/cy1706/practice/text",O_RDONLY)