An overview,
Pipeline model
Ps - ef | grep keyword | awk '{print $2}' | xargs kill 9Copy the code
Here the vertical bar “|” is a pipe. It takes the output of the previous command as the input of the later command. As the name suggests, a pipe is a one-way data transfer mechanism. It is actually a cache in which data can only be written from one end and read from the other. If we want to communicate with each other, we need to create two pipes.
Pipeline is divided into two types, “|” said pipe called anonymous pipe, which means that this type of pipeline has no name, destroyed you’re done. As in the command above, the pipe represented by the vertical line is automatically created and destroyed as the command executes. The user doesn’t even know they’re using the plumbing technology, and they’ve solved the problem. So this is often asked in the interview questions, when the time comes, don’t say this is a vertical line, but to answer the mechanism behind the pipeline.
Another type is named pipes. This type of pipe needs to be explicitly created with the mkFIFo command.
mkfifo hello
Copy the code
Hello is the name of the pipe. Pipes exist as files, which is consistent with the everything is a file principle in Linux. In this case, we can see that the file type is P, which stands for pipe.
# ls -l
prw-r--r-- 1 root root 0 May 21 23:29 hello
Copy the code
Next, we can write something into the pipe. For example, write a string.
# echo "hello world" > hello
Copy the code
At this time, the contents of the pipe are not read, the command is stopped here, which means that when one project team wants to hand over its output to another project team for input, the former project team can not walk away until the handover is complete.
At this point, we need to reconnect a terminal. On the terminal, read the contents of the pipe with the following command:
# cat < hello
hello world
Copy the code
On the one hand, we can see that the contents of the pipe are read and printed to the terminal. On the other hand, the echo command exits normally. That is to say, the previous project team has completed its mission and can be disbanded.
We can see that the efficiency of the pipeline is relatively low. Also, it is not suitable for frequent data exchange between processes.
Message queue model
This model is similar to the message queue model for interprocess communication. Unlike pipes, which pour information from one process to another, message queues are a bit like mail. As data is sent, it is broken up into separate data units called message bodies, each of which is a fixed size chunk of storage that is discontinuous on the byte stream.
The definition of this message structure is given below. The types of type and text are not mandatory, as long as the sender and receiver of the message agree.
struct msg_buffer {
long mtype;
char mtext[1024];
};
Copy the code
Next, we need to create a message queue using the msgget function. This function needs to take a parameter key, which is the unique identifier of the message queue and should be unique. How do you keep it unique? This is still associated with the file.
We can specify a file, and fTOK will generate a near-unique key based on the file’s inode. As long as the file is not deleted during the lifetime of the message queue. Whenever you call ftok again, you’ll get the same key as long as you don’t delete it. This use of keys will be encountered frequently in this chapter because they are part of the System V IPC interprocess communication mechanism.
Shared memory model
Sometimes, however, processes need to communicate very closely and share large amounts of data. If you use email, you will find that, on the one hand, the email is not timely; On the other hand, attachment size is also limited, so in this case, we often take the approach of shared memory.
When we talked about memory management, we know that each process has its own virtual memory space, and the virtual memory space of different processes is mapped to different physical memory. This process accesses address A and another process accesses address A. In fact, they access different physical memory addresses and do not affect the addition, deletion, and change of data.
However, we can take a piece of virtual address space, mapping to the same physical memory. This allows one process to write something that another process can see immediately, without having to copy it back and forth or pass it around.
Shared memory is also part of the System V IPC interprocess communication mechanism, so familiar faces can be seen in its usage flow.
We can create a shared memory and call shmget. In this system, the creation of an IPC object is xxxGET, which is the first parameter of the key, and msgget, which is the same as the key, is the unique location of a shared memory object, can also be associated with the file to achieve unique. The second parameter is the size of shared memory. The third argument, if IPC_CREAT, also creates a new one.
int shmget(key_t key, size_t size, int flag);
Copy the code
A semaphore
If two processes attach the same shared memory and everyone writes to it, it’s likely to conflict. For example, if two processes write to the same address at the same time, the first process will find that the content has been overwritten by someone else.
Therefore, a protection mechanism is needed so that the same shared resource can only be accessed by one process at a time. In the System V IPC interprocess communication mechanism, a solution has long been thought out, namely Semaphore. Therefore, semaphores and shared memory are often used together.
A semaphore is actually a counter that is used for mutual exclusion and synchronization between processes rather than for storing interprocess communication data.
We can initialize a semaphore to a number that represents the total amount of a resource. For semaphores, two atomic operations are defined. One is the P operation, which we call the resource request operation. This operation subtracts N from the semaphore value to indicate that it is used by the semaphore and not by others. The other is the V operation, which we call the return resource operation. This operation will request that the semaphore be added to M to indicate that the quantity has been returned to the semaphore and can be used by others.
For example, if you have $100, you can set the semaphore to 100. Where A borrows 80 dollars from you, it calls P and asks for 80 less. If B borrows 50 yuan from you at the same time, but B’s P operation is later than A’s, there is no way out. Only when A returns the money can B’s P operation be successful. After that, A calls V, asks for 30 dollars, so it gives you back 30 dollars, and at this point, the semaphore has 50 dollars, and then B’s P can succeed, and then B can borrow the 50 dollars.
The so-called Atomic Operation means that any dollar can only be lent to one person through P Operation, not to two people at the same time. In other words, when A’s P operation (borrowing 80) and B’s P operation (borrowing 50) arrive at almost the same time, they can’t all succeed just because they see 100 in their account, they have to come first and come later.
signal
The way of interprocess communication mentioned above is the working mode in the normal state, corresponding to our usual work handover, sending and receiving emails, joint development, etc. In fact, there is also a working mode in abnormal circumstances.
For example, when there is an online system failure, there is no time for any process, no email, no meeting, all the architects, development, operations have to be called out. Therefore, 7 times 24 hours of uninterrupted implementation of the system need to have an alarm system, once something happens, it is necessary to notify people, even if it is the middle of the night, also want to call up, deal with the fault.
Corresponding to the operating system, is the signal. The signal has no particularly complex data structure, just a code number. Linux provides dozens of signals, each with different meanings. Signals are distinguished by their values. It’s just like when we watch a gangster movie, for urgent action, we always say, “Operation Mission 1” begins to carry out, the police begin to act. This is an emergency. We can’t talk too much.
A signal can be sent to a process at any time, and the process needs to configure a signal handler for the signal. When a signal occurs, this function is executed by default. This is equivalent to our operation and maintenance of a system emergency manual, when we meet any situation, do anything, are prepared in advance, out of the thing to do it.