⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validate.c

📁 如何将基于vxworks的应用程序移植到LINUX操作系统。
💻 C
📖 第 1 页 / 共 5 页
字号:
    {
        puts( "Task 1 sending token to semaphore SEM3." );
        errno = 0;
        err = semGive( sema43_id );
        if ( err != OK )
        {
            printf( "\nTask 1 send token to SEM3 returned error %x\r\n", errno );
        }
    }
    puts( "Task 1 blocking until Tasks 4, 7, and 10 consume SEM3 tokens." );
    errno = 0;
    err = semTake( complt4, WAIT_FOREVER );
    err = semTake( complt7, WAIT_FOREVER );
    err = semTake( complt10, WAIT_FOREVER );

    /************************************************************************
    **  Semaphore Deletion Test
    ************************************************************************/

    puts( "\n.......... Next Tasks 4, 7, and 10 look for tokens from SEM1" );
    puts( "           in priority order.  Task 1 will delete SEM1 before any" );
    puts( "           tokens become available.  Tasks 4, 7, and 10 should be" );
    puts( "           awakened and return errno 0x3d0001." );
    puts( "           SEM2 will be deleted with no tasks waiting." );
    puts( "           This tests the semDelete logic." );
    taskDelay( 2 );
    puts( "Task 1 deleting semaphore SEM1." );
    errno = 0;
    err = semDelete( sema41_id );
    if ( err != OK )
        printf( "\nTask 1 delete of SEM1 returned error %x\r\n", errno );
    else
        printf( "\r\n" );
    puts( "Task 1 deleting semaphore SEM2." );
    errno = 0;
    err = semDelete( sema42_id );
    if ( err != OK )
        printf( "\nTask 1 delete of SEM2 returned error %x\r\n", errno );
    else
        printf( "\r\n" );

    puts( "Task 1 blocking until Tasks 4, 7, and 10 complete semDelete test." );
    errno = 0;
    err = semTake( complt4, WAIT_FOREVER );
    err = semTake( complt7, WAIT_FOREVER );
    err = semTake( complt10, WAIT_FOREVER );

    /************************************************************************
    **  Semaphore-Not-Found Test
    ************************************************************************/

    puts( "\n.......... Finally, we verify the error codes returned when" );
    puts( "           a non-existent semaphore is specified." );

    errno = 0;
    err = semGive( sema41_id );
    printf( "\nsemGive for SEM1 returned error %x\r\n", errno );

    errno = 0;
    err = semTake( sema41_id, NO_WAIT );
    printf( "\nsemTake for SEM1 (no waiting) returned error %x\r\n", errno );

    errno = 0;
    err = semTake( sema41_id, WAIT_FOREVER );
    printf( "\nsemTake for SEM1 (wait forever) returned error %x\r\n", errno );

    errno = 0;
    err = semDelete( sema41_id );
    printf( "\nsemDelete for SEM1 returned error %x\r\n", errno );
}

/*****************************************************************************
**  validate_mutexes
**         This function sequences through a series of actions to exercise
**         the various features and characteristics of VxWorks mutexes
**
*****************************************************************************/
void validate_mutexes( void )
{
    STATUS err;
    int i;

    puts( "\r\n********** Mutex Semaphore validation:" );

    /************************************************************************
    **  Mutex Semaphore Creation Test
    ************************************************************************/
    puts( "\n.......... First we create three mutex semaphores:" );
 
    puts( "\nCreating Mutex 1, FIFO queuing" );
    errno = 0;
    mutex1_id = semMCreate( SEM_Q_FIFO );
    if ( errno != OK )
    {
        printf( "... returned error %x\r\n", errno );
    }
    puts( "\nTask 1 locking Mutex 1" );
    errno = 0;
    err = semTake( mutex1_id, WAIT_FOREVER );
    if ( err != OK )
    {
        printf( " returned error %x\r\n", errno );
    }
 
    puts( "\nCreating Mutex 2, FIFO queuing and Delete Safety" );
    errno = 0;
    mutex2_id = semMCreate( SEM_Q_FIFO | SEM_DELETE_SAFE );
    if ( errno != OK )
    {
        printf( "... returned error %x\r\n", errno );
    }
 
    puts( "\nCreating Mutex 3, PRIORITY queuing and Inversion Safety" );
    errno = 0;
    mutex3_id = semMCreate( SEM_Q_PRIORITY | SEM_INVERSION_SAFE );
    if ( errno != OK )
    {
        printf( "... returned error %x\r\n", errno );
    }

    /************************************************************************
    **  Mutex Semaphore Recursive semTake Test
    ************************************************************************/
    puts( "\n.......... Next we attempt to recursively lock Mutex 3." );
    puts( "           This should return no errors." );

    for ( i = 1; i < 4; i++ )
    {
        printf( "Task 1 recursively locking Mutex 3 - Locking pass %d", i );
        errno = 0;
        err = semTake( mutex3_id, WAIT_FOREVER );
        if ( err != OK )
            printf( " returned error %x\r\n", errno );
        else
            puts( "\r\n" );
    }

    /************************************************************************
    **  Mutex Semaphore Recursive semGive Test
    ************************************************************************/
    puts( "\n.......... Now we recursively unlock Mutex 3." );
    puts( "           This should return no errors on passes 1 through 3," );
    puts( "           but should return error 0x160068 on pass 4" );
    puts( "           since we only recursively locked the mutex 3 times." );

    for ( i = 1; i < 5; i++ )
    {
        printf( "Task 1 recursively unlocking Mutex 3 - Unlocking pass %d",
                i );
        errno = 0;
        err = semGive( mutex3_id );
        if ( err != OK )
            printf( " returned error %x\r\n", errno );
        else
            puts( "\r\n" );
    }

    /************************************************************************
    **  Mutex Semaphore semGive (Not Owner) Test 
    ************************************************************************/
    puts( "\n.......... Next we enable Task 2 to attempt to unlock Mutex 1," );
    puts( "           which Task 1 previously locked and still 'owns'." );
    puts( "           This should return an error 0x160068." );

    puts( "Task 1 signalling Task 2 to attempt to unlock Mutex 1.");
    errno = 0;
    err = semGive( enable2 );
    if ( err != OK )
    {
        printf( "semGive of enable2 returned error %x\r\n", errno );
    }

    puts( "Task 1 blocking for handshake from Task 2..." );
    errno = 0;
    err = semTake( complt2, WAIT_FOREVER );

    /************************************************************************
    **  Mutex Semaphore Flush Test
    ************************************************************************/

    puts( "\n.......... Next we attempt to flush Mutex 1.  Flushes are not" );
    puts( "           allowed for mutex semaphores, so this should fail with" );
    puts( "           an error 0x160068." );

    puts( "Task 1 attempting to flush Mutex 1." );
    errno = 0;
    err = semFlush( mutex1_id );
    if ( err != OK )
        printf( "\nTask 1 semFlush of Mutex 1 returned error %x\r\n", errno );
    else
        printf( "\r\n" );

    /************************************************************************
    **  Mutex Semaphore Priority Inversion Safety Test 
    ************************************************************************/
    puts( "\n.......... Next we test mutex priority inversion protection." );
    puts( "           First task 2 (priority 20) will lock Mutex 3.  Then");
    puts( "           task 1 (priority 5) will attempt to lock Mutex 3, and");
    puts( "           will block.  At this point task 2's priority should be");
    puts( "           boosted to equal that of task 1.  Task 2 will then");
    puts( "           unlock the mutex, at which point task 2 should drop");
    puts( "           back to its initial priority setting, and task 1 should");
    puts( "           acquire the mutex.");

    puts( "Task 1 enabling Task 2 to acquire ownership of Mutex 3.");
    errno = 0;
    err = semGive( enable2 );
    if ( err != OK )
    {
        printf( "semGive of enable2 returned error %x\r\n", errno );
    }

    puts( "Task 1 blocking for handshake from Task 2..." );
    errno = 0;
    err = semTake( complt2, WAIT_FOREVER );

    printf( "Task 1 attempting to lock Mutex 3" );
    errno = 0;
    err = semTake( mutex3_id, WAIT_FOREVER );
    if ( err != OK )
        printf( " returned error %x\r\n", errno );
    else
        puts( "\r\n" );

    puts( "Task 1 blocking until Task 2 completes priority inversion test." );
    errno = 0;
    err = semTake( complt2, WAIT_FOREVER );

    /************************************************************************
    **  Mutex Semaphore Task Deletion Safety Test 
    ************************************************************************/
    puts( "\n.......... Next we test mutex automatic deletion safety." );
    puts( "           First task 2 will lock Mutex 2, making task 2 safe");
    puts( "           from deletion.  Task 1 will then attempt to delete");
    puts( "           task 2, and should block.  Task 2 will then unlock the");
    puts( "           mutex, at which point task 2 should become deletable,");
    puts( "           and task 1 should complete the deletion of task 2.");

    puts( "Task 1 enabling Task 2 to acquire ownership of Mutex 2.");
    errno = 0;
    err = semGive( enable2 );
    if ( err != OK )
    {
        printf( "semGive of enable2 returned error %x\r\n", errno );
    }

    puts( "Task 1 blocking for handshake from Task 2..." );
    errno = 0;
    err = semTake( complt2, WAIT_FOREVER );

    printf( "Task 1 attempting to delete Task 2" );
    taskDelete( task2_id );

    puts( "\nTask 1 calling taskIdVerify to confirm Task 2 deleted..." );
    err = taskIdVerify( task2_id );
    if ( err != OK )
        puts( "taskIdVerify indicates Task 2 does not exist." );
    else
        puts( "taskIdVerify indicates Task 2 still exists." );

    /************************************************************************
    **  Mutex Semaphore Deletion Test 
    ************************************************************************/
    puts( "\n.......... Finally we test mutex deletion behavior for a" );
    puts( "           mutex owned by the task deleting it.  (This is the");
    puts( "           recommended technique for mutex deletion.)");
    puts( "           Then we test deletion of a mutex not owned by the task");
    puts( "           which is deleting it.  No errors should be returned.");

    puts( "Task 1 deleting Mutex 1." );
    errno = 0;
    err = semDelete( mutex1_id );
    if ( err != OK )
        printf( "\nTask 1 delete of Mutex 1 returned error %x\r\n", errno );
    else
        printf( "\r\n" );

    puts( "Task 1 deleting Mutex 3." );
    errno = 0;
    err = semDelete( mutex3_id );
    if ( err != OK )
        printf( "\nTask 1 delete of Mutex 3 returned error %x\r\n", errno );
    else
        printf( "\r\n" );

    puts( "Task 1 deleting Mutex 2." );
    errno = 0;
    err = semDelete( mutex2_id );
    if ( err != OK )
        printf( "\nTask 1 delete of Mutex 2 returned error %x\r\n", errno );
    else
        printf( "\r\n" );
}

/*****************************************************************************
**  validate_msg_queues
**         This function sequences through a series of actions to exercise
**         the various features and characteristics of VxWorks message
**         queues.
**
*****************************************************************************/
void validate_msg_queues( void )
{
    STATUS err;
    int message_num;
    int msg_count;
    msgblk_t msg;
    msgblk_t rcvd_msg;
    char msg_string[80];

    puts( "\r\n********** Message Queue validation:" );
    /************************************************************************
    **  Message Queue-full Test
    ************************************************************************/
    puts( "\n.......... First we created three message queues" );
    puts( "           Next we enable Tasks 3, 6, and 9 to consume" );
    puts( "           messages from MSQ1 in reverse-priority order." );
    puts( "           The enables are sent to lowest-priority tasks first." );

    puts( "Task 1 enabling Tasks 3, 6, and 9 to consume MSQ1 messages.");
    errno = 0;
    err = semGive( enable3 );
    if ( err != OK )
    {
        printf( "semGive of enable3 returned error %x\r\n", errno );
    }
    errno = 0;
    err = semGive( enable6 );
    if ( err != OK )
    {
        printf( "semGive of enable6 returned error %x\r\n", errno );
    }
    errno = 0;
    err = semGive( enable9 );
    if ( err != OK )
    {
        printf( "semGive of enable9 returned error %x\r\n", errno );
    }

    /*
    **  Delay to allow consumer tasks to recognize enable signals.
    */
    taskDelay( 2 );

    puts( "\n.......... Next we attempt to send nine messages to each queue" );
    puts( "           This tests message queue full logic." );
    puts( "           The message queue MSQ1 should return no errors" );
    puts( "           but MSQ2 should return five 0x3d0002 errs" );
    puts( "           and MSQ3 should return nine 0x3d0002 errs" );

    /*
    **  This is a 'sneaky trick' to null-terminate the object name string.
    */
    msg.msg.nullterm = (ulong)NULL; 

    /*
    **  Define nine unique messages to send to each message queue.
    */

    msg.msg.qname[0] = 'M';
    msg.msg.qname[1] = 'S';
    msg.msg.qname[2] = 'Q';
    for ( message_num = 1; message_num < 10; message_num++ )
    { 
        /*
        **  Post a unique message to each of the message queues

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -