前言

本文主要介绍如何编写一个 qnx 下 的 device resource managers (设备驱动)
软件环境:qnx7.1


一、resource managers 是什么

resource managers (资源管理器)是具有某些定义明确特征的程序。该程序在不同的操作系统上被称为不同的事物,有人称它们为“设备驱动程序”,“ I / O管理器”,“文件系统”,“驱动程序”,“设备”,等等。但是,在所有情况下,该程序(我们将其简称为资源管理器)的目标都是提供某些服务的抽象视图。
在qnx 系统上 resource managers 分为两大类:Device resource managersFilesystem resource managers,本文主要介绍 Device resource managers(设备资源管理器),它其实就是一个设备驱动,为应用提供 /dev/sample 等设备节点。

下图是 resource managers 的整体框图:
qnx resource managers 实例-LMLPHP

二、device resource managers 实例

1. Single-threaded device resource manager

单线程设备资源管理器代码如下(生成/dev/sample 设备节点):

#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>

static resmgr_connect_funcs_t    connect_funcs;
static resmgr_io_funcs_t         io_funcs;
static iofunc_attr_t             attr;

int main(int argc, char **argv)
{
   
    /* declare variables we'll be using */
    resmgr_attr_t        resmgr_attr;
    dispatch_t           *dpp;
    dispatch_context_t   *ctp;
    int                  id;

    /* initialize dispatch interface */
    dpp = dispatch_create_channel(-1, DISPATCH_FLAG_NOLOCK );
    if (dpp == NULL) {
   
        fprintf(stderr,
                "%s: Unable to allocate dispatch handle.\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    /* initialize resource manager attributes */
    memset(&resmgr_a
11-01 04:07