Commit f3df337e authored by 马伊齐's avatar 马伊齐

进程间信号通信(完成)

parent ad3e990b
...@@ -13,10 +13,14 @@ ...@@ -13,10 +13,14 @@
#include <sys/msg.h> #include <sys/msg.h>
#include <mqueue.h> #include <mqueue.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h>
Message messageQueue[MAX_MESSAGES]; // 消息队列 #define SHM_KEY 1234
int messageCount = 0; // 消息队列中消息数量
int msgid = -1; // 全局变量,用于存储消息队列ID int msgid = -1; // 全局变量,用于存储消息队列ID
int shmid;
messageQueue *queue;
// socket信息结构体 // socket信息结构体
typedef struct typedef struct
...@@ -284,41 +288,86 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len) ...@@ -284,41 +288,86 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
return 0; return 0;
} }
int msg_local_recv_pulse(uint16_t *type, uint32_t *code) int msg_local_ssignal_init(void)
{ {
if (messageCount > 0)
// 获取已存在的共享内存
if ((shmid = shmget(SHM_KEY, sizeof(int), IPC_CREAT | 0666)) < 0) {
perror("shmget");
return -1;
}
// 将共享内存连接到进程地址空间
if ((queue = shmat(shmid, NULL, 0)) == (messageQueue *)-1)
{ {
*type = messageQueue[0].type; perror("shmat");
*code = messageQueue[0].code; return -1;
// 移动队列 }
for (int i = 0; i < messageCount - 1; i++) queue->front = 0;
queue->rear = -1;
}
int msg_local_rsignal_init(void)
{
shmid = shmget(SHM_KEY, sizeof(int), 0666);
// 获取已存在的共享内存
if ((shmid = shmget(SHM_KEY, sizeof(int), 0666)) < 0) {
perror("shmget");
return -1;
}
// 将共享内存连接到进程地址空间
if ((queue = shmat(shmid, NULL, 0)) == (messageQueue *)-1)
{ {
messageQueue[i] = messageQueue[i + 1]; perror("shmat");
return -1;
} }
messageCount--; }
return 0; // 成功
int msg_local_send_pulse(uint16_t type, uint32_t code) {
if (queue->rear == queue->front)
{
printf("signal queue already full\n");
return -1;
}
else if (queue->rear == -1)
{
queue->rear = 0;
queue->front = 1;
queue->mQueue[0].type = type;
queue->mQueue[0].code = code;
return 0;
} }
else else
{ {
errno = EAGAIN; // 队列空 queue->mQueue[queue->front].type = type;
return -1; // 失败 queue->mQueue[queue->front].code = code;
queue->front = (queue->front + 1) % MAX_QSIZE;
return 0;
} }
} }
int msg_local_send_pulse(uint16_t type, uint32_t code) int msg_local_recv_pulse(uint16_t *type, uint32_t *code) {
{
if (messageCount < MAX_MESSAGES) if (queue->rear == -1)
{ {
messageQueue[messageCount].type = type; printf("Null signal queue\n");
messageQueue[messageCount].code = code; return -1;
messageCount++; }
return 0; // 成功 else if ((queue->rear + 1) % MAX_QSIZE == queue->front)
{
*type = queue->mQueue[queue->rear].type;
*code = queue->mQueue[queue->rear].code;
queue->rear = -1;
return 0;
} }
else else
{ {
errno = EAGAIN; // 队列满 *type = queue->mQueue[queue->rear].type;
return -1; // 失败 *code = queue->mQueue[queue->rear].code;
queue->rear = (queue->rear + 1) % MAX_QSIZE;
return 0;
} }
} }
int msg_recv(int channel, msg_data_t *msg) int msg_recv(int channel, msg_data_t *msg)
......
...@@ -7,10 +7,25 @@ ...@@ -7,10 +7,25 @@
// 读写模式宏定义 // 读写模式宏定义
#define SOCK_READ 0x01 #define SOCK_READ 0x01
#define SOCK_WRITE 0x02 #define SOCK_WRITE 0x02
#define MAX_QSIZE 100 // 消息队列最大长度
#define BUFFER_SIZE 4096 // 缓冲区大小 #define BUFFER_SIZE 4096 // 缓冲区大小
#define MAX_CLIENTS 10 // 最大连接客户端数量 #define MAX_CLIENTS 10 // 最大连接客户端数量
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef struct message{
uint16_t type;
uint32_t code;
}message;
typedef struct messageQueue{
int front;
int rear;
message mQueue[MAX_QSIZE];
}messageQueue;
/** 定义函数指针类型 */ /** 定义函数指针类型 */
typedef int (*sock_callback_t)(int, uint8_t *, int); typedef int (*sock_callback_t)(int, uint8_t *, int);
...@@ -188,6 +203,11 @@ int msg_local_recv(msg_data_t *msg); ...@@ -188,6 +203,11 @@ int msg_local_recv(msg_data_t *msg);
*/ */
int msg_local_send(uint16_t type, uint32_t code, void *pad, int len); int msg_local_send(uint16_t type, uint32_t code, void *pad, int len);
// 以下三个函数是用于进程间信号通讯的
int msg_local_ssignal_init(void);
int msg_local_rsignal_init(void);
int msg_local_recv_pulse(uint16_t *type, uint32_t *code);
/** /**
* @brief 本地进程间发送信号(整形) * @brief 本地进程间发送信号(整形)
* *
...@@ -197,6 +217,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len); ...@@ -197,6 +217,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len);
*/ */
int msg_local_send_pulse(uint16_t type, uint32_t code); int msg_local_send_pulse(uint16_t type, uint32_t code);
/** /**
* @brief 接收消息,主要用于进程间消息通讯 * @brief 接收消息,主要用于进程间消息通讯
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment