• Linux系统之《消息队列》入手应用


     

    目录

     

    简述

    代码

    编译

    运行


    简述

    消息队列是Linux进程间通信方式之一,消息队列一般是用于简单的通信,数据量不大,通信不频繁的情况。如果交互频繁或者数据量大就不适合了。

    代码

    下面直接上代码,发送端代码:

    //file name: msg_test.c
    
    #include "msg_test.h"
    
    //创建消息队列
    int Creat_msg(char *path, int id)
    {
    	key_t key;
    	int msg_id;
    	puts(path);
    	if ((key = ftok(path, id)) == -1)
    	{
    		perror("ftok");
    		exit(-1);
    	}
    	else 
    	{
    		printf("key = %d
    ", key);
    	}
    	
    	if ((msg_id = msgget(key, 0666 | IPC_CREAT)) == -1)
    	{
    		perror("msgget");
    		exit(-1);
    	}
    	else 
    	{
    		printf("msg_id = %d
    ", msg_id);
    	}	
    	return msg_id;
    }
    struct msg 
    {
    	long type;
    	char buf[256+1];
    };
    int 
    main(int argc, char **argv)
    {
    	int msgid = 0;
    	int ret = 0;
    	struct msg_struct msg_buf;
    	struct msg buf1;
    	bzero(&msg_buf, sizeof(msg_buf));
    	
    	if(argc != 3)
    	{
    		printf("Usag: ./%s path id
    ", argv[0]);
    		return 0;
    	}
    	
    	printf("path : %s, id = %x
    ", argv[1], atoi(argv[2]));
    	msgid = Creat_msg(argv[1], atoi(argv[2]));
    	printf("msgid = %d
    ", msgid);
    	
    	buf1.type = 0x32;
    #if 1
    	while(1)
    	{
    		ret = msgsnd(msgid, (void *)&buf1, 256, 0); //最后一个参数IPC_NOWAIT可以设置非阻塞
    		//perror("msgrecv");
    		printf("ret = %d, errno = %d
    ", ret, errno);
    		if(ret  < 0 )
    		{
    			printf("get ENOMSG
    ");
    			return 0;
    		}
    		usleep(2000);
    	}
    #endif	
    	if(msgctl(msgid, IPC_RMID, NULL)< 0)
    	{
    		perror("");
    	}
    	else 
    	printf("rm OK!");
    	return 0;
    }
    
    

    接收端代码:

    //file name: msg_test.c
    
    #include "msg_test.h"
    
    //创建消息队列
    int Creat_msg(char *path, int id)
    {
    	key_t key;
    	int msg_id;
    	puts(path);
    	if ((key = ftok(path, id)) == -1)
    	{
    		perror("ftok");
    		exit(-1);
    	}
    	else 
    	{
    		printf("key = %d
    ", key);
    	}
    	
    	if ((msg_id = msgget(key, 0666 | IPC_CREAT)) == -1)
    	{
    		perror("msgget");
    		exit(-1);
    	}
    	else 
    	{
    		printf("msg_id = %d
    ", msg_id);
    	}	
    	return msg_id;
    }
    struct msg 
    {
    	long type;
    	char buf[256+1];
    };
    int 
    main(int argc, char **argv)
    {
    	int msgid = 0;
    	int ret = 0;
    	struct msg_struct msg_buf;
    	struct msg buf1;
    	bzero(&msg_buf, sizeof(msg_buf));
    	
    	if(argc != 3)
    	{
    		printf("Usag: ./%s path id
    ", argv[0]);
    		return 0;
    	}
    	printf("path : %s, id = %x
    ", argv[1], atoi(argv[2]));
    	msgid = Creat_msg(argv[1], atoi(argv[2]));
    	printf("msgid = %d
    ", msgid);
    	
    	
    #if 1
    	while(1)
    	{
    		ret = msgrcv(msgid, (void *)&buf1, 256, 0x32, 0); //最后一个参数使用IPC_NOWAIT可以设置非阻塞
    		//perror("msgrecv");
    		printf("ret = %d, errno = %d
    ", ret, errno);
    		if(ret  < 0 )
    		{
    			printf("get ENOMSG
    ");
    			return 0;
    		}		
    	}
    #endif	
    	if(msgctl(msgid, IPC_RMID, NULL)< 0)
    	{
    		perror("");
    	}
    	else 
    	printf("rm OK!");
    	return 0;
    }
    
    

    编译

    gcc msg_send.c  -o msg_send
    gcc msg_test.c  -o msg_recv

    运行

    接收端:
    ./msg_recv  /tmp/ 1
    path : /tmp/, id = 1
    /tmp/
    key = 16842896
    msg_id = 16252928
    msgid = 16252928
    ret = 256, errno = 0   //接收到数据
    ret = 256, errno = 0
    
    发送端:
    ./msg_send  /tmp/ 1
    path : /tmp/, id = 1
    /tmp/
    key = 16842896
    msg_id = 16252928
    msgid = 16252928
    ret = 0, errno = 0     //发送成功
    ret = 0, errno = 0
    
  • 相关阅读:
    机器学习数学笔记|Taylor展开式与拟牛顿
    机器学习数学笔记|微积分梯度jensen不等式
    [DeeplearningAI笔记]第三章2.9-2.10端到端学习
    [DeeplearningAI笔记]第三章2.7-2.8多任务学习/迁移学习
    [DeeplearningAI笔记]第三章2.4-2.6不匹配的训练和开发/测试数据
    [DeeplearningAI笔记]第三章2.1-2.3误差分析
    [DeeplearningAI笔记]第三章1.8-1.12可避免误差与模型改善
    [DeeplearningAI笔记]第三章1.4-1.7开发集测试集划分与满足与优化指标
    [DeeplearningAI笔记]第三章1.1-1.3查准率/查全率/F1分数
    [DeeplearningAI笔记]第二章3.8-3.9分类与softmax
  • 原文地址:https://www.cnblogs.com/fensnote/p/13436453.html
Copyright © 2020-2023  润新知