Preliminary Research on VxWorks multi-task Programming-bottom

Source: Internet
Author: User
When a task needs to enter the resource, first obtain a semaphore (take that semaphore), as long as a task is using this semaphore, other tasks to enter the resource need to be stopped (blocked from execution). When this task is used for the resource, it will release the semaphore and allow another task to use the resource.
Semtake (semmutex, wait_forever );

.. Critical region, only accessible by a single task at a time

Semgive (semmutex );
Synchronization)
Sembcreat (sem_q_fifo, sem_empty), sem_empty indicates that it is used for synchronization between tasks.

  #include "vxWorks.h"   #include "semLib.h"   SEM_ID syncSem;   init ( int someIntNum )   {   intConnect (INUM_TO_IVEC (someIntNum), eventInterruptSvcRout, 0);   syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);   taskSpawn ("sample", 100, 0, 20000, task1, 0,0,0,0,0,0,0,0,0,0);   }   task1 (void)   { ...   semTake (syncSem, WAIT_FOREVER);   printf ("task 1 got the semaphore\n");   ...   }   eventInterruptSvcRout (void)   { ...   semGive (syncSem);   ...   } 

Semtake (Semid, time out) -------- if semaphore is idle, take. If not, set by time out. If it times out, it is executed downward.

Mutex semaphores are a special binary semaphores designed for priority inheritance, secure deletion, and regression. Its usage is similar to that of binary semaphores. But there are the following differences:
1. It is used only for mutual exclusion.
2. The task can only be released by the task. (It can be given only by the task that took it .)
3. ISR cannot release it.
4. The semflush () function cannot be used ().

Priority inversion)
Priority inversion means that a task is blocked by waiting for a task with a lower priority to release resources. If there is a medium priority ready task, the blocking will further deteriorate. Priority Inheritance technology can be used to solve the priority inversion problem.

Priority Inheritance)
Priority Inheritance can be used to solve the priority inversion problem. When a priority inversion occurs, a task with a lower priority is temporarily raised to its priority, so that the task can be executed as soon as possible and the resources required for the task with a higher priority are released.
Counting semaphores)
The Count semaphores are another Implementation Method for task synchronization and mutex. The Count semaphores are the same except for the number of times the semaphores are released. Each time the semaphores are released (gaven), the Count increases; each time the semaphores are used (taken), the Count decreases; when the count decreases to 0, the task to obtain the semaphore is blocked (BLOCKED ). If a semaphore is released and a task is blocked for waiting, the task is unblocked. If a semaphore is released and no task is blocked for waiting, the Count increases. This indicates that a count semaphore that has been released twice can be used twice without blocking. The following is an example of how to count semaphores:

      Semaphore Call   Count after Call   Resulting Behavior   semCCreate( )   3   Semaphore initialized with initial count of 3.   semTake( )   2   Semaphore taken.   semTake( )   1   Semaphore taken.   semTake( )   0   Semaphore taken.   semTake( )   0   Task blocks waiting for semaphore to be available.   semGive( )   0   Task waiting is given semaphore.   semGive( )   1   No task waiting for semaphore; count incremented. 

Message Queue)
The real-time application of message queue is composed of a series of tasks that work independently and collaboratively. Semaphores provide an efficient mechanism for synchronization and locking between tasks. In VxWorks, the message queue is used for communication between a CPU task (primary. A Message Queue allows a certain number of messages of different lengths to be arranged. Any task or interrupt service program (ISR) can send messages to message queues. Any task can receive messages from the message queue. Multi-task can send and receive messages from the consent message queue. Full-duplex communication between two tasks requires two message queues in different directions.
Introduction to message queue Functions
Msgqcreate () creates a bin to initialize a message queue.
Msgqdelete () terminates and releases a message queue.
Msgqsend () sends a message to the message queue.
Msgqreceive () receives a message from the Message Queue
A message queue is created by the msgqcreate (max_msgs, max_msg_len, msg_q_priority) function. Its Parameter max_msgs specifies the maximum number of messages allowed in the message queue and the maximum number of bytes allowed for each message max_msg_len.
A task or interrupt service program (ISR) sends a message to the Message Queue using the msgqsend () function. If no task is waiting for a message in the message queue, the message is added to the Message cache queue. If some tasks are waiting for messages in the message queue, the messages are immediately sent to the first waiting message task.
A task uses the msgqreceive () function to obtain a message from the message queue. If a message exists in the message queue cache, the first message is immediately listed and returned to the call center (caller ). if no message exists, the calling task stops (blocks) and is added to the task queue waiting for the message. The waiting task queue is arranged according to the priority or first-in-first-out (FIFO) rule, which is specified when the message queue is created.
Time out)
Both msgqsend () and msgqreceive () have time limit parameters. When a message is sent, if there is no space in the message queue cache, this parameter specifies the waiting time (the number of ticks) until the queue cache has space to receive messages. When receiving a message, if there is no message in the message queue, this parameter specifies the waiting time (the number of ticks) until the message queue has a message.

  #include "vxWorks.h" #include "msgQLib.h"   #define MAX_MSGS (10) #define MAX_MSG_LEN (100)   MSG_Q_ID myMsgQId;   task2 (void){char msgBuf[MAX_MSG_LEN];    if (msgQReceive(myMsgQId, msgBuf, MAX_MSG_LEN, WAIT_FOREVER) == ERROR) return (ERROR);    printf ("Message from task 1:\n%s\n", msgBuf);}    #define MESSAGE "Greetings from Task 1" task1 (void) {if ((myMsgQId = msgQCreate (MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY)) == NULL) return (ERROR);    if (msgQSend (myMsgQId, MESSAGE, sizeof (MESSAGE), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR) return (ERROR); } 

Pipelines)
The MPs queue provides an interface to choose from, namely, the VxWorks I/O system. A pipe is a virtual I/O device managed by the driver pipedrv. The pipedevcreate () function creates a pipeline device. This call specifies the name of the pipeline, the maximum number of messages that can be arranged, and the allowed length of each message.
Status = pipedevcreate ("/pipe/Name", max_msgs, max_length );
The created MPs queue is an I/O device usually named (named). Tasks can open, read, and write MPs queues using standard I/O functions and call IOCTL routines. When a task attempts to read data from an empty pipeline or write data to a full pipeline, the task is blocked. Like message queue, ISR can be written to the pipeline, but cannot be read from the pipeline. As an I/O device, MPs queues provide important features not available in message queues and call select ().

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.