前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Task之暂缓执行

Task之暂缓执行

作者头像
Taishan3721
发布2021-09-15 12:33:09
2760
发布2021-09-15 12:33:09
举报

分享一段代码,pipe的一个使用案例

/* pipeServer.c - reads requests over pipe */

#include <stdio.h>
#include <ioLib.h>
#include <pipeDrv.h>
#include <taskLib.h>

typedef struct
    {
    VOIDFUNCPTR pRoutine;
    int         arg;
    }MSG_REQUEST;

#define TASK_PRI    254                     /* server priority */
#define TASK_STACK  20000                   /* server stack space */
#define PIPE_NAME   "/pipe/server"
#define NUM_MSGS    10

static int pipeFd;

/**************************************************
 * Server task which reads from a pipe
 * and executes the function passed in the MSG_REQUEST data structure.
 */
LOCAL void pipeServer()
{
    MSG_REQUEST msgRequest;

    while(read(pipeFd,(char *)&msgRequest,sizeof(MSG_REQUEST))>0)
        {
        if(NULL != msgRequest.pRoutine)
            {
            (*msgRequest.pRoutine)(msgRequest.arg);
            }
        }
    }

/**************************************************
 * Initializes a server task to execute functons at a low priority.
 * Uses pipes as the communication mechanism.
 *
 * RETURNS: OK or ERROR on failure.
 */
STATUS serverStart()
{
    /* Create the pipe device */
    if(pipeDevCreate(PIPE_NAME, NUM_MSGS, sizeof(MSG_REQUEST)) == ERROR)
        return (ERROR);

    /* Open the pipe */
    if((pipeFd = open(PIPE_NAME,UPDATE,0)) == ERROR)
        return (ERROR);

    /* Spawn the server task */
    if(taskSpawn("tServer", TASK_PRI, 0, TASK_STACK,
                (FUNCPTR)pipeServer,0,0,0,0,0,0,0,0,0,0) == ERROR)
        {
        close(pipeFd);
        return (ERROR);
        }
    printf("pipe server started\n");
    return (OK);
    }

/**************************************************
* Sends a request to the server to
* execute a function at the server's priority.
*
* RETURNS: OK or ERROR on failure.
*/
STATUS serverSend(VOIDFUNCPTR pRoutine, int arg)
{
    MSG_REQUEST msgRequest;
    int         status = ERROR;

    if(ERROR != pipeFd)
        {
        /* Initialize the message structure */
        msgRequest.pRoutine = pRoutine;
        msgRequest.arg      = arg;

        /* Send the message and return the results */
        status = write(pipeFd, (char *)&msgRequest, sizeof(MSG_REQUEST));
        if(status == sizeof(MSG_REQUEST))
            {
            status = OK;
            }
        }
    return (status);
    }

这段代码的作用是什么?看标题!

贴个图吧

有异议的请留言

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 这里只有VxWorks 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档