进程间通信 消息队列

1_1 代码:

#include <head.h>

int main(int argc, const char *argv[])
{
    // 创建一个有名管道文件1
    if(mkfifo("./myfifo_1",0775)<0)
    {
        if(errno != 17)   // 17 是文件存在导致的错误,是允许存在的错误
        {
            perror("mkfifo");
            return -1; 
        }
    }
    printf("mkfifo1 申请成功 \n");

    // 打开管道文件
    int fd = open("./myfifo_1",O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("打开文件1成功\n");

// 创建一个有名管道文件2
    if(mkfifo("./myfifo_2",0775)<0)
    {
        if(errno != 17)   // 17 是文件存在导致的错误,是允许存在的错误
        {
            perror("mkfifo");
            return -1; 
        }
    }
    printf("mkfifo2 申请成功 \n");

    // 打开管道文件
    int tp = open("./myfifo_2",O_WRONLY);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("打开文件2成功\n");


    // 从终端获取数据,发送到管道中
    char buf[32];
    ssize_t res;
    while(1)
    {
        // 读取进程1的内容
        bzero(buf,sizeof(buf));
        res = read(fd,buf,sizeof(buf));
        if(res < 0)
        {
            perror("read");
            return -1;
        }
        else if(0 == res)
        {
            printf("写段关闭,且管道内没有数据\n");
            printf("对端退出\n");
            break;
        }
        printf("进程1说%s\n",buf);

        if(strcmp(buf,"quit") == 0)
        {
            break;
        }

        // 进程2从终端获取数据
        printf("进程2输入:\n");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1] = '\0';

        if(write(tp,buf,sizeof(buf)) < 0)
        {
            perror("write");
            return -1;
        }
        printf("进程2写入成功\n");
        if(strcmp(buf,"quit") == 0)
        {
            break;
        }

    }

    close(fd);
    close(tp);

    
    return 0;
}
 


 

1_2 代码:

#include <head.h>

int main(int argc, const char *argv[])
{
    // 创建一个有名管道文件1
    if(mkfifo("./myfifo_1",0775)<0)
    {
        if(errno != 17)   // 17 是文件存在导致的错误,是允许存在的错误
        {
            perror("mkfifo");
            return -1; 
        }
    }
    printf("mkfifo1 申请成功 \n");

    // 打开管道文件
    int fd = open("./myfifo_1",O_WRONLY);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("打开文件1成功\n");

// 创建一个有名管道文件2
    if(mkfifo("./myfifo_2",0775)<0)
    {
        if(errno != 17)   // 17 是文件存在导致的错误,是允许存在的错误
        {
            perror("mkfifo");
            return -1; 
        }
    }
    printf("mkfifo2 申请成功 \n");

    // 打开管道文件
    int tp = open("./myfifo_2",O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("打开文件2成功\n");

    // 从终端获取数据,发送到管道中
    char buf[32];
    ssize_t res;
    while(1)
    {
        // 进程1从终端获取数据
        printf("进程1输入:\n");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1] = '\0';

        if(write(fd,buf,sizeof(buf)) < 0)
        {
            perror("write");
            return -1;
        }
        printf("进程1写入成功\n");
        if(strcmp(buf,"quit") == 0)
        {
            break;
        }

        // 读取进程2的内容
        bzero(buf,sizeof(buf));
        res = read(tp,buf,sizeof(buf));
        if(res < 0)
        {
            perror("read");
            return -1;
        }
        else if(0 == res)
        {
            printf("写段关闭,且管道内没有数据\n");
            printf("对端退出\n");
            break;
        }
        printf("进程2说%s\n",buf);

        if(strcmp(buf,"quit") == 0)
        {
            break;
        }
    }

    close(fd);
    close(tp);

    return 0;
}
 

2_1 代码:

#include <head.h>

struct msgbuf
{
    long mtype;
    char mtext[128];
};
 
int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 2);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
 
    printf("key = %#x\n", key);
 
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
 
    printf("msqid = %d\n", msqid);
 
    struct msgbuf snd;
    struct msgbuf rcv;
 
    while (1)
    {
        // A进程发送消息给B进程
        printf("A进程请输入消息内容(输入quit结束)\n");
        fgets(snd.mtext, sizeof(snd.mtext), stdin);
        snd.mtext[strlen(snd.mtext) - 1] = '\0';
 
        if (strcmp(snd.mtext, "quit") == 0)
            break;
 
        snd.mtype = 1; // 消息类型设为1表示A发送的消息
 
        if (msgsnd(msqid, (void *)&snd, sizeof(snd.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }
        printf("A进程发送消息成功\n");
 
        // 接收来自B进程的回复消息
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 2, 0) < 0)
        {
            perror("msgrcv");
            return -1;
        }
        printf("B进程回复消息:%s\n", rcv.mtext);
    }
 
    if (msgctl(msqid, IPC_RMID, NULL) < 0)
    {
        perror("msgctl");
        return -1;
    }
    printf("删除消息队列成功\n");
 
    return 0;
}

2_2 代码:

#include <head.h>
 
struct msgbuf
{
    long mtype;
    char mtext[128];
};
 
int main(int argc, const char *argv[])
{
    key_t key = ftok("/home/ubuntu/", 2);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
 
    printf("key = %#x\n", key);
 
    int msqid = msgget(key, IPC_CREAT | 0777);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
 
    printf("msqid = %d\n", msqid);
 
    struct msgbuf snd;
    struct msgbuf rcv;
 
    while (1)
    {
        // 接收来自A进程的消息
        if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 1, 0) < 0)
        {
            perror("msgrcv");
            return -1;
        }
        printf("A进程发送的消息:%s\n", rcv.mtext);
 
        if (strcmp(rcv.mtext, "quit") == 0)
            break;
 
        // B进程回复消息给A进程
        printf("B进程请输入回复消息内容(输入quit结束)\n");
        fgets(snd.mtext, sizeof(snd.mtext), stdin);
        snd.mtext[strlen(snd.mtext) - 1] = '\0';
 
        if (strcmp(snd.mtext, "quit") == 0)
            break;
 
        snd.mtype = 2; // 消息类型设为2表示B发送的消息
 
        if (msgsnd(msqid, (void *)&snd, sizeof(snd.mtext), 0) < 0)
        {
            perror("msgsnd");
            return -1;
        }
        printf("B进程发送回复消息成功\n");
    }
 
    if (msgctl(msqid, IPC_RMID, NULL) < 0)
    {
        perror("msgctl");
        return -1;
    }
    printf("删除消息队列成功\n");
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值