作业4.5

该文提供了两个C语言程序,分别展示了使用消息队列进行AB进程间的对话,以及通过共享内存实现数据打印和倒置。在消息队列示例中,进程A和B通过消息队列交换信息,当接收到quit时终止。在共享内存示例中,一个进程打印数据,另一个进程则倒置数据,共享内存包含一个标志和字符串。
摘要由CSDN通过智能技术生成

作业1:

要求实现AB进程对话
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
重复1.2步骤,当收到quit后,要结束AB进程
提示:两根管道
提示:用一个消息队列,两种类型即可
当对方输入quit后,退出AB进程删除消息队列;

进程A:

// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
    long mytype;     // 消息类型
    char mtext[128]; // 消息内容
};

int main()
{
    // 创建消息队列
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    // 通过key值找到对应的消息队列
    int msqid = msgget(key, IPC_CREAT | 0664);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    struct msgbuf rcv;  // 接收
    struct msgbuf send; // 发送
    printf("请注意:本程序通讯类型为2\n");

    while (1)
    { // 接收阻塞
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 2, 0) < 0)
        {
            perror("msgrcv");
            return -1;
        }
        printf("收到:%s\n", rcv.mtext);
          if (strcmp(rcv.mtext, "quit") == 0)
            break;
        // 发送
        printf("请输入消息类型>>> ");
        scanf("%ld", &send.mytype);
        getchar();
        printf("请输入消息内容>>> ");
        scanf("%s", send.mtext);
        getchar();
        // 发送数据到消息队列中
        if (msgsnd(msqid, &send, sizeof(send.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }
        printf("发送成功\n");
        if (strcmp(send.mtext, "quit")==0) // 如果发送quit,跳出循环
            break;
    }

  return 0;
}

进程B:

// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
    long mytype;     // 消息类型
    char mtext[128]; // 消息内容
};

int main()
{
    // 创建消息队列
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    // 通过key值找到对应的消息队列
    int msqid = msgget(key, IPC_CREAT | 0664);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    struct msgbuf rcv;  // 接收
    struct msgbuf send; // 发送
    printf("请注意:本程序通讯类型为1\n");
    while (1)
    {
        /***发送***/
        printf("请输入消息类型:\t ");
        scanf("%ld", &send.mytype);
        getchar();
        printf("请输入消息内容:\t ");
        scanf("%s", send.mtext);
        getchar();
        if (msgsnd(msqid, &send, sizeof(send.mtext), 0) < 0) // 发送数据到消息队列中
        {
            perror("msgsnd");
            return -1;
        }
        printf("发送成功\n");
        if (strcmp(send.mtext, "quit") == 0) // 如果发送qUit,跳出循环
            break;
        /***接收阻塞***/
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 1, 0) < 0)
        {
            perror("msgrcv");
            return -1;
        }
        if (strcmp(rcv.mtext, "quit") == 0) // 如果接收到quit,跳出循环
            break;
        printf("收到:%s\n", rcv.mtext);
    }

    return 0;
}

作业2:

一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串

逆置程序:

// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*
 程序要求:一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
*/

int main()
{

    // key_t ftok(const char *pathname, int proj_id);
    key_t key = ftok("/home/ubuntu/", 'a'); // 创建
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    // int shmget(key_t key, size_t size, int shmflg);
    int shmid = shmget(key, 32, IPC_CREAT | 0664);
    if (shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    // 将共享内存映射到用户空间中
    void *addr = shmat(shmid, NULL, 0);
    if ((void *)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    /*倒置程序段*/

    int *flag = (int *)addr;
    *flag = 0;
    char *str = (char *)addr + 4;
    strcpy(str, "hello world");
    while (1)
    {
        if (*flag == 1)
        {
            int i = 0, j = strlen(str) - 1;
            while (i < j)
            {
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
                i++;
                j--;
            }
           * flag = 0;
        }
    }
    return 0;
}

输出程序:

// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*
 程序要求:一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
*/

int main()
{
    // key_t ftok(const char *pathname, int proj_id);
    key_t key = ftok("/home/ubuntu/", 'a'); // 创建
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    // int shmget(key_t key, size_t size, int shmflg);
    int shmid = shmget(key, 32, IPC_CREAT | 0664);
    if (shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    // 将共享内存映射到用户空间中
    void *addr = shmat(shmid, NULL, 0);
    if ((void *)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    /*输出段*/
    int *flag = (int *)addr;
    while (1)
    {
        if (*flag == 0)
        {
            printf("%s\n", (char *)addr + 4);
            *flag = 1;
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值