消息队列和共享内存来管理进程间通信的C代码示例
时间: 2023-09-22 20:06:13 浏览: 105
### 回答1:
使用消息队列进行进程间通信: #include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h> // 消息结构体
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message; int main()
{
key_t key;
int msgid;
// 生成键值
key = ftok("progfile", 65);
// 创建消息队列
msgid = msgget(key, 0666 | IPC_CREAT);
// 向消息队列中写入消息
message.mesg_type = 1;
printf("请输入一条消息:");
fgets(message.mesg_text, 100, stdin);
msgsnd(msgid, &message, sizeof(message), 0);
// 从消息队列中读取消息
msgrcv(msgid, &message, sizeof(message), 1, 0);
// 打印消息
printf("收到的消息是: %s", message.mesg_text);
// 删除消息队列
msgctl(msgid, IPC_RMID, NULL);
return 0;
}使用共享内存进行进程间通信: #include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h> int main()
{
// 标志符
key_t key;
int shmid;
// 生成键值
key = ftok("progfile", 65);
// 创建共享内存
shmid = shmget(key, 1024, 0666 | IPC_CREAT);
// 指向共享内存
char *str = (char*) shmat(shmid,(void*)0,0);
printf("请输入一条消息:");
gets(str);
printf("收到的消息是: %s\n", str);
// 断开共享内存
shmdt(str);
// 删除共享内存
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
### 回答2:
消息队列和共享内存是常用的进程间通信方式。下面是一个使用消息队列和共享内存来管理进程间通信的C代码示例:
使用消息队列:
```
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_SIZE 100
// 定义消息结构体
struct message {
long mtype;
char mtext[MAX_MSG_SIZE];
};
int main() {
key_t key;
int msgid;
struct message msg;
// 生成唯一的key值
key = ftok(".", 1);
// 创建消息队列
msgid = msgget(key, IPC_CREAT | 0666);
if (msgid == -1) {
perror("msgget");
return 1;
}
// 发送消息
msg.mtype = 1;
strcpy(msg.mtext, "Hello, message queue!");
if (msgsnd(msgid, &msg, strlen(msg.mtext)+1, 0) == -1) {
perror("msgsnd");
return 1;
}
// 接收消息
if (msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0) == -1) {
perror("msgrcv");
return 1;
}
printf("Received message: %s\n", msg.mtext);
// 删除消息队列
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl");
return 1;
}
return 0;
}
```
使用共享内存:
```
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
key_t key;
int shmid;
char *shmaddr;
// 生成唯一的key值
key = ftok(".", 1);
// 创建共享内存
shmid = shmget(key, sizeof(char) * 100, IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget");
return 1;
}
// 将共享内存连接到当前进程地址空间
shmaddr = (char *)shmat(shmid, NULL, 0);
if (shmaddr == (char *)-1) {
perror("shmat");
return 1;
}
// 写入数据到共享内存
strcpy(shmaddr, "Hello, shared memory!");
// 从共享内存读取数据
printf("Shared memory: %s\n", shmaddr);
// 断开与共享内存的连接
if (shmdt(shmaddr) == -1) {
perror("shmdt");
return 1;
}
// 删除共享内存
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
return 1;
}
return 0;
}
```
以上代码分别演示了使用消息队列和共享内存进行进程间通信的过程。消息队列通过msgget、msgsnd和msgrcv进行消息的发送和接收;共享内存通过shmget、shmat、shmdt和shmctl进行共享内存的创建、连接、断开和删除。
### 回答3:
下面是一个使用消息队列和共享内存来管理进程间通信的C代码示例:
使用消息队列进行进程间通信:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_SIZE 10
struct message {
long msg_type;
char msg_text[MAX_MSG_SIZE];
};
int main() {
key_t key;
int msgqid;
struct message msg_send, msg_receive;
// 创建消息队列
key = ftok(".", 'a');
msgqid = msgget(key, IPC_CREAT | 0666);
if (msgqid == -1) {
perror("msgget");
exit(1);
}
// 发送消息
msg_send.msg_type = 1;
strcpy(msg_send.msg_text, "Hello");
msgsnd(msgqid, &msg_send, sizeof(msg_send.msg_text), 0);
// 接收消息
msgrcv(msgqid, &msg_receive, sizeof(msg_receive.msg_text), 1, 0);
printf("Received message: %s\n", msg_receive.msg_text);
// 删除消息队列
msgctl(msgqid, IPC_RMID, NULL);
return 0;
}
```
使用共享内存进行进程间通信:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
int main() {
key_t key;
int shmid;
char* data;
// 创建共享内存
key = ftok(".", 'b');
shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget");
exit(1);
}
// 连接共享内存
data = shmat(shmid, NULL, 0);
if (data == (char*)-1) {
perror("shmat");
exit(1);
}
// 写入共享内存
strcpy(data, "Hello");
// 从共享内存读取数据
printf("Received data: %s\n", data);
// 分离共享内存
shmdt(data);
// 删除共享内存
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
```
上面的两个示例分别使用了消息队列和共享内存来实现进程间通信。消息队列可以发送和接收消息,而共享内存可以用于简单的数据共享。请注意,这只是基本示例,实际应用中可能需要更多的错误处理和同步机制。