当前位置:网站首页>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;
} 
边栏推荐
- With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
- Implementation steps of dynamic proxy
- Blue Bridge Cup: magic cube rotation [Vocational group]
- lego_ Reading and summary of loam code
- [从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
- JS reflect
- 基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
- Salary management system based on servlet+jsp+mysql [source code + database]
- Project safety and quality
- 进程间通信之匿名管道
猜你喜欢

A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"

Matlab reads fig file and restores signal

Myrpc version 4

Educoder group purchase suspension box page production

An error occurs when sqlyog imports the database. Please help solve it!

How to use div boxes to simulate line triangles

管道实现进程间通信之命名管道

Technology sharing | broadcast function design in integrated dispatching

Myrpc version 6

破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
随机推荐
base64.c
JS inheritance
The same node code will cause duplicate data
base64.c
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
Error encountered in SQL statement, solve
Project safety and quality
How to solve the problem of link hyperlinks when trying to link the database?
7-3 打怪升级 单源最短路
Configure specific source IP in SLA detection of FortiGate sdwan
An error occurs when sqlyog imports the database. Please help solve it!
[learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
FortiGate firewall modifies the default timeout of a session
各位大佬,flink 1.13.6,mysql-cdc2.2.0,抽取上来的datetime(6)类
OneNote software
小C的数组(array)
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
Concatenation of Languages(UVA10887)
Titanic(POJ2361)
JS import and export