一.作业
1> 使用消息队列完成两个进程之间相互通信
A.c
#include <myhead.h>
// 要发送的消息类型
struct msgbuf
{
    long mtype;       /* message type, must be > 0 */
    char mtext[1024]; /* message data */
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
    pid_t pid = 0;
    pid = fork();
    if (pid > 0)
    {
        // 1、创建key值,用于生产消息队列
        key_t key = ftok("/", 'k');
        if (key == -1)
        {
            perror("ftok error");
            return -1;
        }
        // 2、通过key值创建一个消息队列
        int msqid = msgget(key, IPC_CREAT | 0664);
        if (msqid == -1)
        {
            perror("msgget error");
            return -1;
        }
        // 向消息队列中存放消息
        struct msgbuf buf={.mtype=1};
        while (1)
        {
            bzero(buf.mtext,sizeof(buf.mtext));
            printf("A发送的消息:\n");
            fgets(buf.mtext, SIZE, stdin); // 从终端获取数据
            buf.mtext[strlen(buf.mtext) - 1] = 0; // 将换行换成 '\0'
            msgsnd(msqid, &buf, SIZE, 0);
            printf("A发送成功\n");
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
        wait(NULL);
    }
    else if (pid == 0)
    {
        // 1、创建key值,用于生产消息队列
        key_t key = ftok("/", 'k');
        if (key == -1)
        {
            perror("ftok error");
            return -1;
        }
        // 2、通过key值创建一个消息队列
        int msqid = msgget(key, IPC_CREAT | 0664);
        if (msqid == -1)
        {
            perror("msgget error");
            return -1;
        }
        struct msgbuf buf;
        while (1)
        {
            bzero(buf.mtext,sizeof(buf.mtext));
            msgrcv(msqid, &buf, SIZE, 2, 0);
            printf("A收到消息为:%s\n", buf.mtext);
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
        exit(EXIT_SUCCESS);
    }
    else
    {
        perror("fork error");
        return -1;
    }
    return 0;
}
 
B.c
#include <myhead.h>
// 要发送的消息类型
struct msgbuf
{
    long mtype;       /* message type, must be > 0 */
    char mtext[1024]; /* message data */
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
    pid_t pid = 0;
    pid = fork();
    if (pid > 0)
    {
        // 1、创建key值,用于生产消息队列
        key_t key = ftok("/", 'k');
        if (key == -1)
        {
            perror("ftok error");
            return -1;
        }
        // 2、通过key值创建一个消息队列
        int msqid = msgget(key, IPC_CREAT | 0664);
        if (msqid == -1)
        {
            perror("msgget error");
            return -1;
        }
        // 向消息队列中存放消息
        struct msgbuf buf={.mtype=2};
        while (1)
        {
            bzero(buf.mtext,sizeof(buf.mtext));
             printf("B发送的消息:\n");
            fgets(buf.mtext, SIZE, stdin); // 从终端获取数据
            buf.mtext[strlen(buf.mtext) - 1] = 0; // 将换行换成 '\0'
            msgsnd(msqid, &buf, SIZE, 0);
            printf("B发送成功\n");
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
        wait(NULL);
    }
    else if (pid == 0)
    {
        // 1、创建key值,用于生产消息队列
        key_t key = ftok("/", 'k');
        if (key == -1)
        {
            perror("ftok error");
            return -1;
        }
        // 2、通过key值创建一个消息队列
        int msqid = msgget(key, IPC_CREAT | 0664);
        if (msqid == -1)
        {
            perror("msgget error");
            return -1;
        }
        struct msgbuf buf;
        while (1)
        {
            bzero(buf.mtext,sizeof(buf.mtext));
            msgrcv(msqid, &buf, SIZE, 1, 0);
            printf("B收到消息为:%s\n", buf.mtext);
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
        exit(EXIT_SUCCESS);
    }
    else
    {
        perror("fork error");
        return -1;
    }
    return 0;
}
 
二.思维导图

![PEPM系统Cookie请求头远程代码执行漏洞复现 [附POC]](https://i-blog.csdnimg.cn/direct/6654cff2c3ce43969b271b490b786d18.png)


















