Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ File sharing and fork function     - How to forcibly change the Linux system password on a regular basis (Linux)

- Linux, MySQL / MariaDB Galera Cluster Setup Process (Database)

- The principle Httpclient4.4 (HttpClient Interface) (Programming)

- Embedded Linux to solve the problem in the time zone (Linux)

- Moosefs Distributed File System Configuration (Server)

- A new method for Linux hidden files (Linux)

- To solve the Mac in question invalid BASH under configuration environment variable (Linux)

- Linux Operating System Security Management describes the top ten tricks (Linux)

- To install the latest version of the EPEL on CentOS 5.x or 6.x (Linux)

- Compile Android libwebcore.so error occurs when solving (Programming)

- iptables allow only specific ip address to access the specified port (Linux)

- Oracle 10g, 11g database silent installation of small differences (Database)

- Automatic batch resolve dependencies problem locally installed rpm package (Linux)

- CentOS network configuration 7, and set the host name and IP-bound problems (Linux)

- Using IntelliJ IDEA Import Spark Spark latest source code and compile the source code (Linux)

- MySQL use benchmarking tool sysbench (Database)

- Linux SVN account password to save your settings (Linux)

- Linux find command to find files (Linux)

- Use eCryptFS encrypt files and directories on Linux (Linux)

- Linux System Getting Started Tutorial: How to find the maximum memory your system supports (Linux)

 
         
  File sharing and fork function
     
  Add Date : 2018-11-21      
         
         
         
  UNIX system supports shared between different processes open files. The kernel uses three kinds of data structures represent open files, the relationship between them determines the impact of file sharing on a process to another process that may arise.

Kernel maintains three tables, namely the process table, file table and v-node table. details as follows:

1> each process in the process table has a record entry, record entry contains a table of open file descriptors, each descriptor occupies one. Associated with each file descriptor is associated with:

. A file descriptor flags (close_on_exec);

b. a pointer to a file table entry.

2> kernel all open files maintain a file table. Each file contains the entry:

. A file status flags (read, write, Tianxie, synchronous and non-blocking, etc.);

. B The current file offset;

c. a pointer to the file v node entries.

3> Each open file (or device) has a node v (v-node) structure. v node contains a file type and a pointer to this file various operations functions. For most files, v node also contains the i-node (i-node, index nodes) of the file, the information in the open file is read from disk into memory used. i node contains the file owner, file size, file pointing to the actual data block pointer location on the disk. (UNIX file system has more on the introduction of i-node. Also, Linux does not use the v node, instead of using a file system i-node associated with the i-node and a file system-independent)

Open the file kernel data structures

A process corresponding to the relationship between the three tables. The process has two different open file: Open a file from the standard input (file descriptor 0), another file is opened from the standard output (file descriptor 1).

Two independent processes each open the same file

An existing process can call the fork function creates a new process. Created by the process fork process is called the child process, frok function is called once, twice returned. The return value is 0 child process, the parent process is the process to return the child process ID. Child and parent process to continue executing instructions after calling fork. (Two will be executed, but the execution order of the first variable, depending on the scheduling algorithm used by the kernel.) Child process is a copy of the parent process, such as the parent process the child process to obtain the data space, heap and stack copies. Parent and child do not share storage space but share the text segment. However, many implementations do not perform an exact copy of the parent process data replication (Copy-On-Write, COW) techniques but the use of writing.

The following code change can be seen in the child process variables did not affect the value of the parent process variable.

#include "apue.h"

int glob = 6;
char buf [] = "a write to stdout \ n";

int main (void)
{
    int var;
    pid_t pid;

    var = 88;
    if (write (STDOUT_FILENO, buf, sizeof (buf) -1)! = sizeof (buf) -1)
        err_sys ( "write error");
    printf ( "before fork \ n");

    if ((pid = fork ()) < 0) {
        err_sys ( "fork error");
    } Else if (pid == 0) {
        glob ++;
        var ++;
    } Else {
        sleep (2);
    }

    printf ( "pid =% d, glob =% d, var =% d \ n", getpid (), glob, var);
    exit (0);
}

We can see, for the first time direct execution, to the standard output; while the second is redirected to a file. Resulting in two executions of the same code, the result was different. We know that the file I / O functions are unbuffered, standard I / O library is buffered. The standard output is connected to a terminal device, it is line buffered; otherwise it is fully buffered. The first execution, printf function is output to the standard output buffer is flushed by the newline character. While redirected to a file, the buffer is full buffer before calling fork call printf once. But when calls fork, the rows remain in the buffer, and then copied again when the parent process data space to the child, the buffer data is replicated to the child, when the parent and child processes each have a with the line buffer. Before the exit of the second printf to append their data into an existing buffer. When the process is terminated, the buffer contents are written to the appropriate file.

The above procedure, we need to pay attention to is this: the standard output redirection parent process, the standard output of the child process is redirected. A characteristic fork is the parent of all open file descriptors are copied to the child process. Each parent and child processes share the same open a file descriptor table entry. The important point is that the parent and child processes share the same file offset.
     
         
         
         
  More:      
 
- Web server security policy (Linux)
- Graphical interface for the CentOS 6.4 installed and connected by remote VNC (Linux)
- Android to determine whether the device to open WIFI, GPRS data connection (Programming)
- Android media library of analysis: MediaProvider (Programming)
- Linux, MySQL root privilege escalation another method (Linux)
- Compiling source code Nginx module installation subs_filter (Server)
- How to use the Linux kill command to kill the process / program is not responding (Linux)
- Nginx Load Balancing (standby) + Keepalived (Server)
- Vim plugin installation YouCompleteMe (Linux)
- Linux operating system ARP Spoofing Defense (Linux)
- Joseph Central Java implementation (Programming)
- Install Ubuntu open source drawing program MyPaint 1.2.0 (Linux)
- Enterprise-class GitHub warehousing environment build (Server)
- JavaScript basic types and type conversion (Programming)
- CentOS6.5 install SVN & visual management tools iF.SVNAdmin (Server)
- Linux character device - automatically creates the device nodes and devices (Linux)
- Eclipse distributed management using GitHub project development (Linux)
- Physical backup and recovery SYSTEM table space (Database)
- What is a logical partition management LVM, how to use in Ubuntu (Linux)
- Ubuntu mysql stop fail to solve the problem (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.