Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the File Istroke O in linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

I would like to share with you the example analysis of the file iPink O in the linux system. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

Linux File I Dot O

One, file descriptor

For the kernel, all open files are referenced by file descriptors. Each process has some file descriptors associated with it. The file descriptor is a non-negative integer. When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process. When reading or writing a file, use the file descriptor returned by open or creat to identify the file and pass it as a parameter to read and write.

There are generally three and open file descriptors, which are:

The code is as follows:

0: standard input STDIN_FILENO

1: standard output STDOUT_FILENO

2 standard error output STDERR_FILENO

The symbol constant after each line is dependent on POSIX.

Open function

The code is as follows:

# include

# include

# include

Int open (const char * pathname, int flags)

Int open (const char * pathname, int flags,mode_t mode)

Pathname is the name of the file to be opened or created.

Flag is used to define the action to be taken to open a file, and one of the following modes must be called

O_RDONLY, O_WRONLY, and O_RDWR stand for read-only, write-only, and read-write, respectively.

Open can also include a combination of the following optional modes

O_APPEND: appends write data to the end of the file

O_CREAT: if the file does not exist, create it. When using this option, a third parameter, mode, is required to specify access to the new file.

O_EXCL: if O_CREAT is also specified and the file exists, an error will occur. Use this to test whether a file exists, and if so, create a file, which makes both test and creation an atomic operation.

O_TRUNC: if this file exists and is successfully opened for write-only or read-write, its length is truncated to 0.

The file descriptor returned by open must be the smallest unused descriptor value. This is used by some applications for standard input, standard output, or standard error output. For example, if a program closes its standard output and then calls open again, the file descriptor 1 will be called and the standard output will be effectively redirected to another file or device.

The POSIX specification also standardizes a creat call, which is equivalent to

Open (pathname,O_WONLY | O_CREAT | O_TRUNC, mode)

Close function

# include

Int close (int fd)

The close call terminates the association between a file descriptor fd and the corresponding file. The file descriptor is released and can be reused. Close call successfully returns 0, error returns-1.

Closing a file releases all record locks that the process adds to the file. When a process terminates, the kernel automatically closes all its open files.

Lseek function

Each open file has a current File offset associated with it. By system default, when a file is opened, the offset is set to 0 unless the O_APPEND option is specified. Lseek can set the offset for an open file.

The code is as follows:

# include

# include

Off_t lseek (int fd, off_t offset, intwhence)

Offset is used to specify the location, and the whence parameter defines the use of the offset value. The following values are available for whence:

The code is as follows:

SEEK_SET: The offset is set to offset bytes.

SEEK_CUR: The offset is set to its current locationplus offset bytes.

SEEK_END: The offset is set to the size of the fileplus offset bytes.

A successful call returns a byte offset from the file header to where the file pointer is set, while a failure returns-1. The parameter offset is defined in.

When the offset is greater than the file length, a hole occurs and does not occupy the storage area.

Read function

The code is as follows:

# include

Ssize_t read (int fd, void * buf, size_tcount)

Put the count characters read from the file associated with the file descriptor fd into the buf. Returns the number of bytes read, which may be less than the number of bytes requested. If the read call returns 0, no data has been read and the end of the file has been reached. If you return-1, it indicates an error.

Write function

The code is as follows:

# include

Ssize_t write (int fd, const void * buf,size_t count)

Writes the first count bytes of the buffer buf to the file associated with the file descriptor fd. Returns the number of bytes actually written, usually the same as the count value; otherwise, there is an error. A common cause of error is that the disk is full or exceeds the file length limit for a given process.

Example: create a file, write data, move the current offset, and read data.

The code is as follows:

# include / / must appear as early as possible, as it may affect other header files. # include

# include

# include

# include

Int main ()

{

Char* filename = ". / / file"

Char buf [100]

Char buf1 [5]

Int fd

Printf ("open a file to write\ n")

If ((fd = open (filename,O_RDWR | O_CREAT | Orange Trunk Magazine Shearing IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) = =-1)

{

Perror ("cannot open file\ n")

Return 1

}

Printf ("open file successfully!\ n")

Printf ("input a string:")

Gets (buf)

/ / write intofile

If (write (fd,buf,strlen (buf))! = strlen (buf))

{

Perror ("cannot write intofile\ n")

Return 1

}

Close (fd)

Printf ("open file to read.\ n")

If ((fd=open (filename,O_RDONLY) =-1)

{

Perror ("cannot open thefile.\ n")

Return 1

}

If (lseek (fd,3,SEEK_SET) =-1)

{

Perror ("lseek erroe\ n")

Return 1

}

/ / read from the file

If (read (fd,buf1,4) =-1)

{

Perror ("read error.\ n")

Return 1

}

Printf ("read from file is%s\ n", buf1)

Close (fd)

Return 0

}

Execution and output results:

The code is as follows:

Root@jb51:~$gcc-o io io.c

Root@jb51:~$./io

Open a file towrite

Open filesuccessfully!

Input a string:akxivbaslzkncxcasbxbwwvaidxbd

Open file toread.

Read from fileis ivba

Linux File IDUBO tutorial (2)

The following is an introduction to the relevant contents of the file Ihand O in linux. The kernel uses three data structures to represent open files, and the relationship between them determines the possible impact of one process on another process in terms of file sharing.

First, file sharing

The kernel uses three data structures to represent open files, and the relationship between them determines the impact one process may have on another process in terms of file sharing.

1) each process has a record entry in the process table, which contains an open file description table, which can be regarded as a vector, and each descriptor occupies one item. Associated with each file descriptor are:

A) File descriptor flag

B) A pointer to a file table entry

2) the kernel maintains a file table for all open files. Each file table entry contains:

A) File status flags (read, write, read and write, add, synchronization and blocking, etc.)

B) current file offset

C) pointers to file v node table items

3) each open file (or device) has a v node (v-node) structure. The v node contains pointers to file types and functions that compare files for various operations. For most files, the v node also contains the I node of the file. The I node contains the owner of the file, the length of the file, the device on which the file is located, a pointer to the location of the actual data block of the file on disk, and so on.

Open the kernel data structure of the file

If each of the two processes opens the same file, as shown in figure 2. Suppose the first process opens the file on file descriptor 3 and the other process opens the file on file descriptor 4. Each process has to get a file table entry, but there is only one v node table entry for a given file. One reason why each process has its own file table entry is to make each process have its own current offset from the asking price.

Now we will further explain several operations in the previous section of file Iram O (1):

1. After completing the write, the current offset in the file is the number of bytes increased. If the current offset is greater than the file length, the current file length in node I is set to the current file offset.

2. Open a file with O_APPEND, and the corresponding flag will be set to the file status ID. Each time you write, the current offset is set to the file length in the I node

3. When lseek is located at the end of the file, the current offset of the file will be set to the current file length.

There may be multiple file descriptors pointing to the same file table entry. You can see this when you call both dup and fork.

Multiple processes can read the same file correctly. However, when multiple processes write the same file, it may have unintended consequences. This can be avoided by atomic manipulation.

Atomic operation

Generally speaking, an atomic operation refers to an operation consisting of multiple parts. If the hospital executes it on its own, it will either complete all the steps or take none at all.

1. Add to a file

Consider a process that involves adding data to the end of a file. Early UNIX does not support open, so it can be implemented as follows:

The code is as follows:

If (lseek (fd, 0L, 2)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report