Chapter VII of the file I / O (4)

File Sharing

Open the file kernel data structures

A process to open the same file twice

Process 1024 can open a file descriptor, a file is not open, the kernel will generate a file table, the file pointer table v-node points to the node table v, v is the node information of some of the information stat function returns, i of node information, when we open a file, the file system will be copied to the i-node information corresponding to the i-node information of the node v (i-node number, that a file exists in)

 

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <pwd.h>
#include <time.h>

#define ERR_EXIT(m) \
        do  \
        {   \
            perror(m); \
            exit(EXIT_FAILURE); \
        }while(0)

int main(int argc, char* argv[])
{
    int fd1,fd2;
    char readbuf1[1024] = {0};
    char readbuf2[1024] = {0};

    //open会打开文件表,文件表会记录当前文件的状态标志,当前文件的偏移量,引用计数,v节点指针,
    //每个fd都是独立的,所以偏移量这些也是唯一的,不会影响其他fd的偏移量
    fd1 = open("test.txt",O_RDONLY);
    if(fd1 == -1)
        ERR_EXIT("open");
    read(fd1, readbuf1, 4);
    printf("readbuf1 = %s\n",readbuf1);
    
    //open会新开文件表
    fd2 = open("test.txt",O_RDWR);
    if(fd2 == -1)
        ERR_EXIT("open");
    read(fd2, readbuf2, 4);
    printf("readbuf2 = %s\n",readbuf2);
    //写入数据后,v节点中的i节点对应硬盘中的数据改变了
    write(fd2,"AAAAA",6);


    memset(readbuf1, 0, sizeof(readbuf1));
    read(fd1, readbuf1, 6);
    printf("readbuf1 = %s\n",readbuf1);


    close(fd1);
    close(fd2);

    return 0;
}

 

The two processes open the same file

Copy the file descriptors (dup, dup2, fcntl)

after

Function:
Create a copy of oldfd using the smallest available file descriptor as a new file descriptor.

int dup(int oldfd);
参数:
    oldfd : 文件描述符
返回值:
   成功:返回新的文件描述符
   出错:返回-1.注意:由dup函数返回的新文件描述符一定是当前可用文件描述符中的最小值。

Output redirection

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <pwd.h>
#include <time.h>

#define ERR_EXIT(m) \
        do  \
        {   \
            perror(m); \
            exit(EXIT_FAILURE); \
        }while(0)

int main(int argc, char* argv[])
{
    int fd1;
    fd1 = open("test2.txt", O_WRONLY);
    if(fd1 == -1)
        ERR_EXIT("open");

    close(1);
    dup(fd1);
    printf("hello\n");

    //等同于上面的代码
    //dup2(fd1,1);

    close(fd1);

    return 0;
}

dup2

Function:
dup2 Duplicate file descriptor for the parameter oldfd referred to, and copies the parameters together return to oldfd newfd. Mandatory use of the new description file to copy, regardless of the original file descriptor is free

  • If the parameter newfd as an open file descriptor, the files will first be referred to newfd closed,
  • If newfd equal oldfd, newfd returns, without closing the document referred newfd.
  • dup2 the copied file descriptors share various documents with the original state of the file descriptor. Share all locked, read-write position and the authority or flags, and so on.
int dup2(int oldfd, int newfd);
参数:
    oldfd : 文件描述符
    newfd : 文件描述符
返回值:
    成功: 返回新的文件描述符
    出错: 返回-1. 注意:由dup2函数返回的新文件描述符一定是当前可用文件描述符中的最小值。

 
newfd and oldfd have in common:
(1) the same open file (pipe).
(2) Same file pointer, i.e. two files share a file pointer.
(3) the same access mode. Read, write.
(4) the state of the same file identifier.

Guess you like

Origin www.cnblogs.com/s3320/p/11329390.html