当前位置:网站首页>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;
} 
边栏推荐
- Idea grey screen problem
- Tea mall system based on SSM framework [project source code + database script + report]
- 基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
- JS file block to Base64 text
- Iterator of JS
- Educoder group purchase suspension box page production
- FortiGate firewall quick initialization administrator password
- Redis cache avalanche, breakdown and penetration
- JS deconstruction assignment
- FortiGate firewall filters the specified session and cleans it up
猜你喜欢

How to use div boxes to simulate line triangles

深度融合云平台,对象存储界的“学霸”ObjectScale来了

MySQL DDL change

Myrpc version 0

Junior students summarize JS basic interview questions

输入输出及中断技术——微机第六章学习笔记

Myrpc version 6
![[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)

Blocking queue example

Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
随机推荐
errno和perror
Myrpc version 4
Equity interest [non DP]
Pig-Latin (UVA492)
Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
Blue Bridge Cup: magic cube rotation [Vocational group]
JS deconstruction assignment
base64.c
7-3 打怪升级 单源最短路
El upload Upload file (Manual upload, Automatic upload, upload progress)
Redis cache avalanche, breakdown and penetration
Titanic(POJ2361)
How to solve the problem of link hyperlinks when trying to link the database?
Clients accessing the daytime service (TCP)
Create thread in callable mode
Everyone, Flink 1.13.6, mysql-cdc2.2.0, the datetime (6) class extracted
基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
管道实现进程间通信之命名管道
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
win10系统使用浏览器下载后,内容无故移动或删除