2024.8.6 作业

使用消息队列完成两个进程之间相互通信

代码:

/*******************************************/

文件名:snd.c

/*******************************************/

#include <myhead.h>
sem_t sem;
//要发送的消息类型
struct msgbuf
{
    long mtype;
    char mtext[1024];
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
void *task1(void *arg)
{
    //创建key值,用于生产信息队列
    key_t key = ftok("/", 'k');
    if (key == -1)
    {
        perror("ftok error");
        return NULL;
    }
    //创建一个消息队列
    int msqid = msgget(key, IPC_EXCL | 0664);
    if (msqid == -1)
    {
        perror("msgget error");
        return NULL;
    }
    //向消息队列中存放消息
    struct msgbuf buf2;
    while (1)
    {
        sem_wait(&sem);
        buf2.mtype=1;
        printf("请输入消息正文:");
        fgets(buf2.mtext, SIZE, stdin);        //从终端获取数据
        buf2.mtext[strlen(buf2.mtext) - 1] = 0; //将换行换成 '\0'
        //将消息发送到消息队列中
        msgsnd(msqid, &buf2, SIZE, 0);
        printf("发送成功\n");
        if (strcmp(buf2.mtext, "quit") == 0)
        {
            break;
        }
    }
    pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{
    //创建key值,用于生产消息队列
    key_t key2 = ftok("/", 't');
    if (key2 == -1)
    {
        perror("ftok error");
        return NULL;
    }
    printf("key = %#x\n", key2);
    //通过key值创建一个消息队列
    int msqid = msgget(key2, IPC_CREAT | 0664);
    if (msqid == -1)
    {
        perror("msgget error");
        return NULL;
    }
    printf("msqid = %d\n", msqid); //id号
    //从消息队列中读取消息
    struct msgbuf buf;
    while (1)
    {
        msgrcv(msqid, &buf, SIZE, 0, 0);
        if (strcmp(buf.mtext, "quit") == 0)
        {
            sem_post(&sem);
            break;
        }
        printf("收到消息为:%s\n", buf.mtext);
        sem_post(&sem);
    }
    pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{
    //初始化无名信号量
    sem_init(&sem, 0, 1);
    //定义变量存储线程号
    pthread_t tido = -1;
    if (pthread_create(&tido, NULL, task1, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //定义变量存储线程号
    pthread_t tido2 = -1;
    if (pthread_create(&tido2, NULL, task2, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //阻塞回收线程的资源
    pthread_join(tido, NULL);
    pthread_join(tido2, NULL);
    //销毁无名信号量
    sem_destroy(&sem);
    return 0;
}

/*******************************************/

文件名:rvs.c

/*******************************************/

#include <myhead.h>
sem_t sem;
//要发送的消息类型
struct msgbuf
{
    long mtype;
    char mtext[1024];
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
void *task2(void *arg)
{
    //创建key值,用于生产信息队列
    key_t key2 = ftok("/", 't');
    if (key2 == -1)
    {
        perror("ftok error");
        return NULL;
    }
    //创建一个消息队列
    int msqid2 = msgget(key2, IPC_EXCL | 0664);
    if (msqid2 == -1)
    {
        perror("msgget error");
        return NULL;
    }
    //向消息队列中存放消息
    struct msgbuf buf;
    while (1)
    {
        sem_wait(&sem);
        buf.mtype=2;
        printf("请输入消息正文:");
        fgets(buf.mtext, SIZE, stdin);        //从终端获取数据
        buf.mtext[strlen(buf.mtext) - 1] = 0; //将换行换成 '\0'
        //将消息发送到消息队列中
        msgsnd(msqid2, &buf, SIZE, 0);
        printf("发送成功\n");
        usleep(10);
        if (strcmp(buf.mtext, "quit") == 0)
        {
            break;
        }
    }
    pthread_exit(NULL); //退出线程
}
void *task1(void *arg)
{
    //创建key值,用于生产消息队列
    key_t key = ftok("/", 'k');
    if (key == -1)
    {
        perror("ftok error");
        return NULL;
    }
    printf("key = %#x\n", key);
    //通过key值创建一个消息队列
    int msqid = msgget(key, IPC_CREAT | 0664);
    if (msqid == -1)
    {
        perror("msgget error");
        return NULL;
    }
    printf("msqid = %d\n", msqid); //id号
    //从消息队列中读取消息
    struct msgbuf buf2;
    while (1)
    {
        usleep(10);
        msgrcv(msqid, &buf2, SIZE, 0, 0);
        if(buf2.mtype==2){
            continue;
        }
        if (strcmp(buf2.mtext, "quit") == 0)
        {
            sem_post(&sem);
            break;
        }
        printf("收到消息为:%s\n", buf2.mtext);
        sem_post(&sem);
    }
    pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{
    //初始化无名信号量
    sem_init(&sem, 0, 0);
    //定义变量存储线程号
    pthread_t tido = -1;
    if (pthread_create(&tido, NULL, task1, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //定义变量存储线程号
    pthread_t tido2 = -1;
    if (pthread_create(&tido2, NULL, task2, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //阻塞回收线程的资源
    pthread_join(tido, NULL);
    pthread_join(tido2, NULL);
    //销毁无名信号量
    sem_destroy(&sem);
    return 0;
}

结果:

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值