作业:要求用消息队列实现AB进程对话

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define SND 100
#define RCV 101

struct msgbuf
{
    long mtype;
    char mtext[128];
};


int main(int argc, const char *argv[])
{
    //获取key值
    key_t key = ftok("./", 2);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("key = %#X\n", key);

    //创建消息队列
    int msqid = msgget(key, IPC_CREAT|0664);
    if(msqid < 0)                                                                                                                    
    {
        perror("msgget");
        return -1;
    }
    printf("msqid = %d\n", msqid);

    struct msgbuf snd = {SND};  //发送给类型为100的消息包
    struct msgbuf rcv;
    ssize_t res = 0;
    while(1)
    {
        bzero(snd.mtext, sizeof(snd.mtext));
        //向消息队列中发送数据
        printf("请输入>>>");
        fgets(snd.mtext, sizeof(snd.mtext), stdin);
        snd.mtext[strlen(snd.mtext)-1] = 0;

        if(msgsnd(msqid, &snd, sizeof(snd.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }

        if(strcmp(snd.mtext, "quit") == 0)
            break;

        memset(&rcv, 0, sizeof(rcv));
        //从消息队列中读取数据
        res=msgrcv(msqid, &rcv, sizeof(rcv.mtext), RCV, 0);
        if(res < 0)
        {
            perror("msgrcv");
            return -1;
        }
        printf("%ld : %s\n", rcv.mtype, rcv.mtext);
        if(strcmp(rcv.mtext, "quit") == 0)
        {
            //删除消息队列
            msgctl(msqid, IPC_RMID, NULL);
            break;
        }
    }

    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define SND 101
#define RCV 100

struct msgbuf
{
    long mtype;
    char mtext[128];
};


int main(int argc, const char *argv[])
{
    //获取key值
    key_t key = ftok("./", 2);
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("key = %#X\n", key);

    //创建消息队列
    int msqid = msgget(key, IPC_CREAT|0664);                                                                                                                              
    if(msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("msqid = %d\n", msqid);

    struct msgbuf snd = {SND};  //发送给类型为100的消息包
    struct msgbuf rcv;
    ssize_t res = 0;
    while(1)
    {
        memset(&rcv, 0, sizeof(rcv));
        //从消息队列中读取数据
        res=msgrcv(msqid, &rcv, sizeof(rcv.mtext), RCV, 0);
        if(res < 0)
        {
            perror("msgrcv");
            return -1;
        }
        printf("%ld : %s\n", rcv.mtype, rcv.mtext);

        if(strcmp(rcv.mtext, "quit") == 0)
        {
            //删除消息队列
            msgctl(msqid, IPC_RMID, NULL);
            break;
        }

        bzero(snd.mtext, sizeof(snd.mtext));
        //向消息队列中发送数据
        printf("请输入>>>");
        fgets(snd.mtext, sizeof(snd.mtext), stdin);
        snd.mtext[strlen(snd.mtext)-1] = 0;

        if(msgsnd(msqid, &snd, sizeof(snd.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }
        if(strcmp(snd.mtext, "quit") == 0)
            break;
    }

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值