IO-day(9)-(消息队列、共享内存)

作业一、

要求实现AB进程对话

A进程先发送一句话给B进程,B进程接收后打印

B进程再回复一句话给A进程,A进程接收后打印

重复1.2步骤,当收到quit后,要结束AB进程

提示:两根管道

提示:用一个消息队列,两种类型即可

当对方输入quit后,退出AB进程删除消息队列;

main1.c

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

struct magbuf
{
    long mtype; // 消息内容
    char mtext[128];
};

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("ket = %#x\n", key);

    // 创建或获取消息队列的id号
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("msgget success msqid=%d\n", msqid);

    // 定义一个结构体变量去调用结构体msgbuf
    struct magbuf snd;
    struct magbuf rcv;

    while (1)
    {
        printf("请输入消息类型>>>");
        scanf("%ld", &snd.mtype);
        getchar();
        if (0 == snd.mtype)
            break;

        printf("请输入要发送的信息>>>");
        scanf("%s", snd.mtext);
        getchar();

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

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

        printf("发送成功\n");

        //==============================================================
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 0, 0) < 0)
        {
            perror("msgrcv");
            break;
        }

        printf("%ld : %s\n", rcv.mtype, rcv.mtext);

        if (strcmp(rcv.mtext, "quit") == 0)
        {
            break;
        }
        //==============================================================
    }

    system("ipcs -q");
    return 0;
}

main2.c

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

struct magbuf
{
    long mtype; // 消息内容
    char mtext[128];
};

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("ket = %#x\n", key);

    // 创建或获取消息队列的id号
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("msgget success msqid=%d\n", msqid);

    // 定义一个结构体变量去调用结构体msgbuf
    struct magbuf rcv;
    struct magbuf snd;

    while (1)
    {
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 0, 0) < 0)
        {
            perror("msgrcv");
            break;
        }

        if (strcmp(rcv.mtext, "quit") == 0)
        {
            break;
        }

        printf("%ld : %s\n", rcv.mtype, rcv.mtext);

        //==============================================================
        printf("请输入消息类型>>>");
        scanf("%ld", &snd.mtype);
        getchar();
        if (0 == snd.mtype)
            break;

        printf("请输入要发送的信息>>>");
        scanf("%s", snd.mtext);
        getchar();

        if (msgsnd(msqid, &snd, sizeof(snd.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }
        printf("发送成功\n");

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

    system("ipcs -q");
    return 0;
}

 结果:

 

作业二、

一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。

提示:共享内存中存储:flag+字符串

main1.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/shm.h>

int main(int argc,const char * argv[])
{
    key_t key = ftok("./",'a');
    if(key < 0)
    {
        perror("ftok");
        return -1;
    }

    //创建获取共享内存的shmid
    int shmid = shmget(key,32,IPC_CREAT|0777);
    if(shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    printf("shmid = %d\n", shmid);

    void *addr = shmat(shmid,NULL,0);
    if((void*)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    printf("addr = %p\n",addr);

    *(int *)addr = 1;
    while(1)
    {   
        if(*(int *)addr == 0)
        {
            continue;
        }
        printf("%s\n", (char *)addr+4);
        *(int *)addr = 1;
    }
   
    system("ipcs -m");
    return 0;
}

mian2.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/shm.h>

char str[] = "hello world";

int main(int argc, const char *argv[])
{
    key_t key = ftok("./", 'a');
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }

    // 创建获取共享内存的shmid
    int shmid = shmget(key, 32, IPC_CREAT | 0777);
    if (shmid < 0)
    {
        perror("shmget");
        return -1;
    }
    printf("shmid = %d\n", shmid);

    void *addr = shmat(shmid, NULL, 0);
    if ((void *)-1 == addr)
    {
        perror("shmat");
        return -1;
    }
    printf("addr = %p\n", addr);

    *(int *)addr = 0;
    while (1)
    {
        *(int *)addr = 0;
        int i = 0, j = 0;
        j = strlen(str) - 1;

        while (i < j)
        {
            char t = str[i];
            str[i] = str[j];
            str[j] = t;
            i++;
            j--;
        }

        char *ptr = (char *)addr + 4;
        strcpy(ptr, str);

        *(int *)addr = 1;
    }

    return 0;
}

结果:

 

 作业三、

在作业一的基础上实现实时收发

main1.c

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

struct magbuf
{
    long mtype; // 消息内容
    char mtext[128];
};

void *my_buf(void *arg)
{
    int msqid = *(int *)arg;

    struct magbuf snd;
    struct magbuf rcv;
    while (1)
    {
        printf("请输入消息类型>>>");
        scanf("%ld", &snd.mtype);
        getchar();
        if (0 == snd.mtype)
            break;

        printf("请输入要发送的信息>>>");
        scanf("%s", snd.mtext);
        getchar();

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

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

        printf("发送成功\n");
    }

    pthread_exit(NULL);
}

void *my_talk(void *arg)
{
    int msqid = *(int *)arg;

    struct magbuf snd;
    struct magbuf rcv;
    while (1)
    {
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 0, 0) < 0)
        {
            perror("msgrcv");
            break;
        }

        printf("%ld : %s\n", rcv.mtype, rcv.mtext);

        if (strcmp(rcv.mtext, "quit") == 0)
        {
            break;
        }
    }

    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("ket = %#x\n", key);

    // 创建或获取消息队列的id号
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("msgget success msqid=%d\n", msqid);
    // 定义一个结构体变量去调用结构体msgbuf
    struct magbuf snd;
    struct magbuf rcv;

    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, my_buf, &msqid) != 0)
    {
        printf("filed__%d__\n", __LINE__);
        return -1;
    }

    if (pthread_create(&tid2, NULL, my_talk, &msqid) != 0)
    {
        printf("filed__%d__\n", __LINE__);
        return -1;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    system("ipcs -q");
    return 0;
}

main2.c

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

struct magbuf
{
    long mtype; // 消息内容
    char mtext[128];
};

void *my_buf(void *arg)
{
    int msqid = *(int *)arg;

    struct magbuf snd;
    struct magbuf rcv;
    while (1)
    {
        printf("请输入消息类型>>>");
        scanf("%ld", &snd.mtype);
        getchar();
        if (0 == snd.mtype)
            break;

        printf("请输入要发送的信息>>>");
        scanf("%s", snd.mtext);
        getchar();

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

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

        printf("发送成功\n");
    }

    pthread_exit(NULL);
}

void *my_talk(void *arg)
{
    int msqid = *(int *)arg;

    struct magbuf snd;
    struct magbuf rcv;
    while (1)
    {
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 0, 0) < 0)
        {
            perror("msgrcv");
            break;
        }

        printf("%ld : %s\n", rcv.mtype, rcv.mtext);

        if (strcmp(rcv.mtext, "quit") == 0)
        {
            break;
        }
    }

    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("ket = %#x\n", key);

    // 创建或获取消息队列的id号
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
    printf("msgget success msqid=%d\n", msqid);
    // 定义一个结构体变量去调用结构体msgbuf
    struct magbuf snd;
    struct magbuf rcv;

    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, my_buf, &msqid) != 0)
    {
        printf("filed__%d__\n", __LINE__);
        return -1;
    }

    if (pthread_create(&tid2, NULL, my_talk, &msqid) != 0)
    {
        printf("filed__%d__\n", __LINE__);
        return -1;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    system("ipcs -q");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值