UC/OS-II Study Notes: semaphore

Source: Internet
Author: User

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //

More original "uC/OS-II Study Notes: Series" Basic and embedded related knowledge details, please visit the cool tiger blog:

Http://blog.csdn.net/dcx1205

I believe it will not disappoint you !!
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //

I. semaphore concept:
For example, when a student enters the classroom, he/she must apply for a pass from the Administrator to allow the student to enter the classroom. When leaving the classroom, he/she must return the pass to the Administrator.
There are two cases:

Case 1: the Administrator has only one pass, so only one student can enter the classroom at a time. Other students are allowed to enter the classroom only when the passing pass is returned.
Case 2: the Administrator has n (n> 1) passes in his/her hands. Then, each time n students are allowed to enter the classroom, starting from the number (n + 1, the rest can only be waited outside the classroom door. The rest of the students may not enter the classroom until they return the pass.

(1) The semaphore here is the number of passes.
(2) students can be considered as "tasks ".
(3) the process of applying for a pass from a student to an administrator can be considered as a "request semaphore ".
(4) The process in which students return the pass to the administrator can be considered as a "Release semaphore ".
(5) When the number of passes in the Administrator's hands is greater than 0, the request semaphore can be successful.
(6) If the semaphores are successfully requested once, the semaphores are reduced by 1.
(7) When the semaphores are released once, 1 is added to the semaphores.

2. Operations on semaphores:
(1) create a semaphore
OS _event * ossemcreate (int16u CNT); // CNT is the initial value set by the semaphore counter
(2) request semaphores
Void ossempend (OS _event * pevent, int16u timeout, int8u * ERR); // The parameters are the pointer of the semaphore, waiting time limit, and error message respectively.
(3) Sending semaphores
Int8u ossempost (OS _event * pevent); // semaphore pointer
(4) Delete semaphores
OS _event * ossemdel (OS _event * pevent, int8u opt, int8u * ERR); // The parameters are the pointer of the semaphore, the deletion condition options, and the error message.
(5) query the semaphore status
Int8u ossemquery (OS _event * pevent, OS _sem_data * pdata); // The parameters are the pointer of the semaphore, respectively, and the structure of the semaphore state is stored.

3. Example of semaphore application:
(1) creation, request, sending, semaphore
// The status of led0 must be allowed by led1 before turning it over
# Include "SYS. H"
# Include "usart. H"
# Include "delay. H"
# Include "led. H"
# Include "key. H"
# Include "des. H"
 
// Set the task stack size
# Define led0_stk_size 64
# Define led1_stk_size 64
# Define start_stk_size 128

// Task Stack
OS _stk task_led0_stk [led0_stk_size];
OS _stk task_led1_stk [led1_stk_size];
OS _stk task_start_stk [start_stk_size];

// Declare semaphores
OS _event * extends maphore;
Int8u err;

// Set the task priority
# Define led0_task_prio 1
# Define led1_task_prio 2
# Define start_task_prio 0

// Task Declaration
Void taskstart (void * pdata );
Void taskled0 (void * pdata );
Void taskled1 (void * pdata );
Void systick_configuration (void); // system clock configuration function

Int main (void)
{
Pai_clock_init (9); // system clock settings
Delay_init (72); // delay Initialization
Uart_init (9600); // the serial port is initialized.
Led_init (); // initialize the hardware interface connected to the LED
Systick_configuration (); // system clock Configuration
Osinit (); // initialize uC/OS-II

// 1) create a semaphore
Required maphore = ossemcreate (0 );

// Create the start task
Ostaskcreate (
Taskstart, // task pointer
(Void *) 0, // parameters passed to the task
& Task_start_stk [START_STK_SIZE-1], // Task Stack top pointer
Start_task_prio // task priority
);

// Start multi-task scheduling
Osstart (); // after this step, the task is managed and scheduled by the operating system.

Return 0;
}

// Start task
Void taskstart (void * pdata)
{
Pdata = pdata; // prevents some compilers from reporting errors.
// Guanzhong disconnection enters the critical section
OS _enter_critical (); // This function is an assembly code, in the OS _cpu_a.asm file.

// Create Task 1
Ostaskcreate (taskled0, (void *) 0, & task_led0_stk [LED0_STK_SIZE-1], led0_task_prio );
// Create Task 2
Ostaskcreate (taskled1, (void *) 0, & task_led1_stk [LED1_STK_SIZE-1], led1_task_prio );

Ostasksuspend (OS _prio_self); // suspended but not deleted
// Enable the critical disconnection Period
OS _exit_critical ();
}

// Task 1
// Controls the power-off of ds0.
Void taskled0 (void * pdata)
{
While (1)
{
// 2) request semaphores
Ossempend (optional maphore, 0, & ERR );

Led0 =! Led0;

// Delay function parameters are in sequence: time, minute, second, and millisecond
Ostimedlyhmsm (0, 0, 1, 0); // delay 1 second

}
}

// Task 2
// Controls the off-light mode of ds1.
Void taskled1 (void * pdata)
{
While (1)
{

Led1 =! Led1;

// 3) Sending semaphores
Ossempost (psemaphore );

Ostimedlyhmsm (0, 0, 3, 0); // delay of 3 seconds
}
}
Symptom: When the request and sending of semaphores are not considered, the latency of Task 1 and Task 2 shows that the flash frequency of led0 is higher than that of led1;
When the semaphores are sent and requested, we can see that led0 and led1 flash simultaneously. Why?
Resolution: Because in code 1), we can see that the initial value of the created semaphore is 0, so Task 1 always needs to send the semaphore after Task 2 sends the semaphore to request the semaphore to succeed,
In order to make led0 flash; and led1 sends a semaphore every time it is flipped, so led1 also flipped once every time it is flipped, so they will flash synchronously.
According to the above inference, if the initial value of the semaphore created in code 1 is greater than 0, is the flash frequency of led0 higher than that of led1? The answer is yes!
If the initial semaphore value is greater than 0, Task 1 can successfully request a semaphore freely, the running of Task 1 does not need to be limited by Task 2.

(2) Delete semaphores
If the task does not need a semaphore, you can call ossemdel (OS _event * pevent, int8u opt, int8u * ERR); to delete the semaphore.
Note: The opt parameter can have two values: OS _del_always and OS _del_no_pend. The former indicates that the semaphore should be deleted immediately no matter whether there is a waiting task in the waiting task table,
The latter indicates that the semaphore is deleted only when the waiting task table has not waited for the task.

// Example: Use OS _del_always to delete the semaphore in Task 1
// Task 1
// Controls the power-off of ds0.
Void taskled0 (void * pdata)
{
While (1)
{
Ossempend (psemaphore, 0, & ERR); // request semaphore
Ossemdel (export maphore, OS _del_always, & ERR); // Delete the semaphore
Led0 =! Led0;

// Delay function parameters are in sequence: time, minute, second, and millisecond
Ostimedlyhmsm (0, 0, 1, 0 );
}
}
Symptom: the flash frequency of led0 is higher than that of led1.
Resolution: Because Task 1 has deleted the semaphore, it is no longer limited by Task 2 and can run freely.
It should be noted that semaphores can only be deleted in tasks but not in interrupted service programs.

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.