当前位置:网站首页>Interprocess communication
Interprocess communication
2022-06-30 04:25:00 【Tra220123】
One 、 name pipes FIFO
FIFO IPC Mechanism : Using system files in the file system to identify . It can be used mkfifo Command to create a FIFO file :
mkfifo tube
ls -l tube
FIFO File has no data block on disk , It is only used to identify one channel in the kernel , Each process can open this file for read/write, It's actually reading and writing kernel channels ( It turns out that file What the structure points to read,write Functions are different from regular files ), To achieve inter process communication .
Two 、 Shared memory
1. Shared memory
Allow two or more processes to share a given storage area . Because the data does not need to be copied between the client and the server .


2. The first function to call is shmget, Get a shared storage identifier of the specified size .
![]()
①key: Used to identify shared memory size Parameter the minimum value of the shared memory segment , If you are creating a new segment , Must specify size. If you are storing an existing segment , take size Designated as 0.
②shmflg:IPC_CREATE and IPC_EXCL, The most important thing is shmflg Specify access rights in , Follow open Of mode Same parameter . Otherwise appear permission denied error .
③ return : If successful, it is shared memory ID, Error is -1.
④key from ftok() Generate .pathname Must be accessible to the calling process .pathname and proj_id Make up one key.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
key_t key = ftok("./9_1readfifo.c", 9);
if (key < 0) {
perror("ftok");
exit(1);
}
printf("key=0x%x\n", key);
int shmid = shmget(key, 20, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget");
exit(1);
}
printf("shmid=%d\n", key);
return 0;
}3. Call once a shared storage segment is created , The process can call shmat Connect it to its address space .
![]()
① return : Pointer to shared storage segment if successful , Error is -1.
② The address of the calling process to which the shared memory segment is connected addr Parameters and flag Specify in SHM_RND Bit related :
If addr=NULL: This segment connects to the first available address selected by the kernel .
If addr Not NULL, There is no designation SHM_RND: This segment connects to addr At the specified address .
If addr Not 0, And specify SHM_RND: This segment connects to addr-(addr mod SHMLBA) On the address of .
SHM_RND: integer .
SHM_LBA: Low boundary address multiples , Always 2 Power of .
This expression takes the address down to the nearest 1 individual SHMLBA Multiple .
③ General designation addr=0, So that the kernel can choose the address .
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(void) {
key_t key = ftok("./9_1readfifo.c", 9);
if (key < 0) {
perror("ftok");
exit(1);
}
printf("key=0x%x\n", key);
int shmid = shmget(key, 20, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget");
exit(1);
}
printf("shmid=%d\n", key);
void *shmp = shmat(shmid, NULL, 0);
if (shmp < 0) {
perror("shmat");
exit(1);
}
printf("shmp=%p\n", shmp);
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid) {
//parent process
while (1) {
scanf("%s", shmp);
if (!strcmp(shmp, "quit")) break;
}
wait(NULL);
} else {
//child process
while (1) {
if (!strcmp(shmp, "quit")) break;
if (shmp) {
printf("child read %s\n", shmp);
bzero(shmp, 20);
}
sleep(1);
}
}
shmdt(shmp);
return 0;
}
4. Call when the operation on the shared storage segment has ended , Call shmdt Disconnect this section .
( Its identifier and its data structure are not deleted from the system . The identifier remains until a process calls shmctl With command IPC_RMID Specifically delete it )
![]()
shmaddr Parameters : Previous call shmat Returns a value of : Return success as 0, Error for -1.
5. call shmctl Perform multiple operations on shared storage segments
![]()
3、 ... and 、 Message queue
1. The system kernel maintains a queue for storing messages , Different users can send messages to the queue or receive messages in the queue .


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>
int main() {
key_t key = ftok("./tube", 9);
printf("mqid=%#x\n", key);
int mqid = msgget(key, IPC_CREAT | 0666);
printf("mqid=%d\n", mqid);
return 0;
}2. Send a message to the queue . This operation will not be restarted after being interrupted ( In signal processing SA_RESTART)
![]()
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>
#define MSGLEN 20
typedef struct msgbuf
{
long mtype;
char mtext[MSGLEN];
}MSG;
int main() {
key_t key = ftok("./tube", 9);
printf("mqid=%#x\n", key);
int mqid = msgget(key, IPC_CREAT | 0666);
printf("mqid=%d\n", mqid);
MSG msg;
msg.mtype = 1;
strncpy(msg.mtext, "how are you?\n", MSGLEN);
msgsnd(mqid, &msg, MSGLEN, 0);
msg.mtype = 2;
strncpy(msg.mtext, "haha\n", MSGLEN);
msgsnd(mqid, &msg, MSGLEN, 0);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>
#define MSGLEN 20
typedef struct msgbuf
{
long mtype;
char mtext[MSGLEN];
}MSG;
int main() {
key_t key = ftok("./tube", 9);
printf("mqid=%#x\n", key);
int mqid = msgget(key, IPC_CREAT | 0666);
printf("mqid=%d\n", mqid);
MSG msg;
msgrcv(mqid, &msg, MSGLEN, 2, 0);
printf("msg.type=%ld\nmsg,text=%s\n", msg.mtype, msg.mtext);
msgrcv(mqid, &msg, MSGLEN, 1, 0);
printf("msg.type=%ld\nmsg,text=%s\n", msg.mtype, msg.mtext);
return 0;
} 
边栏推荐
- A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
- [从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
- 基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
- Unity 在编辑器中输入字符串时,转义字符的输入
- internship:接口案例实现
- How to solve the problem of link hyperlinks when trying to link the database?
- FortiGate firewall modifies the default timeout of a session
- lego_loam 代码阅读与总结
- Explain the underlying principles of JVM garbage collection in simple terms
- FortiGate firewall filters the specified session and cleans it up
猜你喜欢

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

Matlab reads fig file and restores signal

How to use div boxes to simulate line triangles

Named pipes for interprocess communication

Threejs实现模拟河流,水面水流,水管水流,海面
![[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)](/img/08/b390810d457af5e4470d9743b01ca1.png)
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)

JS inheritance

基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】

Day 12 advanced programming techniques

I spent three years in a big factory outsourcing, which subverted my understanding!
随机推荐
Default value of JS parameter
FortiGate firewall modifies the default timeout of a session
Unity 在編輯器中輸入字符串時,轉義字符的輸入
Es2018 key summary
OneNote production schedule
Junior students summarize JS basic interview questions
Anonymous pipeline for interprocess communication
win10系统使用浏览器下载后,内容无故移动或删除
Errno and PERROR
Use of thread pool
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
Indefinite parameters of JS function
base64.c
JS import and export
Myrpc version 1
RPC correction based on arcpy API
JS inheritance
487-3279(POJ1002)
管道实现进程间通信之命名管道
Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL