Что такое хендскейк в контексте ОСРВ?

Я использую ОСРВ Micro C OS II, и я должен решить задание с помощью «рукопожатия», но я не знаю, что это такое. Я мог бы использовать семафоры, но не знаю, что такое рукопожатие:

Спецификация

две задачи, которые обмениваются данными друг с другом посредством процедуры установления связи. Обе задачи имеют два состояния 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;
}

person Niklas R.    schedule 13.10.2013    source источник


Ответы (1)


Термин «рукопожатие» здесь имеет значение в контексте назначения (и в более общем плане), а не конкретно RTOS.

Вопрос - это еще и ответ; в нем говорится, что задачи будут взаимодействовать посредством процедуры установления связи, а затем продолжается объяснение требуемой процедуры установления связи. То есть вам не нужно знать, что конкретно такое «рукопожатие», вам просто нужно реализовать описанную связь / синхронизацию потоков (которая является рукопожатием).

Термин «рукопожатие» имеет общее значение в смысле протокола двусторонней синхронизации, который это то, что описывается в задании. Обычно это применяется к протоколам связи, где рукопожатие происходит между двумя конечными точками, в этом контексте это относится к двусторонней синхронизации двух потоков.

Тем не менее, при всем сказанном я должен сказать, что описанию требуемого протокола установления связи не хватает точной ясности; если это остается неясным, вам, вероятно, следует попросить вашего репетитора уточнить.

person Clifford    schedule 13.10.2013