什么是RTOS环境中的“握手”?

时间:2013-10-13 04:36:44

标签: c operating-system multitasking rtos handshake

我使用的是Micro C OS II RTOS,我应该用“握手”解决一项任务,但我不知道那是什么。我可以使用信号量,但我不知道握手是什么:

规范

  

通过握手相互通信的两个任务   程序。两个任务都有两个状态0和1.在每个状态下执行任务   应打印一条消息,以指示活动任务的状态,例如   “任务0 - 状态0”,如果任务0处于状态0,则程序应该   显示以下执行模式任务0 - 状态0任务1 - 状态0   任务1 - 状态1任务0 - 状态1任务0 - 状态0任务1 - 状态0   ......独立于任务期间。使用信号量来解决问题   问题

该计划

#include <stdio.h>
#include "includes.h"
#include <string.h>

#define DEBUG 1

/* Definition of Task Stacks */
/* Stack grows from HIGH to LOW memory */
#define   TASK_STACKSIZE       2048
OS_STK    task1_stk[TASK_STACKSIZE];
OS_STK    task2_stk[TASK_STACKSIZE];
OS_STK    stat_stk[TASK_STACKSIZE];
OS_EVENT *aSemaphore;
/* Definition of Task Priorities */
#define TASK1_PRIORITY      6  // highest priority
#define TASK2_PRIORITY      7
#define TASK_STAT_PRIORITY 12  // lowest priority 

void printStackSize(INT8U prio)
{
    INT8U err;
    OS_STK_DATA stk_data;

    err = OSTaskStkChk(prio, &stk_data);
    if (err == OS_NO_ERR) 
    {
        if (DEBUG == 1)
           printf("Task Priority %d - Used: %d; Free: %d\n", 
                   prio, stk_data.OSFree, stk_data.OSUsed);
    }
    else
    {
        if (DEBUG == 1)
           printf("Stack Check Error!\n");    
    }
}

/* Prints a message and sleeps for given time interval */
void task1(void* pdata)
{
  INT8U err;   
  while (1)
  { 
    char text1[] = "Hello from Task1\n";
    int i;
    OSSemPend(aSemaphore, 0, &err); // Trying to access the key

    for (i = 0; i < strlen(text1); i++)
        putchar(text1[i]);
    OSSemPost(aSemaphore); // Releasing the key
    OSTimeDlyHMSM(0, 0, 0, 11); // Context Switch to next task

                               // Task will go to the ready state

                               // after the specified delay
  }
}

/* Prints a message and sleeps for given time interval */
void task2(void* pdata)
{
  INT8U err;  
  while (1)
  { 
    char text2[] = "Hello from Task2\n";
    int i;
    OSSemPend(aSemaphore, 0, &err); // Trying to access the key
    for (i = 0; i < strlen(text2); i++)
        putchar(text2[i]);
    OSSemPost(aSemaphore); // Releasing the key
    OSTimeDlyHMSM(0, 0, 0, 4);
  }
}

/* Printing Statistics */
void statisticTask(void* pdata)
{
    while(1)
    {
        printStackSize(TASK1_PRIORITY);
        printStackSize(TASK2_PRIORITY);
        printStackSize(TASK_STAT_PRIORITY);
    }
}

/* The main function creates two task and starts multi-tasking */
int main(void)
{
  printf("Lab 3 - Two Tasks\n");
  aSemaphore = OSSemCreate(1); // binary semaphore (1 key)
  OSTaskCreateExt
    (task1,                        // Pointer to task code
     NULL,                         // Pointer to argument that is
                                   // passed to task
     &task1_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
     TASK1_PRIORITY,               // Desired Task priority
     TASK1_PRIORITY,               // Task ID
     &task1_stk[0],                // Pointer to bottom of task stack
     TASK_STACKSIZE,               // Stacksize
     NULL,                         // Pointer to user supplied memory
                                   // (not needed here)
     OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
     OS_TASK_OPT_STK_CLR           // Stack Cleared                                 
    );

  OSTaskCreateExt
    (task2,                        // Pointer to task code
     NULL,                         // Pointer to argument that is
                                   // passed to task
     &task2_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
     TASK2_PRIORITY,               // Desired Task priority
     TASK2_PRIORITY,               // Task ID
     &task2_stk[0],                // Pointer to bottom of task stack
     TASK_STACKSIZE,               // Stacksize
     NULL,                         // Pointer to user supplied memory
                                   // (not needed here)
     OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
     OS_TASK_OPT_STK_CLR           // Stack Cleared                       
    );  

  if (DEBUG == 1)
  {
    OSTaskCreateExt
      (statisticTask,                // Pointer to task code
       NULL,                         // Pointer to argument that is
                                     // passed to task
       &stat_stk[TASK_STACKSIZE-1],  // Pointer to top of task stack
       TASK_STAT_PRIORITY,           // Desired Task priority
       TASK_STAT_PRIORITY,           // Task ID
       &stat_stk[0],                 // Pointer to bottom of task stack
       TASK_STACKSIZE,               // Stacksize
       NULL,                         // Pointer to user supplied memory
                                     // (not needed here)
       OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
       OS_TASK_OPT_STK_CLR           // Stack Cleared                              
      );
  }  

  OSStart();
  return 0;
}

我的解决方案

此程序输出正确但程序是否正确?

输出

Task 0 - State 0
Task 1 - State 0
Task 1 - State 1
Task 0 - State 1
Task 0 - State 0
Task 1 - State 0
Task 1 - State 1

...

Handshake.c

#include <stdio.h>
#include "includes.h"
#include <string.h>

#define DEBUG 0

/* Definition of Task Stacks */
/* Stack grows from HIGH to LOW memory */
#define   TASK_STACKSIZE       2048
OS_STK    task1_stk[TASK_STACKSIZE];
OS_STK    task2_stk[TASK_STACKSIZE];
OS_STK    stat_stk[TASK_STACKSIZE];
OS_EVENT *aSemaphore;
/* Definition of Task Priorities */
#define TASK1_PRIORITY      6  // highest priority
#define TASK2_PRIORITY      7
#define TASK_STAT_PRIORITY 12  // lowest priority 

void handle_button_interrupts(void* context, alt_u32 id) 
{ 
 volatile int* edge_capture_ptr = (volatile int*) context; 

 OSIntEnter(); 
 // Read the edge capture register on the button PIO 
 //*edge_capture_ptr = 
 //IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE); 

 OSIntExit(); 
} 


void printStackSize(INT8U prio)
{
    INT8U err;
    OS_STK_DATA stk_data;

    err = OSTaskStkChk(prio, &stk_data);
    if (err == OS_NO_ERR) 
    {
        if (DEBUG == 1)
           printf("Task Priority %d - Used: %d; Free: %d\n", 
                   prio, stk_data.OSFree, stk_data.OSUsed);
    }
    else
    {
        if (DEBUG == 1)
           printf("Stack Check Error!\n");    
    }
}

/* Prints a message and sleeps for given time interval */
void task1(void* pdata)
{
  INT8U err;
  char state = '1';
  while (1)
  { 
    char text1[] = "Task 0 - State ";
    int i;
    OSSemPend(aSemaphore, 0, &err); // Trying to access the key

    for (i = 0; i < strlen(text1); i++)
        putchar(text1[i]);
    putchar(state); 
    putchar('\n');   
    if (state=='0')
        state='1';
    else
       state='0';

    for (i = 0; i < strlen(text1); i++)
        putchar(text1[i]);
    putchar(state); 
    putchar('\n');   

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

    OSSemPost(aSemaphore); // Releasing the key
    OSTimeDlyHMSM(0, 0, 0, 11); // Context Switch to next task

                               // Task will go to the ready state

                               // after the specified delay
  }
}

/* Prints a message and sleeps for given time interval */
void task2(void* pdata)
{
  INT8U err;  
  char state = '0';
  while (1)
  { 
    char text2[] = "Task 1 - State ";
    int i;
    OSSemPend(aSemaphore, 0, &err); // Trying to access the key
    for (i = 0; i < strlen(text2); i++)
        putchar(text2[i]);

    putchar(state); 
    putchar('\n');   
    if (state=='0')
        state='1';
    else
       state='0';

    for (i = 0; i < strlen(text2); i++)
        putchar(text2[i]);

    putchar(state); 
    putchar('\n');       

        if (state=='0')
        state='1';
    else
       state='0';
    OSSemPost(aSemaphore); // Releasing the key
    OSTimeDlyHMSM(0, 0, 0, 4);
  }
}

/* Printing Statistics */
void statisticTask(void* pdata)
{
    while(1)
    {
        printStackSize(TASK1_PRIORITY);
        printStackSize(TASK2_PRIORITY);
        printStackSize(TASK_STAT_PRIORITY);
    }
}

/* The main function creates two task and starts multi-tasking */
int main(void)
{
  printf("Lab 3 - Two Tasks\n");
  aSemaphore = OSSemCreate(1); // binary semaphore (1 key)
  OSTaskCreateExt
    (task1,                        // Pointer to task code
     NULL,                         // Pointer to argument that is
                                   // passed to task
     &task1_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
     TASK1_PRIORITY,               // Desired Task priority
     TASK1_PRIORITY,               // Task ID
     &task1_stk[0],                // Pointer to bottom of task stack
     TASK_STACKSIZE,               // Stacksize
     NULL,                         // Pointer to user supplied memory
                                   // (not needed here)
     OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
     OS_TASK_OPT_STK_CLR           // Stack Cleared                                 
    );

  OSTaskCreateExt
    (task2,                        // Pointer to task code
     NULL,                         // Pointer to argument that is
                                   // passed to task
     &task2_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
     TASK2_PRIORITY,               // Desired Task priority
     TASK2_PRIORITY,               // Task ID
     &task2_stk[0],                // Pointer to bottom of task stack
     TASK_STACKSIZE,               // Stacksize
     NULL,                         // Pointer to user supplied memory
                                   // (not needed here)
     OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
     OS_TASK_OPT_STK_CLR           // Stack Cleared                       
    );  

  if (DEBUG == 1)
  {
    OSTaskCreateExt
      (statisticTask,                // Pointer to task code
       NULL,                         // Pointer to argument that is
                                     // passed to task
       &stat_stk[TASK_STACKSIZE-1],  // Pointer to top of task stack
       TASK_STAT_PRIORITY,           // Desired Task priority
       TASK_STAT_PRIORITY,           // Task ID
       &stat_stk[0],                 // Pointer to bottom of task stack
       TASK_STACKSIZE,               // Stacksize
       NULL,                         // Pointer to user supplied memory
                                     // (not needed here)
       OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
       OS_TASK_OPT_STK_CLR           // Stack Cleared                              
      );
  }  

  OSStart();
  return 0;
}

1 个答案:

答案 0 :(得分:1)

这里的术语“握手”在赋值(更一般地说)的上下文中具有含义,而不是具体的RTOS。

问题也是答案;它声明任务将通过握手程序进行通信,然后继续解释所需的握手程序。也就是说你不需要知道具体的“握手”是什么,你只需要实现所描述的线程通信/同步( 握手)。

术语"handshake"在双向同步协议意义上具有一般意义,这是分配所要描述的内容。它通常应用于两个端点之间发生握手的通信协议,在此上下文中它指的是两个线程的双向同步。

然而,所有这些都说,我必须说所需握手协议的描述缺乏准确的清晰度;如果仍不清楚,你应该让你的导师澄清一下。