In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the usage of application layer programming chain queue under linux". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of application layer programming chain queue under linux".
This paper describes the use of a chain queue in application layer programming under linux. A queue is a linear table that only allows inserts on one end and deletions on the other. In real life, queue applications can be seen everywhere, such as queuing to buy XXX, hospital registration system and so on. Take queuing up to buy tickets, for example, all people stand in a line, the first arrivals are at the front, and the last arrivals can only wait in line from the end of the queue. Everyone in the queue must wait until all the people in front of them have successfully bought tickets and left the queue from the head of the line. This is a typical queue structure. Chained queues do not need to consider the use of space, because chained queues themselves are real-time application space. Therefore, this can be counted as an advantage of chained queues over sequential queues. The complete source code of the chain queue is given below, which has been verified to be feasible and can be used for direct reference if necessary.
/ *
*
* INCLUDE
*
, /
# include "queue.h"
# include
# include
# include
# include "debug.h"
# include "log.h"
/ / initialize the queue and establish an empty queue Q
Status InitQueue (LinkQueue* Q)
{
Q-> front = NULL
Q-> rear = NULL
Sem_init (& Q-> sem_lock, 0,1)
Return OK
}
/ / destroy queue Q if it exists
Status DestroyQueue (LinkQueue* Q)
{
Sem_wait (& Q-> sem_lock)
QeuePtr qn_tmp_ptr
While (Q-> front)
{
Qn_tmp_ptr = Q-> front
Q-> front = Q-> front- > next
Free (qn_tmp_ptr)
}
Free (Q)
Sem_post (& Q-> sem_lock)
Return OK
}
/ / clear queue Q
Status ClearQueue (LinkQueue* Q)
{
Sem_wait (& Q-> sem_lock)
QeuePtr qn_tmp_ptr
Qn_tmp_ptr = Q-> front
While (qn_tmp_ptr)
{
Memset (& qn_tmp_ptr- > data, 0, sizeof (QElemType))
Qn_tmp_ptr = qn_tmp_ptr- > next
}
Sem_post (& Q-> sem_lock)
Return OK
}
/ / return OK if the queue column is empty, ERROR otherwise
Status QueueEmpty (LinkQueue Q)
{
Sem_wait & Q.sem_lock)
If (! Q.front)
{
Return OK
}
Else
{
Return ERROR
}
Sem_post & Q.sem_lock)
}
/ / if queue Q exists and is not empty, return the queue header element of queue Q with e
Status GetHead (LinkQueue Q, QElemType * e)
{
Sem_wait & Q.sem_lock)
* e = Q.front-> data
Sem_post & Q.sem_lock)
Return OK
}
/ / join the queue. If queue Q exists, insert a new element e into queue Q and become the end element of the queue.
Status EnQueue (LinkQueue* Q, QElemType e)
{
Sem_wait (& Q-> sem_lock)
QeuePtr s = (QeuePtr) malloc (sizeof (QNode))
If (s = = NULL) / / failed to allocate memory
{
Return ERROR
}
S-> data = e
S-> next = NULL
/ * determine whether there are any nodes in the queue * /
If (Q-> front = = NULL)
{
Q-> front = s
Q-> rear = s
}
Else
{
Q-> rear- > next = s
Q-> rear = s
}
Sem_post (& Q-> sem_lock)
Return OK
}
/ / returns the number of elements in queue Q
Int QueueLength (LinkQueue Q)
{
Sem_wait & Q.sem_lock)
Int count = 0
While (Q.front)
{
Count++
Q.front = Q.front-> next
}
Sem_post & Q.sem_lock)
Return count
}
Void PrintQueue (LinkQueue Q)
{
Sem_wait & Q.sem_lock)
While (Q.front)
{
DEBUG_LOG (MSG_DEBUG, "Q.front-> data.len= [% d]!!\ n", Q.front-> data.len)
DEBUG_LOG_HEX (MSG_DEBUG, "Q.front-> data.buf\ n", Q.front-> data.buf, Q.front-> data.len)
Q.front = Q.front-> next
}
Sem_post & Q.sem_lock)
}
/ / out of the queue, delete the queue header element of queue Q and return its value with e
Status deQueue (LinkQueue* Q, QElemType* e)
{
If (Q-> front = = NULL)
{
Printf ("delQueue: this queue is empty\ n")
Return ERROR
}
Sem_wait (& Q-> sem_lock)
* e = Q-> front- > data
QeuePtr p = Q-> front
Q-> front = Q-> front- > next
Free (p)
Sem_post (& Q-> sem_lock)
Return OK
}
# ifndef _ _ QUEUE_H__
# define _ _ QUEUE_H__
# include
/ * *
* queue (implemented in C language, based on chain structure)
, /
# define OK 1
# define ERROR 0
Typedef struct lora
{
Unsigned char buf [70]; / / storing data frames
Int len;// stores valid data length
} lora
Typedef int Status
The type of typedef lora QElemType;//QElemType depends on the situation. Here is the structure.
/ / define the structure of the queue node
Typedef struct QNode
{
QElemType data
Struct QNode* next
} QNode, * QeuePtr
/ / define the linked list structure of the queue
Typedef struct
{
QeuePtr front
QeuePtr rear
Sem_t sem_lock
} LinkQueue
/ *
* initialize an empty queue
, /
Status InitQueue (LinkQueue* Q)
/ *
* destroy the queue
, /
Status DestroyQueue (LinkQueue* Q)
/ *
* clear the queue
, /
Status ClearQueue (LinkQueue* Q)
/ *
* determine whether the queue is empty
, /
Status QueueEmpty (LinkQueue Q)
/ *
* get the value of the first element of the queue
, /
Status GetHead (LinkQueue Q, QElemType * e)
/ *
* queued
, /
Status EnQueue (LinkQueue* Q, QElemType e)
/ *
* get queue length
, /
Int QueueLength (LinkQueue Q)
/ *
* print all elements of the queue
, /
Void PrintQueue (LinkQueue Q)
/ *
* out of queue
, /
Status deQueue (LinkQueue* Q, QElemType* e)
# endif
Thank you for your reading. the above is the content of "the usage of application layer programming chain queue under linux". After the study of this article, I believe you have a deeper understanding of the usage of application layer programming chain queue under linux, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.