Ucoⅱ semaphore and semaphore set instance

Source: Internet
Author: User
Tags semaphore

The Code comes from the book CD, "Principles and Applications of the embedded operating system ucoⅱ"

Instance 1 semaphore

# Include "des. H"

# Define task_stk_size 512 // Task Stack Length

OS _stk starttaskstk [task_stk_size]; // defines the task stack area.
OS _stk mytaskstk [task_stk_size];
OS _stk youtaskstk [task_stk_size];
OS _stk hertaskstk [task_stk_size];

Int16s key; // The key used to exit ucos_ii
Char * S1 = "mytask ";
Char * S2 = "youtask ";

Int8u err;
Int8u y = 0;
// Character display position

OS _event * fun_semp; // defines the semaphore.

Void starttask (void * data );
Void mytask (void * data );
Void youtask (void * data );
Void fun (int8u X, int8u y );
Void quitkey (void );

/************************ Main function ************* ********************************/
Void main (void)
{
Osinit (); // initialize ucos_ii
Pc_dossavereturn (); // Save the DOS environment
Pc_vectset (UCOS, osctxsw); // ucos_ii installation interrupted

Fun_semp = ossemcreate (1); // create a semaphore
Ostaskcreate (starttask, 0, & starttaskstk [task_stk_size-1], 0 );
Osstart ();
}

// ***************************** Starttask ******** ************************************
Void starttask (void * pdata)
{
Pdata = pdata;
OS _enter_critical ();
Pc_vectset (0x08, ostickisr );
Pc_settickrate (OS _ticks_per_sec );
OS _exit_critical ();
// Osstatinit ();

Ostaskcreate (mytask, 0, & mytaskstk [task_stk_size-1], 1 );
Ostaskcreate (youtask, 0, & youtaskstk [task_stk_size-1], 2 );
Ostasksuspend (OS _prio_self );

}
// ***************************** Mytask ******** ************************************
Void mytask (void * pdata)
{
Pdata = pdata;

For (;;)
{
// Ossempend (fun_semp, 0, & ERR); // request semaphore
Pc_dispstr (0, ++ y, S1, disp_bgnd_black + disp_fgnd_white );
Fun (9, y );
// Ossempost (fun_semp); // sends a semaphore
Quitkey ();
Ostimedlyhmsm (0, 0, 1, 0 );
}
}

// ***************************** Youtask ******** ************************************
Void youtask (void * pdata)
{
Pdata = pdata;

For (;;)
{
// Ossempend (fun_semp, 0, & ERR );
Pc_dispstr (0, ++ y, S2, disp_bgnd_black + disp_fgnd_white );
Fun (10, y );
// Ossempost (fun_semp );
Quitkey ();
Ostimedlyhmsm (0, 0, 2, 0 );
}
}

// ****************************** Fun () **************************************** ****
Void fun (int8u X, int8u y)
{
Pc_dispstr (X, Y, "call fun ()", disp_bgnd_black + disp_fgnd_yellow );
}

// ****************************** Quitkey () **************************************** ****
Void quitkey (void)
{
If (pc_getkey (& Key) = true)
{
If (Key = 0x1b)
{Pc_dosreturn ();}
}
}






Instance 2 semaphores

# Include "des. H"

# Define task_stk_size 512 // Task Stack Length

OS _stk starttaskstk [task_stk_size]; // defines the task stack area.
OS _stk mytaskstk [task_stk_size];
OS _stk youtaskstk [task_stk_size];
OS _stk hertaskstk [task_stk_size];

Int16s key; // The key used to exit ucos_ii
Char * S1 = "mytask is running ";
Char * S2 = "youtask is running ";
Char * S3 = "hertask is running ";
Char * Ss = "mytask pend a SEM ";

Int8u err;
Int8u x = 0, y = 0; // character position
Int32u times = 0;

OS _event * SEMP;

Void starttask (void * data );
Void mytask (void * data );
Void youtask (void * data );
Void hertask (void * data );

Void quitkey (void );

/************************ Main function ************* ********************************/
Void main (void)
{
Osinit (); // initialize ucos_ii
Pc_dossavereturn (); // Save the DOS environment
Pc_vectset (UCOS, osctxsw); // ucos_ii installation interrupted

SEMP = ossemcreate (1 );
Ostaskcreate (starttask, 0, & starttaskstk [task_stk_size-1], 5 );
Osstart ();
}

// ***************************** Starttask ******** ************************************
Void starttask (void * pdata)
{
Pdata = pdata;
OS _enter_critical ();
Pc_vectset (0x08, ostickisr );
Pc_settickrate (OS _ticks_per_sec );
OS _exit_critical ();
Osstatinit ();

Ostaskcreate (mytask, 0, & mytaskstk [task_stk_size-1], 6 );
Ostaskcreate (youtask, 0, & youtaskstk [task_stk_size-1], 7 );
Ostaskcreate (hertask, 0, & hertaskstk [task_stk_size-1], 8 );
Ostasksuspend (OS _prio_self );
}
// ***************************** Mytask ******** ************************************
Void mytask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Ostimedlyhmsm (0, 0, 1,200 );
{
Pc_dispstr (10, ++ y, SS, disp_bgnd_black + disp_fgnd_yellow );
Ossempend (SEMP, 0, & ERR );
Pc_dispstr (10, ++ y, S1, disp_bgnd_black + disp_fgnd_yellow );
Ossempost (SEMP );
Quitkey ();
}
Ostimedlyhmsm (0, 0, 0,200 );
}
}

// ***************************** Youtask ******** ************************************
Void youtask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Pc_dispstr (7, ++ y, S2, disp_bgnd_black + disp_fgnd_white );
Quitkey ();
Ostimedlyhmsm (0, 0, 0,300 );
}
}

// ****************************** Hertask ******** ************************************

Void hertask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Ossempend (SEMP, 0, & ERR );
Pc_dispstr (10, ++ y, S3, disp_bgnd_black + disp_fgnd_white );
For (times; Times <30000000; times ++)
{OS _sched ();}
Ossempost (SEMP );
Quitkey ();
Ostimedlyhmsm (0, 0, 1, 0 );
}
}

// ****************************** Quitkey () **************************************** ****
Void quitkey (void)
{
If (pc_getkey (& Key) = true)
{
If (Key = 0x1b)
{Pc_dosreturn ();}
}
}

Instance three semaphores

The first task is executed only after the last two tasks are executed.

# Include "des. H"

# Define task_stk_size 512 // Task Stack Length

OS _stk starttaskstk [task_stk_size]; // defines the task stack area.
OS _stk mytaskstk [task_stk_size];
OS _stk youtaskstk [task_stk_size];
OS _stk hertaskstk [task_stk_size];

Int16s key; // The key used to exit ucos_ii

Char * S1 = "mytask is running ";
Char * S2 = "youtask is running ";
Char * S3 = "hertask is running ";

Int8u err;
Int8u x = 0, y = 0;
// Character display position

OS _flag_grp * wflag;/* event flag pointer */

Void starttask (void * data );
Void mytask (void * data );
Void youtask (void * data );
Void hertask (void * data );

Void quitkey (void );

/************************ Main function ************* ********************************/
Void main (void)
{
Osinit (); // initialize ucos_ii
Pc_dossavereturn (); // Save the DOS environment
Pc_vectset (UCOS, osctxsw); // ucos_ii installation interrupted

Wflag = osflagcreate (0, & ERR );
/* Create an event flag Group */
If (ERR = OS _no_err)
/* Check whether the creation is successful */
Pc_dispstr (0, 23, "no error", disp_bgnd_black + disp_fgnd_white );
Ostaskcreate (starttask, 0, & starttaskstk [task_stk_size-1], 5 );

Osstart ();
}

// ***************************** Starttask ******** ************************************
Void starttask (void * pdata)
{
Pdata = pdata;

OS _enter_critical ();
Pc_vectset (0x08, ostickisr );
Pc_settickrate (OS _ticks_per_sec );
OS _exit_critical ();
// Osstatinit ();

Ostaskcreate (mytask, 0, & mytaskstk [task_stk_size-1], 6 );
Ostaskcreate (youtask, 0, & youtaskstk [task_stk_size-1], 7 );
Ostaskcreate (hertask, 0, & hertaskstk [task_stk_size-1], 8 );

Ostasksuspend (OS _prio_self );

}
// ***************************** Mytask ******** ************************************
Void mytask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Osflagpend (wflag, (OS _flags) 3, OS _flag_wait_set_all, 0, & ERR );
Pc_dispstr (10, ++ y, S1, disp_bgnd_black + disp_fgnd_white );
Quitkey ();
Ostimedlyhmsm (0, 0, 2, 0 );
}
}

// ***************************** Youtask ******** ************************************
Void youtask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Pc_dispstr (10, ++ y, S2, disp_bgnd_black + disp_fgnd_white );
Ostimedlyhmsm (0, 0, 8, 0 );
Osflagpost (wflag, (OS _flags) 2, OS _flag_set, & ERR );
Quitkey ();
Ostimedlyhmsm (0, 0, 2, 0 );
}
}

// ****************************** Hertask ******** ************************************
Void hertask (void * pdata)
{
Pdata = pdata;

For (;;)
{
Pc_dispstr (10, ++ y, S3, disp_bgnd_black + disp_fgnd_white );
Osflagpost (wflag, (OS _flags) 1, OS _flag_set, & ERR );
Quitkey ();
Ostimedlyhmsm (0, 0, 1, 0 );
}
}
// ****************************** Quitkey () **************************************** ****
Void quitkey (void)
{
If (pc_getkey (& Key) = true)
{
If (Key = 0x1b)
{Pc_dosreturn ();}
}
}

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.