Chinaunix首页 | 论坛 | 博客
  • 博客访问: 363840
  • 博文数量: 53
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 1143
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 11:53
文章分类

全部博文(53)

文章存档

2011年(1)

2008年(52)

我的朋友

分类:

2008-09-22 19:18:26

/* includes */
#include "vxWorks.h"
#include "taskLib.h"
#include "stdio.h"
#include "ioLib.h"
#include "pipeDrv.h"

/*globals */
typedef struct
{
  VOIDFUNCPTR routine;   /* 函数指针 */
  int arg;
} MSG_REQUEST; /* message structure */

#define TASK_PRI          254             /* tServers task's priority */
#define TASK_STACK_SIZE  5000      /* tServer task's stack size */
#define PIPE_NAME       "/pipe/server"  /* name of the pipe device */
#define NUM_MSGS  10     /* max number of messages in the pipe */

LOCAL int pipeFd; /* File descriptor for the pipe device */
LOCAL void pipeServer(); /* server task */

/* serverStart:初始化pipeServer任务以执行管道中夹带的函数 */
STATUS serverStart()
{
    if (pipeDevCreate(PIPE_NAME, NUM_MSGS, sizeof(MSG_REQUEST)) == ERROR)
    {
        perror("Error in creating pipe"); /* print error if pipe is already
         * created, but do not return */
    }
   
    /* Open the pipe */
    if ((pipeFd = open(PIPE_NAME, UPDATE, 0)) == ERROR)
    {
        perror("Error in opening pipe device");
        return (ERROR);
    }
   
    /* Spawn the server task */
    if (taskSpawn("tServer", TASK_PRI, 0, TASK_STACK_SIZE, (FUNCPTR)pipeServer, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR)
    {
        perror("Error in spawning tServer task");
        close(pipeFd);
        return (ERROR);
    }
   
    return (OK);
}

/* serverSend :发送管道消息,将函数指针作为消息的一部分 */
STATUS serverSend(VOIDFUNCPTR routine, /* name of the routine to execute */ int arg /* argument of the routine */)
{
    MSG_REQUEST msgRequest;
    int status;
   
    /* Initialize the message structure */
    msgRequest.routine = routine;
    msgRequest.arg = arg;
   
    /* Send the message and return the results */
    status = write(pipeFd, (char*) &msgRequest, sizeof(MSG_REQUEST));
   
    return ((status == sizeof(MSG_REQUEST)) ? OK : ERROR);
}

/* pipeServer:读取管道消息并执行管道消息中夹带的函数 */
LOCAL void pipeServer()
{
    MSG_REQUEST msgRequest;
   
    while (read(pipeFd, (char*) &msgRequest, sizeof(MSG_REQUEST)) > 0)
        (*msgRequest.routine)(msgRequest.arg);
}

void PRINT(int arg)
{
    printf("%d",arg);
}


  程序中,pipeServer执行于非常低的优先级(254级),当我们在shell中输入“serverSend(VOIDFUNCPTR routine, int arg)” 时,pipeServer将读到管道中的消息,并执行“*routine (arg)”。
当我们在tShell中输入“serverSend(PRINT,2);”,在VxSim中将输出2。

  管道可以看作受驱动器pipeDrv管理的虚拟I/O设备,使用基本的I/O系统接口可以读、写和操作管道,这些函数包括read、write、open、close、ioctl和select等。与pipe密切相关的其它API还有:
(1)pipeDrv( ):初始化pipeDrv,函数原型:
STATUS pipeDrv (void);

(2)pipeDevCreate( ):创建pipe,函数原型:
STATUS pipeDevCreate
(
    char * name,              /* 创建的pipe名 */
    int    nMessages,         /* pipe中的最大消息数 */
    int    nBytes             /* 每个消息的大小 */
);

(3)pipeDevDelete:删除pipe,函数原型:
STATUS pipeDevDelete
(
    char * name,              /* 要删除的pipe名 */
    BOOL   force              /* 如果为真,则强制删除pipe */
);


 

阅读(4937) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~