Linux system Programming-interprocess communication (i)

Source: Internet
Author: User
Tags terminates

Basic Operations Command:

PS-AJX/-AUX/-EF view inter-process status/relationships

Top dynamic display of processes in the system

Nice run/renice by the specified priority change the priority of the running process

Kill-9 Kill Process

Jobs view the number of background processes

The structure, type, state, mode of the process

The 0.Linux process consists of three segments:

(1) Data segment. The data space for global variables, constants, and dynamic memory is stored.

(2) Text paragraph. The code in the program is stored.

(3) Stack segment. The return address of the function, the parameters of the function, and the local variables are stored.

Process type: Interactive process that can be run in the foreground or in the background. This class of processes is controlled and run by the shell.

The batch process, which is not part of a terminal, is submitted to a queue for sequential execution .

Daemon: Execution at the beginning of the system, end when system shuts down.

1. Dedicated processes in the system:

(1) a process with ID 0 is a dispatch process, often called a swap process, which is part of the kernel and is not executed on any disk, and therefore is also referred to as a system process.

(2) the process with ID 1 is the init process, and theinit process typically reads system-related initialization files and boot to a state . The init process is never terminated, and the Init process is the parent process for all orphan processes.

(3) The process with ID 2 is the page daemon, which is responsible for paging operations that support virtual storage systems.

2. The child process is a copy of the parent process, and the child process obtains a copy of the parent process's data space, heap, and stack. This is a separate space for the child process. Parent-child processes do not share these storage spaces, and parent-child processes share body segments. Whether the parent or child processes are first executed after the fork is not necessarily, depends on the scheduling algorithm used by the kernel, and requires inter-process communication if the parent-child process is required to synchronize with one another.

Vfork can also create a process, but it cannot produce a copy of the parent process, and the parent process is copied when the child process needs to change the in-memory data.

The difference between a parent and a child process:

The return value of fork is different;

The process ID is different;

The file lock set by the parent process is not inherited by the quilt process;

The unhandled alarm of the child process is cleared;

The set of signals that are not processed by the child process sets to empty.

(1) Orphan process: for all processes that are terminated by the parent process, their parent process becomes the init process. whenever a process terminates, Init invokes a wait function to get its terminating state. This prevents a lot of zombie processes in the system.

(2) Zombie process: child process end, parent process no corpse.

(3) Inter-process data sharing.

exec function Family: When the process thinks that it can't make any contribution to the system and the user, it can call the exec function family and let itself execute the new program. If a process wants to execute another program while executing a program, the child process is created first, and the EXEC function is called in the middle of the child process. The EXEC function is used to add error judgments.

Traditional inter-process communication

(1) Nameless pipe: The nameless pipe is half-duplex , the data only want to flow in one direction, with fixed read and write end; nameless pipes can only be used between parent-child processes or sibling processes (processes that have affinity).

The nameless pipe is a separate file system, and the pipeline is a file descriptor based method of communication, and when a pipeline is established, it creates two file descriptors, where fd[0] is fixed to a read pipeline and Fd[1] is fixed to a write pipeline. The pipeline exists only in memory.

Pipeline reads and writes:

A pipeline is a file, so read and write to the pipeline with the read () and write () functions.

One process writes to the pipeline that the content is read out by the other end of the pipeline. The content that is written is added to the end of the pipeline every time , and the data is read from the head of the buffer each time.

When the parent process writes to the pipeline process , the file descriptor of the corresponding read pipeline is closed, and the child process writes, and it closes the corresponding read operation.

When the pipeline is empty, the read operation is blocked and the Linux operating system is not guaranteed when the data is written to the pipeline . An atomic operation that is written to a pipeline where the write process attempts to write data to the pipeline as long as there is an idle area. If the read operation does not read the data in the buffer, the write operation is always blocked. The write pipeline only makes sense if the read pipeline exists, not

But the sigpipe signal (pipe rupture) is emitted by the system. The application can process the signal or ignore it ( the default action is to terminate the program).

#include <unistd.h>

int pipe (int fd[2])

(2) a well-known pipe: Unlike a famous pipe, it provides a path name associated with it, and a well-known pipeline enables communication between non-closed processes. exists in the file system in the form of a FIFO file.

Note: FIFO strictly follows first-out, the pipeline and FIFO reads always return data from the beginning, the write to them is to add data to the end. File operations such as Lseek () are not supported.

#include <sys/types.h>

#include <sys/stat.h>

int Mkfifo (const char * pathname, mode_t mode)

The first parameter of the function is a normal pathname, which is the name of the FIFO after creation. The second parameter is the same as the mode parameter in the open () function that opens the normal file. If the first parameter of Mkfifo is an already existing pathname, the eexist error is returned, so the typical calling code first checks to see if the error is returned, and if it does return the error, simply call the function that opened the FIFO. General file I/O functions can be used for FIFO, such as close, read, write, and so on

A nameless pipe consists of an inode on a basic file system storage device, a memory inode attached to it, two open file control blocks (respectively, corresponding to the information sender of the pipeline and the information receiving end) and the description of the process to which it belongs, Generated after the system executes the pipe (P) command line. And in p[0] returns the Read channel Open file description of the pipeline, etc., in p[1] return the pipeline's write channel open file descriptor. Structurally, nameless pipes do not have file path names, do not occupy file directory entries, so the linked list in the file directory structure does not apply to such a file, it only exists in the open file structure of a temporary file, with the process of its attachment to survive, and when the process terminates, the nameless pipe also dies.

The information sent to the pipeline disappears from the pipeline once the read process is taken, and the read-write operation conforms to the FIFO-first queue principle.

Pipeline files are tools for interprocess communication, and in order to minimize the use of system storage resources, the general system restricts them to small files with a maximum length of 4096 (Pipsiz) bytes. when the message to be written exceeds 4096 bytes, a synchronization problem between the read and write processes occurs. The first write operation to find the pipe file in the current pointer offset F-offset, and then start as far as possible to write information, when the length of 4096 bytes, the system controls the write process to sleep, waiting for the read process to take all the information, the file length pointer 0, the write process is awakened to continue to work.

In order to prevent multiple processes from reading and writing a pipe file at the same time, the Ilock flag entry is set in the Inode glyph I-flay entry in the pipeline file to set up the software lock to implement mutually exclusive use of the pipeline file between multiple processes.

There are two serious shortcomings in the nameless pipeline.

First, nameless pipes can only be used to connect processes that have a common ancestor.

Second, the nameless pipe is the temporary existence of the attachment process. So later came a variant of the nameless pipe-known as a pipeline, which is often referred to as FIFO. In addition to inheriting all of the features of the Nameless pipeline, the famous pipeline also discards the two drawbacks of the nameless pipeline.

First,FIFO is a permanent mechanism that has a common Unix system file name. You can use the Mknod command to create a permanent pipeline under the system, unless you deliberately delete it, otherwise it will remain in the system.

Secondly, it is because the famous pipe is identified by "file name", so as long as the agreement of a specific file name, so that all know that the contract service process, regardless of whether there is kinship between them, can conveniently use the pipeline to communicate.

You can create a named pipe by using the following command:

/etc/mknod Pipe_name P

where "Pipe_name" is the name of the named pipe to be created, the parameter p must appear after the named pipe name.

The Named pipes (named Pipes) pipeline has a good use of flexibility, as shown in:

1) can be used both locally and on the network.

2) can be referenced by its name.

3) Support multi-client connection.

4) Support bidirectional communication.

5) supports asynchronous overlapped I/O operations.

The similarities and differences between nameless pipes and famous pipelines:

1. Means of communication;

2. Whether the process is relevant;

3. Different areas of existence;

4. The existence cycle is different;

Same point:

1. all are FIFO;

2. The capacity of the pipeline is the same;

Linux system Programming-interprocess communication (i)

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.