当前位置:网站首页>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;
}
边栏推荐
- Error in conditional filter (if) syntax in sum function in SQL Server2005
- 两个月拿到N个offer,什么难搞的面试官在我这里都不算事
- AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
- El upload Upload file (Manual upload, Automatic upload, upload progress)
- Myrpc version 0
- Internship: interface case implementation
- The same node code will cause duplicate data
- Myrpc version 5
- 2021-07-14
- JS static method
猜你喜欢
Junior students summarize JS basic interview questions
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
el-upload上傳文件(手動上傳,自動上傳,上傳進度)
Junior students summarize JS advanced interview questions
Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
Es2017 key summary
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
管道实现进程间通信之命名管道
How the FortiGate firewall rejects a port by using the local in policy policy
How to solve the problem of link hyperlinks when trying to link the database?
随机推荐
Configure specific source IP in SLA detection of FortiGate sdwan
After the win10 system uses the browser to download, the content is moved or deleted without reason
Refers to the difference between IP and *ip at output
FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
数据链路层详解
Myrpc version 4
Junior students summarize JS advanced interview questions
Technology sharing | broadcast function design in integrated dispatching
Cloud native -- websocket of Web real-time communication technology
深度融合云平台,对象存储界的“学霸”ObjectScale来了
I spent three years in a big factory outsourcing, which subverted my understanding!
Error encountered in SQL statement, solve
Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
lego_loam 代码阅读与总结
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
Junior students summarize JS basic interview questions
el-upload上传文件(手动上传,自动上传,上传进度)