Deadlock example in C using MQX Lite

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Deadlock example in C using MQX Lite

1,015件の閲覧回数
vivian_dandan
Contributor I

Hi there,

I'm pretty new to multithread, but I need to write a test case for Deadlock condition. Saw an example using mutex, but with pthread.

Thread 1:

pthread_mutex_lock(&m1);

pthread_mutex_lock(&m2);

pthread_mutex_unlock(&m2);

pthread_mutex_unlock(&m1);

Thread 2:

pthread_mutex_lock(&m2);

pthread_mutex_lock(&m1);

pthread_mutex_unlock(&m1);

pthread_mutex_unlock(&m2);

How should I re-write it here?

Thanks a lot!

Vivian

0 件の賞賛
返信
1 返信

726件の閲覧回数
soledad
NXP Employee
NXP Employee

Hello Dandan,

Please check the below example, I hope this helps.


Have a great day,
Sol

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

/* ###################################################################

**     Filename    : mqx_tasks.c

**     Project     : ProcessorExpert

**     Processor   : MK20DX128VLH5

**     Component   : Events

**     Version     : Driver 01.00

**     Compiler    : GNU C Compiler

**     Date/Time   : 2013-02-11, 11:06, # CodeGen: 0

**     Abstract    :

**         This is user's event module.

**         Put your event handler code here.

**     Settings    :

**     Contents    :

**         Task1_task - void Task1_task(uint32_t task_init_data);

**         Task2_task - void Task2_task(uint32_t task_init_data);

**

** ###################################################################*/

/*!

** @file mqx_tasks.c

** @version 01.00

** @date 2013-02-11, 11:06, # CodeGen: 0

** @brief

**         This is user's event module.

**         Put your event handler code here.

*/

/*!

**  @addtogroup mqx_tasks_module mqx_tasks module documentation

**  @{

*/

/* MODULE mqx_tasks */

#include "Cpu.h"

#include "Events.h"

#include "mqx_tasks.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

#include "mutex.h"

MUTEX_STRUCT   print_mutex;

/*

** ===================================================================

**     Event       :  Task1_task (module mqx_tasks)

**

**     Component   :  Task1 [MQXLite_task]

**     Description :

**         MQX task routine. The routine is generated into mqx_tasks.c

**         file.

**     Parameters  :

**         NAME            - DESCRIPTION

**         task_init_data  -

**     Returns     : Nothing

** ===================================================================

*/

void main_task(uint32_t task_init_data)

{

    _task_id            task_id;

    MUTEX_ATTR_STRUCT   mutexattr;

    const char*         string1 = "Hello from Print task 1\n";

    const char*         string2 = "Hello from Print task 2\n";

    /* Initialize mutex attributes */

    if (_mutatr_init(&mutexattr) != MQX_OK) {

       printf("Initialize mutex attributes failed.\n");

       _task_block();

    }

    /* Initialize the mutex */

    if (_mutex_init(&print_mutex, &mutexattr) != MQX_OK) {

       printf("Initialize print mutex failed.\n");

       _task_block();

    }

    /* Create the print tasks */

    printf(" - Creating print1_task");

    task_id = _task_create_at(0, PRINT1_TASK, (uint_32)string1, print1_task_stack, PRINT1_TASK_STACK_SIZE);

    if (task_id == MQX_NULL_TASK_ID) {

        printf("...failed\n");

    }

    printf(" - Creating print2_task");

    task_id = _task_create_at(0, PRINT2_TASK, (uint_32)string2, print2_task_stack, PRINT2_TASK_STACK_SIZE);

    if (task_id == MQX_NULL_TASK_ID) {

        printf("...failed\n");

    }

    _task_block();

}

/*

** ===================================================================

**     Event       :  print1_task (module mqx_tasks)

**

**     Component   :  Task2 [MQXLite_task]

**     Description :

**         MQX task routine. This task prints a message. It uses a mutex to

**             ensure I/O is not interleaved.

**     Parameters  :

**         NAME            - DESCRIPTION

**         task_init_data  -

**     Returns     : Nothing

** ===================================================================

*/

void print1_task(uint32_t task_init_data)

{

    while(TRUE) {

       if (_mutex_lock(&print_mutex) != MQX_OK) {

          printf("Mutex lock failed.\n");

          _task_block();

       }

       puts((char *)task_init_data);

       _time_delay_ticks(10);

       _mutex_unlock(&print_mutex);

    }

}

/*

** ===================================================================

**     Event       :  print2_task (module mqx_tasks)

**

**     Component   :  Task3 [MQXLite_task]

**     Description :

**         MQX task routine. This task prints a message. It uses a mutex to

**             ensure I/O is not interleaved.

**     Parameters  :

**         NAME            - DESCRIPTION

**         task_init_data  -

**     Returns     : Nothing

** ===================================================================

*/

void print2_task(uint32_t task_init_data)

{

    while(TRUE) {

       if (_mutex_lock(&print_mutex) != MQX_OK) {

          printf("Mutex lock failed.\n");

          _task_block();

       }

       puts((char *)task_init_data);

       _time_delay_ticks(10);

       _mutex_unlock(&print_mutex);

    }

}

/* END mqx_tasks */

/*!

** @}

*/

/*

** ###################################################################

**

**     This file was created by Processor Expert 10.0.12 [05.05]

**     for the Freescale Kinetis series of microcontrollers.

**

** ###################################################################

*/

0 件の賞賛
返信