Sei sulla pagina 1di 6

EX NO: DATE: IMPLEMENTION OF MUTUAL EXCLUSION USING LPC2378 AIM: To create a program for sharing resources using mutual

exclusion functions in LPC 2378 in IAR Embedded workbench 6.30. ALGORITHM: Initialize the constants (stack size, priority) for each task. Create all tasks in Task start function. Each task is defined and set the semaphore, OSMutexCreate(). In task function, if any task needs to perform an event, it gets mutex and released mutex only after performing the event. Use mutex functions to share the resource in each task. Each event has an ID TaskData[], during their start of execution. To get and release the mutex, semaphore is made as OSMutexPend() and OSMutexPost() respectively.

MUTUAL EXCLUSION: A mutual exclusion (mutex) is a program object that prevents simultaneous access to a shared resource. This concept is used in concurrent programming with a critical section, a piece of code in which processes or threads access a shared resource. Only one thread owns the mutex at a time, thus a mutex with a unique name is created when a program starts. When a thread holds a resource, it has to lock the mutex from other threads to prevent concurrent access of the resource. Upon releasing the resource, the thread unlocks the mutex.Mutex comes into the
picture when two threads work on the same data at the same time. It acts as a lock and is the most basic synchronization tool. When a thread tries to acquire a mutex, it gains the mutex if it is available, otherwise the thread is set to sleep condition. Mutual exclusion reduces latency and busy-waits using queuing and context switches. Mutex can be enforced at both the hardware and software levels.

PROGRAMS: #include <includes.h> #define TASK_STK_SIZE 512 #define N_TASKS 3 /* Size of each task's stacks (# of WORDs) */

/* Number of tasks */ /* Tasks stacks */

OS_STK TskStk[N_TASKS][TASK_STK_SIZE]; OS_STK TaskStartStk[TASK_STK_SIZE]; INT8U TaskData[N_TASKS]; OS_EVENT *mutex; void Task(void *pdata); void TaskStart(void *pdata);

/* Parameters to pass to each task */ /* 1 mutual exclusive semaphore*/ /* Function prototypes of tasks */ /* Function prototypes of Startup task */

static void TaskStartCreateTasks(void); void main (void) { INT8U err; BSP_IntDisAll(); FIO3DIR=0x008000FF; BSP_Init(); OSInit(); mutex = OSMutexCreate(8,&err); /* Initialize uC/OS-II */ /* set a mutual semaphore , 8 is the PIP */ /* for setting the mutex */

OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 9); OSStart(); } /* Start multitasking */

void TaskStart (void *pdata) { #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif pdata = pdata; OSTmr_Init(); OSStatInit(); TaskStartCreateTasks(); // Create all the application tasks // Suspend the TaskStart /* Prevent compiler warning */ /* Allocate storage for CPU status register */

OSTaskSuspend(OS_PRIO_SELF); OSTimeDlyHMSM(0, 0, 10, 0); } static void TaskStartCreateTasks (void) { INT8U i; for (i = 0; i <N_TASKS; i++) { TaskData[i] = i;

/* Create N_TASKS identical tasks

*/

/* Each task will pass its own id */

OSTaskCreate(Task, (void *)&TaskData[i], &TaskStk[i][TASK_STK_SIZE - 1], 12-i); } }

void Task (void *pdata) { INT8U err; INT8U id; id=*(int *)pdata; for (;;) { printf("\n\r Task %3d try to get the mutex.", id); OSMutexPend(mutex, 0, &err); /* Acquire mutex to get continue */

printf("\n\r Task %3d is getting the mutex.", id); OSTimeDlyHMSM(0, 0, 0, 200); OSMutexPost(mutex); /* Wait 200 minisecond /* Release mutex */ */

printf("\n\r Task %3d releases the mutex.", id); OSTimeDlyHMSM(0, 0, 0, (3-id)*150); } } /* Wait (3-id)*150 minisecond */

SNAPSHOT:

Fig: Snapshot for Mutual exclusion Function

RESULT: Thus the program for sharing resources is created using mutual exclusion funtion and the result is viewed in WinXTalk tool.

Potrebbero piacerti anche