首页 > 其他分享 >【Qnx 】Qnx IPC通信PPS

【Qnx 】Qnx IPC通信PPS

时间:2024-05-26 16:29:02浏览次数:20  
标签:IPC int Qnx bytes PPS fd include buf

Qnx IPC通信PPS

Qnx自带PPS服务,PPS全称Persistent Publish/Subscribe Service,就是常见的P/S通信模式。
Qnx PPS的通信模式是异步的,Publisher和Subscriber也无需关心对方是否存在。

利用Qnx提供的PPS服务,Publisher可以通知多个Subscriber进行某种动作。
该图是QNX官网的PPS示例场景。

  • Applications和组件(比如Media、Radio、Phone等等)通过PPS交互消息。
    在这里插入图片描述
    官网链接:
    http://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.pps.developer/topic/about.html

使用QNX PPS

Qnx 的PPS,是利用文件的方式实现的。所以使用起来,跟文件的读写差不多。

  • Publisher端打开文件,写入文件。
int fd = open( "/pps/example/button", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO );

ssize_t len, bytes_written;
len = snprintf(buf, 256, "state::%s\npub1::%d", state ? "on" : "off", count);
bytes_written = write( fd, buf, len );
  • Subscriber打开文件,监听文件描述符产生的写入事件,监听可以使用epoll、select、ionotify(qnx提供)等等。
  • 打开文件时“?”后面表示打开模式。delta表示增量模式,所谓增量模式,每次只读取PPS数据中变更的部分。
int fd = open( "/pps/example/button?delta", O_RDONLY );
struct pollfd readfds[1];
 

 while (1) {
    readfds[0].fd = fd;
    readfds[0].events = POLLIN;
    
    int result = poll(readfds, 1, timeout);
    if(result != -1 && result != 0) {
        if( readfds[0].revents & POLLRDNORM ){
               // 也可以选择其他读取方式
               // 利用fd的读文件方式有很多种
               num_bytes = read( fd, buf, sizeof(buf) );
               if (num_bytes > 0) {
                   // 解析数据
               }
        }
    } 
 }
  • Publisher打开(不存在的时候创建)PPS服务路径下(默认为/pps,可配置)的文件,往文件里写入数据。Subscriber打开要监听的文件(与Publisher一个文件),监听该文件的写入事件,当写入时读取该文件,即可。

完整的例子(QNX官网提供)

  • Publisher
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char *argv[])
{
   int fd;
   int state = 0;
   char buf[256];
   struct stat stat_buf;
   int count = 0;
   ssize_t len, bytes_written;

   /* Is PPS running? */
   if (stat( "/pps", &stat_buf) != 0)
   {
      if (errno == ENOENT)
         printf ("The PPS server isn't running.\n");
      else
         perror ("stat (/pps)");
      return EXIT_FAILURE;
   }

   /* Create the "button" object (if it doesn't already exist). */
   fd = open( "/pps/example/button", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO );
   if ( fd < 0 )
   {
      perror ("Couldn't open /pps/example/button");
      return EXIT_FAILURE;
   }

   /* Loop forever, toggling the state of the button. */
   while ( 1 )
   {
      usleep (500);
      count++;
      len = snprintf(buf, 256, "state::%s\npub1::%d", state ? "on" : "off", count);
      bytes_written = write( fd, buf, len );
      if (bytes_written == -1)
      {
         perror ("write()");
      }
      else if (bytes_written != len)
      {
         printf ("Bytes written: %d String length: %d\n", bytes_written, len);
      }

      if ( state == 0 )
         state = 1;
      else
         state = 0;
   }

   return EXIT_SUCCESS;
}
  • Subscriber
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/poll.h>

int main(int argc, char *argv[])
{
   int fd;
   char buf[256];
   ssize_t num_bytes;
   int ret;
   struct pollfd readfds[1];
   int timeout = 2000;

   fd = open( "/pps/example/button?delta", O_RDONLY );
   if ( fd < 0 )
   {
      perror ("Couldn't open /pps/example/button");
      return EXIT_FAILURE;
   }

   /* Loop, echoing the attributes of the button. */
   while (1)
   {
      readfds[0].fd = fd;
      readfds[0].events = POLLIN;

      switch ( ret = poll(readfds, 1, timeout ) )
      {
         case -1:
            /* An error occurred. */
            break;
         case  0:
            /* poll() timed out. */
            break;
         default:
            if( readfds[0].revents & POLLRDNORM )
            {
               num_bytes = read( fd, buf, sizeof(buf) );
               if (num_bytes > 0)
               {
                  write (STDOUT_FILENO, buf, (size_t) num_bytes);
               }
            }
      }
   }

   return EXIT_SUCCESS;
}

标签:IPC,int,Qnx,bytes,PPS,fd,include,buf
From: https://blog.csdn.net/zxc024000/article/details/139216423

相关文章

  • NIUSHOP 开源商城 V6 开源版(商城+分销+VIPCard+上门服务)前端技术探索与实践
    摘要:本文深入探讨了NIUSHOPV6开源商城前端技术的选型、实现与设计理念。NIUSHOPV6作为一款优秀的企业级商城系统,其前端采用了Vite、TypeScript、Vue3及ElementPlus等最新技术栈,为开发者提供了高效、灵活的开发体验。本文将从技术选型、设计理念、插件化开发以及一键云编译......
  • 苹果应用上传AppStore
    将ipa提交到AppStore需要Mac电脑操作,现在大部分的程序员都是使用混合开发平台windows系统的电脑,自己装虚拟机过程又繁琐。使用此工具只需要网页上点两下帮你完成这些鸡毛蒜皮事,让你有更多的时间花在改bug上。1.打开苹果应用商店管理后台获取密钥,地址https://appstoreconnect.a......
  • 互斥锁,IPC机制,队列,生产者消费者模型
    Ⅰ互斥锁【一】什么是互斥锁互斥锁其实就是一种锁。为当前进程或线程添加额外的限制限制当前时间段只能由当前进程使用,当前进程使用完成后才能其他进程继续使用其作用是保证在同一时刻只有一个线程在访问共享资源,从而避免多个线程同时读写数据造成的问题。互斥锁的基本原......
  • mit6.828笔记 - lab4 Part C:抢占式多任务和进程间通信(IPC)
    PartC:抢占式多任务和进程间通信(IPClab4到目前为止,我们能够启动多个CPU,让多个CPU同时处理多个进程。实现了中断处理,并且实现了用户级页面故障机制以及写时复制fork。但是,我们的进程调度不是抢占式的,现在每个进程只有在发生中断的时候,才会被调度(调用shed_yeild),这样就有可能会有......
  • Centos 无法使用yum 错误:Failed to download metadata for repo ‘AppStream’: Canno
    大家都知道Centos8于2021年年底停止了服务,大家再在使用yum源安装时候,出现下面错误“错误:Failedtodownloadmetadataforrepo‘AppStream’:Cannotprepareinternalmirrorlist:NoURLsinmirrorlist”1、进入yum的repos目录 复制cd/etc/yum.repos.d/ ......
  • C# app.config配置appSettings标签
    AppSettings标签主要用于应用程中的一些配置信息。比如上传文件的路径,需要用到的可能会根据不同环境改变的一些常量等。可以在配置文件添加可执行文件要的配置。usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;classProgram{staticvoid......
  • 2024年AppScan 最新10.5.0破解版 附详细安装教程
     免责声明请勿利用文章内的相关技术从事非法测试。由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,作者不为此承担任何责任,请务必遵守网络安全法律法规。本文仅用于测试,请完成测试后24小时删除,请勿用于商业用途。如文中内容涉及侵权行......
  • openGauss 开启RemoveIPC引起的core问题
    开启RemoveIPC引起的core问题问题现象操作系统配置中RemoveIPC参数设置为yes,数据库运行过程中出现宕机,并显示如下日志消息。FATAL:semctl(1463124609,3,SETVAL,0)failed:Invalidargument原因分析当RemoveIPC参数设置为yes时,操作系统会在对应用户退出时删除IPC资源(共......
  • iceoryx源码阅读(八)——IPC通信机制
    目录1 整体结构2 序列化与反序列化3 类Unix系统的实现3.1 发送函数send3.2 接收函数receive4 Windows系统的实现4.1 发送函数send4.2 接收函数receive5 Roudi的监听逻辑1 整体结构通过前面的介绍,订阅者、发布者与Roudi守护进程之间也需要通信,如上文介绍的,请求Roudi守护进村创建......
  • AppSpider Pro 7.5.009 for Windows - Web 应用程序安全测试
    AppSpiderPro7.5.009forWindows-Web应用程序安全测试Rapid7DynamicApplicationSecurityTesting(DAST)请访问原文链接:https://sysin.org/blog/appspider/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgappspider没有任何应用程序未经测试,没有未知风险......