<unix Environment Advanced Programming > file sharing and Fork functions

Source: Internet
Author: User
Tags glob

UNIX systems support the sharing of open files between different processes. The kernel uses 3 data structures to represent open files, and the relationship between them determines the possible impact one process may have on another process in terms of file sharing.

The kernel maintains 3 tables, the process table, the file table, and the V node table. Specific as follows:

1> each process has a record entry in the process table, and the record entry contains an open file descriptor table with each descriptor occupying one item. associated with each file description typeface:

A. File descriptor flag (CLOSE_ON_EXEC);

B. A pointer to a file table entry.

The 2> kernel maintains a single file table for all open files. Each file Table entry contains:

A. File status flag (read, write, add write, synchronous and non-blocking, etc.);

B. current file offset;

C. A pointer to the V-node table entry for the file.

3> each open file (or device) has a V-node (V-NODE) structure. The V node contains the file type and pointers to various manipulation functions for this file. For most files, the V node also contains the I node (I-node, index node) of the file, which is used to read the open file from disk to memory. The I node contains the owner of the file, the length of the file, and a pointer to the location of the file's actual data block on disk. (The Unix file system has more information about the I node.) In addition, Linux does not use the V node, but instead employs a file system-related I node and a file system-independent I node)

         

                 Figure 1 Kernel data structure for open files

  Shows the relationship between the 3 tables that a process corresponds to. The process has two different open files: one file opens from standard input (file descriptor 0) and the other file opens from standard output (file descriptor 1).

  

         

             Figure 22 Benefit processes each open the same file

  

  An existing process can call the fork function to create a new process. The process created by the fork process is called a child process, and the Frok function is called once and returned two times. The return value of the child process is 0, and the parent process's return is the process ID of the child process. The child process and the parent process continue to execute the instruction after the fork call. (two will be executed, but the order of execution is variable, depending on the scheduling algorithm used by the kernel.) A child process is a copy of the parent process, such as a child process that obtains a copy of the parent process data space, heap, and stack. The parent and child processes do not share storage space, but share body segments. However, many implementations do not perform a full copy of the parent process data, but instead use the write-time replication (copy-on-write, COW) technique.

The following code can see that the changes made by the child process to the variable do not affect the value of the variable in the parent process.

1#include"apue.h"2 3 intGlob =6;4 CharBuf[] ="a write to stdout\n";5 6 int7Mainvoid)8 {9     int        var;Ten pid_t pid; One  A     var= the; -     if(Write (Stdout_fileno, buf,sizeof(BUF)-1) !=sizeof(BUF)-1) -Err_sys ("Write Error"); theprintf"before fork\n"); -  -     if(PID = fork ()) <0) { -Err_sys ("Fork Error"); +}Else if(PID = =0) { -glob++; +         var++; A}Else { atSleep2); -     } -  -printf"pid =%d, Glob =%d, var =%d\n", Getpid (), Glob,var); -Exit0); -}
View Code

If the program is executed, you can get:

  

as you can see, the first time for direct execution, output to standard output, and the second redirect to the file. Causes the same code to be executed two times, resulting in a different result. We know that the file I/O functions are unbuffered and the standard I/O libraries are buffered. When the standard output is connected to an end device, it is buffered, otherwise it is fully buffered. In the first execution, the printf function outputs to standard output, and the buffer is flushed by newline characters. When redirected to a file, the buffer is fully buffered, and printf is called once before the fork is called. However, when Fork is called, the row data is still in the buffer, and then the parent process data space is copied into the child process, the buffer data is also copied to the child process, where the parent and child processes each have a buffer with that row. The second printf before exit appends its data to an existing buffer. When the process terminates, the contents of its buffer are written to the appropriate file.

One thing to note about the above procedure is that when redirecting the standard output of the parent process, the standard output of the child process is also redirected. One feature of fork is that all open file descriptions of the parent process are nonalphanumeric copied into the child process. The parent and child processes share a single file table entry for each of the same open descriptors. It is important that both the parent and child processes share the same file offset.

             

Reprint Please specify address <HTTP://WWW.CNBLOGS.COM/QIUYI116/P/4322466.HTML>, thank you!

  Refer to the third edition of UNIX environment programming

<unix Environment Advanced Programming > file sharing and Fork functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.