gpt4 book ai didi

c - 如何使用 Micro C OS II 同步两个任务?

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:55 26 4
gpt4 key购买 nike

我有这个规范:

task 0 sends integer numbers (starting from 1) to task 1. Task 1 shall multiply the numbers with -1 and send them back to task 0. Task 0 shall then print these numbers to the console. For the communication between task 0 and task 1 a single memory location sharedAddress shall be used, i.e. both task 0 and task 1 read and write to/from this location! Save the file as SharedMemory.c. The execution of the program shall give the following output. Sending : 1 Receiving : -1 Sending : 2 Receiving : -2 ...

我写了这个程序,但任务没有正确同步,我想我可能在信号量或上下文切换方面做错了什么。

我得到这个输出,而不是数字有时倒计时,因为任务没有正确同步:

Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17
Sending : 16
Receiving -16
Sending : -17
Receiving 17

我需要改的程序是

#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
int number = 1;
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");
}
}

/* Producer */
void task1(void* pdata)
{
INT8U err;

while (1)
{
char text1[] = "Sending : ";
char text2[] = "Receiving : ";

int i;
OSSemPend(aSemaphore, 0, &err); // Trying to access the key

for (i = 0; i < strlen(text1); i++)
putchar(text1[i]);
printf("%d", number);
putchar('\n');


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


OSSemPend(aSemaphore, 0, &err); // Trying to access the key

for (i = 0; i < strlen(text1); i++)
putchar(text2[i]);
printf("%d", number);
putchar('\n');
number=-number;
number++;
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


}
}

/* Consumer */
void task2(void* pdata)
{
INT8U err;
while (1)
{
OSSemPend(aSemaphore, 0, &err); // Trying to access the key
number = -number;
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 - Handshake\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;
}

你能帮帮我吗?

最佳答案

再想一想:您可能需要两个信号量:

  • 一个用于从生产者发送到消费者 (sem1)
  • 一个用于从消费者发送给生产者 (sem2)

你用 0 初始化 sem1,用 1 初始化 sem2。

  • 任务 1 首先在 sem2 上挂起,然后处理数据并在 sem1 上发布。下一个循环步骤将等到任务 2 发布 sem2。
  • 任务 2 在 sem 1 上挂起(因此等待第一个数据输入),处理数据并在 sem2 上发布

关于c - 如何使用 Micro C OS II 同步两个任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com