<< Chapter < Page Chapter >> Page >
if (QUE_empty(&queue)) { // Check to see if the queue is empty LOG_printf(&trace,"queue error\n"); // Print something if it is empty // If the queue is empty you probably will not want to proceed} msg = QUE_get(&queue); // If there is a message, dequeue it // use the message here// After using the message, free the memory // This tells MEM_free the location of the msg and size so it// can remove it from memory MEM_free(0, msg, sizeof(MsgObj)); // Free up the memory

Functions QUE_put and QUE_get are atomic in that they add and remove elements from the queue with interrupts turned off. Therefore there should not be a problem of more than one task trying to access the queue at the same time. The function QUE_get is also non-blocking so the tasks should determine if there are any elements on the queue before calling QUE_get .

A semaphore can be used to count the number of elements on a queue and be used to block a task that needs access to a queue. Figure 2 shows how one task TSK0 will write to a queue, QUE0 , and the other task TSK1 will read from the queue. Also, the semaphore QUE0_SEM is used to keep track of how many elements are on the queue. After TSK0 puts a message on the queue it will call SEM_post and increment the semaphore. Before reading from the queue, TSK1 will call SEM_pend on the semaphore and if the task does not block, there is an element on the queue. If there are no elements on the queue, the task will block on the semaphore.

Queue and semaphore setup

For the setup with one queue and one semaphore notice that if the program ran for a long time the code would have to continually allocate memory for a message and then de-allocate it when it was done using the message. This could take up a substantial amount of time and could cause fragmentation of the memory space. A better method is to have two queues where one queue holds messages that are free and one holds messages that contain data being transmitted from one task to another. Figure 3 shows the same setup in Figure 2 except there is now a queue that contains free messages or empty messages.

Free message queue setup

During the initialization phase of the program, memory for empty messages is allocated and the messages are put in the QUE_Free queue and the semaphore QUE_Free_SEM is incremented for each message put on the queue. When TSK0 needs to send a message to TSK1 it will check QUE_Free_SEM to see if there are any free messages. If not, it will block. If there are free messages it will take one off of QUE_Free after the QUE_Free_SEM is decremented and then fill the message with data and put it on the queue QUE0 . The semaphore QUE0_SEM is incremented after the message is put on QUE0 .

The task TSK1 will block on QUE0_SEM until task TSK1 puts a message on the queue. Then it will decrement the semaphore and use the message. When it is done it will put the message on QUE_Free and increment its semaphore.

Example

This is a very simple example to demonstrate the structure of a program that uses queues. In the example we will assume that DSP/BIOS has been set up with two tasks, TSK0 and TSK1, and one queue, QUE0. The two tasks have the same priority and TSK0 is set to execute first. The code for the example follows.

#include<std.h>// Target definition header #include<sys.h>// DSP/BIOS config/error header #include<log.h>// LOG module header #include<mem.h>// MEM module header #include<que.h>// QUE module header #include<tsk.h>// TSK module header#include "QUE_Examplecfg.h" // header generated by QUE_Example.tcf config filetypedef struct MsgObj { QUE_Elem elem; /* first field for QUE */Int val; /* message value */ } MsgObj, *Msg;Void main() {} // TSK0 will generate two messages and put them on the queueVoid funTSK0() {Msg msg; // Pointer to the message object // allocate memory for first messagemsg = MEM_alloc(0, sizeof(MsgObj), 0); if (msg == MEM_ILLEGAL) {// If the memory allocation fails, abort SYS_abort("Memory allocation failed!\n");} msg->val = 1; // put the message number in the message // print the message numberLOG_printf(&trace, "Writing message %d", msg->val); // Put the message on the queueQUE_put(&QUE0, msg); // repeat for the second message msg = MEM_alloc(0, sizeof(MsgObj), 0);if (msg == MEM_ILLEGAL) { // If the memory allocation fails, abortSYS_abort("Memory allocation failed!\n"); }msg->val = 2; // put the message number in the message // print the message numberLOG_printf(&trace, "Writing message %d", msg->val); // Put the message on the queueQUE_put(&QUE0, msg); }// TSK1 will get two messages from the queue Void funTSK1(){ Msg msg; // Pointer to the message object// If the queue is empty, we should not proceedif (QUE_empty(&QUE0)) { LOG_printf(&trace,"TSK1 queue error"); return; // This will make the task terminate} // Get the message off the queuemsg = QUE_get(&QUE0); // print value in the messageLOG_printf(&trace, "Reading message %d", msg->val); // Since we are done with the message, free the memoryMEM_free(0, msg, sizeof(MsgObj)); // Repeat for the second message // If the queue is empty, we should not proceedif (QUE_empty(&QUE0)) { LOG_printf(&trace,"TSK1 queue error"); return; // This will make the task terminate} // Get the message off the queuemsg = QUE_get(&QUE0); // print value in the messageLOG_printf(&trace, "Reading message %d", msg->val); // Since we are done with the message, free the memoryMEM_free(0, msg, sizeof(MsgObj)); }

It is important to free the memory of each message after it is used so that the memory does not get used up. The result of the run follows.

Writing message 1 Writing message 2Reading message 1 Reading message 2

Questions & Answers

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ti dsp/bios lab. OpenStax CNX. Sep 03, 2013 Download for free at http://cnx.org/content/col11265/1.8
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ti dsp/bios lab' conversation and receive update notifications?

Ask