20200103-inter-process-communication-linux translated

This commit is contained in:
laingke 2020-01-27 14:20:51 +08:00
parent 6763227775
commit 06dc5110d3
2 changed files with 168 additions and 176 deletions

View File

@ -1,176 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (laingke)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Introducing the guide to inter-process communication in Linux)
[#]: via: (https://opensource.com/article/20/1/inter-process-communication-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Introducing the guide to inter-process communication in Linux
======
This free eBook gives seasoned and occasional coders insight into the
core concepts and mechanisms of inter-process communication (IPC) in
Linux.
![Inter-process Communication in Linux][1]
Getting one software process to talk to another software process is a delicate balancing act. It can be a vital function for an application, though, so it's a problem any programmer embarking on a complex project has to solve. Whether your application needs to kick off a job being handled by someone else's software; to monitor an action being performed by a peripheral or over a network; or to detect a signal from some other source, when your software relies on something outside of its own code to know what to do next or when to do it, you need to think about inter-process communication (IPC).
The Unix operating system accounted for this long ago, possibly because of an early expectation that software would originate from diverse sources. In the same tradition, Linux provides many of the same interfaces for IPC and some new ones. The Linux kernel features several IPC methods, and the [util-linux package][2] contains the **ipcmk**, **ipcrm**, **ipcs**, and **lsipc** commands for monitoring and managing IPC messages.
### Show IPC information
Before experimenting with IPC, you should know what IPC facilities are already on your system. The **lsipc** command provides that information.
```
RESOURCE DESCRIPTION               LIMIT  USED  USE%
MSGMNI   Number of message queues  32000     0 0.00%
MSGMAX   Max size of message (byt.. 8192     -     -
MSGMNB   Default max size of queue 16384     -     -
SHMMNI   Shared memory segments     4096    79 1.93%
SHMALL   Shared memory pages       184[...] 25452 0.00%
SHMMAX   Max size of shared memory 18446744073692774399
SHMMIN   Min size of shared memory     1     -     -
SEMMNI   Number of semaphore ident 32000     0 0.00%
SEMMNS   Total number of semaphore 1024000.. 0 0.00%
SEMMSL   Max semaphores per semap  32000     -     -
SEMOPM   Max number of operations p  500     -     -
SEMVMX   Semaphore max value       32767     -     -
```
You may notice that this sample listing includes three different types of IPC mechanisms, each available in the Linux kernel: messages (MSG), shared memory (SHM), and semaphores (SEM). You can view current activity in each of those subsystems with the **ipcs** command:
```
$ ipcs
\------ Message Queues Creators/Owners ---
msqid     perms     cuid      cgid  [...]
\------ Shared Memory Segment Creators/Owners
shmid     perms    cuid    cgid  [...]
557056    700      seth    users [...]
3571713   700      seth    users [...]
2654210   600      seth    users [...]
2457603   700      seth    users [...]
\------ Semaphore Arrays Creators/Owners ---
semid     perms     cuid      cgid  [...]
```
This shows that there currently are no messages or semaphore arrays, but a number of shared memory segments are in use.
There's a simple example you can perform on your system so you can see one of these systems at work. It involves some C code, so you must have build tools on your system. The names of the packages you must install to be able to build from source code vary depending on your distro, so refer to your documentation for specifics. For example, on Debian-based distributions, you can learn about build requirements on the [BuildingTutorial][3] section of the wiki, and on Fedora-based distributions, refer to the [Installing software from source][4] section of the docs.
### Create a message queue
Your system has a default message queue already, but you can create your own using the **ipcmk** command:
```
$ ipcmk --queue
Message queue id: 32764
```
Write a simple IPC message sender, hard-coding in the queue ID for simplicity:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
struct msgbuffer {
  char text[24];
} message;
int main() {
    int msqid = 32764;
    strcpy(message.text,"opensource.com");
    msgsnd(msqid, &message, sizeof(message), 0);
    printf("Message: %s\n",message.text);
    printf("Queue: %d\n",msqid);
    return 0;
        }
```
Compile the application and run it:
```
$ gcc msgsend.c -o msg.bin
$ ./msg.bin
Message: opensource.com
Queue: 32769
```
You just sent a message to your message queue. You can verify that with the **ipcs** command, using the **\--queue** option to limit output to the message queue:
```
$ ipcs -q
\------ Message Queues --------
key        msqid   owner  perms  used-bytes  messages
0x7b341ab9 0       seth   666    0          0
0x72bd8410 32764   seth   644    24         1
```
You can also retrieve those messages with:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
struct msgbuffer {
    char text[24];
} message;
int main() {
    int msqid = 32764;
    msgrcv(msqid, &message, sizeof(message),0,0);
    printf("\nQueue: %d\n",msqid);
    printf("Got this message: %s\n", message.text);
    msgctl(msqid,IPC_RMID,NULL);
    return 0;
```
Compile and run with:
```
$ gcc get.c -o get.bin
$ ./get.bin
Queue: 32764
Got this message: opensource.com
```
### Download [the eBook][5]
This is just one example of the lessons available in Marty Kalin's [A guide to inter-process communication in Linux][5], the latest free (and Creative Commons) downloadable eBook from Opensource.com. In just a few short lessons, you will learn about POSIX methods of IPC from message queues, shared memory and semaphores, sockets, signals, and much more. Sit down with Marty's book, and you'll emerge a better-informed programmer. But it isn't just for seasoned coders—if all you ever write are shell scripts, there's plenty of practical knowledge about pipes (named and unnamed) and shared files, as well as important concepts you need to know when you use a shared file or an external message queue.
If you're interested in making great software that's written to be dynamic and system-aware, you need to know about IPC. Let [this book][5] be your guide.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/inter-process-communication-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[laingke](https://github.com/laingke)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_inter-process_communication_linux_520x292.png?itok=hPoen7oI (Inter-process Communication in Linux)
[2]: https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/
[3]: https://wiki.debian.org/BuildingTutorial
[4]: https://docs.pagure.org/docs-fedora/installing-software-from-source.html
[5]: https://opensource.com/downloads/guide-inter-process-communication-linux

View File

@ -0,0 +1,168 @@
[#]: collector: (lujun9972)
[#]: translator: (laingke)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Introducing the guide to inter-process communication in Linux)
[#]: via: (https://opensource.com/article/20/1/inter-process-communication-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Linux 进程间通信介绍指南
======
这本免费的电子书使经验丰富的程序员更深入了解 Linux 中进程间通信IPC的核心概念和机制。
![Inter-process Communication in Linux][1]
让一个软件过程与另一个软件过程进行对话是一个微妙的平衡行为。但是它对于应用程序而言可能是至关重要的功能因此这是任何从事复杂项目的程序员都必须解决的问题。你的应用程序是否需要启动由其它软件处理的工作监视外设或网络上正在执行的操作或者检测来自其它来源的信号当你的软件需要依赖其自身代码之外的东西来知道下一步做什么或什么时候做时你就需要考虑进程间通信IPC
Unix 操作系统在很久以前就说明了这一点这可能是因为人们早就期望软件会来自各种来源。按照相同的传统Linux 为 IPC 和一些新接口提供了许多相同的接口。Linux 内核具有几个 IPC 方法,[util-linux 包][2]包含 `ipcmk`、`ipcrm`、`ipcs` 和 `lsipc` 命令,用于监视和管理 IPC 消息。
### 显示进程间通信信息
在尝试 IPC 之前,你应该知道系统上已经有哪些 IPC 工具。`lsipc` 命令提供了该信息。
```
RESOURCE DESCRIPTION LIMIT USED USE%
MSGMNI Number of message queues 32000 0 0.00%
MSGMAX Max size of message (byt.. 8192 - -
MSGMNB Default max size of queue 16384 - -
SHMMNI Shared memory segments 4096 79 1.93%
SHMALL Shared memory pages 184[...] 25452 0.00%
SHMMAX Max size of shared memory 18446744073692774399
SHMMIN Min size of shared memory 1 - -
SEMMNI Number of semaphore ident 32000 0 0.00%
SEMMNS Total number of semaphore 1024000.. 0 0.00%
SEMMSL Max semaphores per semap 32000 - -
SEMOPM Max number of operations p 500 - -
SEMVMX Semaphore max value 32767 - -
```
你可能注意到,这个示例清单包含三种不同类型的 IPC 机制,每种机制在 Linux 内核中都是可用的消息MSG、共享内存SHM和信号量SEM。你可以用 `ipcs` 命令查看每个子系统的当前活动:
```
$ ipcs
\------ Message Queues Creators/Owners ---
msqid perms cuid cgid [...]
\------ Shared Memory Segment Creators/Owners
shmid perms cuid cgid [...]
557056 700 seth users [...]
3571713 700 seth users [...]
2654210 600 seth users [...]
2457603 700 seth users [...]
\------ Semaphore Arrays Creators/Owners ---
semid perms cuid cgid [...]
```
这表明当前没有消息或信号量数组,但是使用了一些共享内存段。
你可以在系统上执行一个简单的示例,这样就可以看到其中一个系统正在工作。它涉及到一些 C 代码,所以你必须在系统上有构建工具。必须安装才能从源代码构建的软件包的名称取决于发行版,因此请参考文档以获取详细信息。例如,在基于 Debian 的发行版上,你可以在 wiki 的[构建教程][3]部分了解构建需求,而在基于 Fedora 的发行版上,你可以参考文档的[从源代码安装软件][4]部分。
### 创建一个消息队列
你的系统已经有一个默认的消息队列,但是你可以使用 `ipcmk` 命令创建你自己的消息队列:
```
$ ipcmk --queue
Message queue id: 32764
```
编写一个简单的 IPC 消息发送器,为了简单,在队列 ID 中硬编码:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
struct msgbuffer {
char text[24];
} message;
int main() {
int msqid = 32764;
strcpy(message.text,"opensource.com");
msgsnd(msqid, &message, sizeof(message), 0);
printf("Message: %s\n",message.text);
printf("Queue: %d\n",msqid);
return 0;
}
```
编译应用程序并运行:
```
$ gcc msgsend.c -o msg.bin
$ ./msg.bin
Message: opensource.com
Queue: 32769
```
你刚刚向消息队列发送了一条消息。你可以使用 `ipcs` 命令验证这一点,使用 `\——queue` 选项将输出限制到消息队列:
```
$ ipcs -q
\------ Message Queues --------
key msqid owner perms used-bytes messages
0x7b341ab9 0 seth 666 0 0
0x72bd8410 32764 seth 644 24 1
```
你也可以检索这些讯息:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
struct msgbuffer {
char text[24];
} message;
int main() {
int msqid = 32764;
msgrcv(msqid, &amp;message, sizeof(message),0,0);
printf("\nQueue: %d\n",msqid);
printf("Got this message: %s\n", message.text);
msgctl(msqid,IPC_RMID,NULL);
return 0;
```
编译并运行:
```
$ gcc get.c -o get.bin
$ ./get.bin
Queue: 32764
Got this message: opensource.com
```
### 下载[电子书][5]
这只是 Marty Kalin 的 [Linux 内进程间通信指南][5]中课程的一个例子,这是可从 Opensource.com 下载的最新免费(和知识共享)电子书。在短短的几节课中,你将从消息队列、共享内存和信号量、套接字、信号等中了解 IPC 的 POSIX 方法。认真阅读 Marty 的书,您将成为一个消息灵通的程序员。而这不仅适用于经验丰富的编码人员,如果你编写的只是 shell 脚本,那么你将拥有有关管道(命名和未命名)和共享文件的大量实践知识,以及使用共享文件或外部消息队列时需要了解的重要概念。
如果你对制作具有动态和系统意识的优秀软件感兴趣,那么你需要了解 IPC。让[这本书][5]做你的向导。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/inter-process-communication-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[laingke](https://github.com/laingke)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_inter-process_communication_linux_520x292.png?itok=hPoen7oI (Inter-process Communication in Linux)
[2]: https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/
[3]: https://wiki.debian.org/BuildingTutorial
[4]: https://docs.pagure.org/docs-fedora/installing-software-from-source.html
[5]: https://opensource.com/downloads/guide-inter-process-communication-linux