队列(Queue)是一种数据结构,可以在队列的一端插入元素而在队列的另一端删除元素。
( 1 )允许删除的一端称为 队头( Front ) 。
( 2 )允许插入的一端称为 队尾( Rear ) 。
( 3 )当队列中没有元素时称为 空队列 。
( 4 )队列亦称作先进先出( First In First Out )的线性表,简称为 FIFO 表 。
队列的修改是依先进先出的原则进行的。新来的成员总是加入队尾(即不允许 ” 加塞 ” ),每次离开的成员总是队列头上的(不允许中途离队),即当前 ” 最老的 ” 成员离队。
多任务系统是一个典型的队列示例,在其中完成作业的调度。假设有五个程序等待执行, 它们将被放入一个队列,如果有第六个程序要执行,它将被放在队列的末尾。队列中首位的程序首先执行.程序代码
#include <stdio.h>
#define maxsize 10
typedef struct
{
int elem[maxsize];
int front,rear;
}queue;
void init_queue(queue *cp)
{
cp->front=0;
cp->rear=0;
}
void en_queue(queue *cp,int x)
{
if((cp->rear+1)%maxsize==cp->front)
printf(”The quequ is full!!”);
else
{
cp->rear=(cp->rear+1)%maxsize;
cp->elem[cp->rear]=x;
}
}
int dl_queue(queue *cp)
{
if(cp->front==cp->rear)
printf(”The queue is empty!!”);
else
{
cp->front=(cp->front+1)%maxsize;
return(cp->elem[cp->front]);
}
}
void print(queue *cp)
{
int i;
for(i=cp->front+1;i<=cp->rear;i++)
{
printf(”[%d]“,cp->elem[i]);
}
}
void main()
{
int x,y;
int select;
queue *cp=NULL;
init_queue(cp);
do
{
printf(”(1) Input a stack data”);
printf(”(2) Output a stack data”);
printf(”(3) Exit”);
printf(”Please select one:”);
scanf(”%d”,&select);
switch(select)
{
case 1:printf(”Please input the data:”);
scanf(”%d”,&x);
en_queue(cp,x);
printf(”The queue is: “);
print(cp);
break;
case 2:y=dl_queue(cp);
printf(”The queue is: “);
print(cp);
printf(”The putout data is %d”,y);
break;
case 3:break;
}
}
while(select<3);
getch();
}
memcached :
#include “memcached.h”
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef USE_THREADS
#include <pthread.h>
#define ITEMS_PER_ALLOC 64
/* An item in the connection queue. */
/*链接队列项 */
typedef struct conn_queue_item CQ_ITEM;
struct conn_queue_item {
int sfd;
int init_state;
int event_flags;
int read_buffer_size;
int is_udp;
CQ_ITEM *next;
};
/*链接 队列*/
/* A connection queue. */
typedef struct conn_queue CQ;
struct conn_queue {
CQ_ITEM *head;
CQ_ITEM *tail;
pthread_mutex_t lock;
pthread_cond_t cond;
};
/* Lock for connection freelist */
static pthread_mutex_t conn_lock;
/* Lock for alternative item suffix freelist */
static pthread_mutex_t suffix_lock;
/* Lock for cache operations (item_*, assoc_*) */
static pthread_mutex_t cache_lock;
/* Lock for slab allocator operations */
static pthread_mutex_t slabs_lock;
/* Lock for global stats */
static pthread_mutex_t stats_lock;
/*cq的连表*/
/* Free list of CQ_ITEM structs */
static CQ_ITEM *cqi_freelist;
static pthread_mutex_t cqi_freelist_lock;
/*
* Each libevent instance has a wakeup pipe, which other threads
* can use to signal that they’ve put a new connection on its queue.
没一个 libevent实例都有一个别 的线程用信号唤醒的管道,他们最早队列中起个链接
*/
typedef struct {
pthread_t thread_id; /* unique ID of this thread 线程id*/
struct event_base *base; /* libevent handle this thread uses 线程用到的libeven的处理t */
struct event notify_event; /* listen event for notify pipe 监听事件来通知管道*/
int notify_receive_fd; /* receiving end of notify pipe 接受结束通知管道 */
int notify_send_fd; /* sending end of notify pipe 发送结束管道 */
CQ new_conn_queue; /* queue of new connections to handle 新 的链接处理*/
} LIBEVENT_THREAD;
static LIBEVENT_THREAD *threads;
/*
* Number of threads that have finished setting themselves up.
*/
static int init_count = 0;
static pthread_mutex_t init_lock;
static pthread_cond_t init_cond;
static void thread_libevent_process(int fd, short which, void *arg);
/*
* Initializes a connection queue.
*/
static void cq_init(CQ *cq) {
pthread_mutex_init(&cq->lock, NULL);
pthread_cond_init(&cq->cond, NULL);
cq->head = NULL;
cq->tail = NULL;
}
/*
* Waits for work on a connection queue.
*/
static CQ_ITEM *cq_pop(CQ *cq) {
CQ_ITEM *item;
pthread_mutex_lock(&cq->lock);
while (NULL == cq->head)
pthread_cond_wait(&cq->cond, &cq->lock);
item = cq->head;
cq->head = item->next;
if (NULL == cq->head)
cq->tail = NULL;
pthread_mutex_unlock(&cq->lock);
return item;
}
/*
* Looks for an item on a connection queue, but doesn’t block if there isn’t
* one.
* Returns the item, or NULL if no item is available
*/
static CQ_ITEM *cq_peek(CQ *cq) {
CQ_ITEM *item;
pthread_mutex_lock(&cq->lock);
item = cq->head;
if (NULL != item) {
cq->head = item->next;
if (NULL == cq->head)
cq->tail = NULL;
}
pthread_mutex_unlock(&cq->lock);
return item;
}
/*
* Adds an item to a connection queue.
*/
static void cq_push(CQ *cq, CQ_ITEM *item) {
item->next = NULL;
pthread_mutex_lock(&cq->lock);
if (NULL == cq->tail)
cq->head = item;
else
cq->tail->next = item;
cq->tail = item;
pthread_cond_signal(&cq->cond);
pthread_mutex_unlock(&cq->lock);
}
/*
* Returns a fresh connection queue item.
*/
static CQ_ITEM *cqi_new() {
CQ_ITEM *item = NULL;
pthread_mutex_lock(&cqi_freelist_lock);
if (cqi_freelist) {
item = cqi_freelist;
cqi_freelist = item->next;
}
pthread_mutex_unlock(&cqi_freelist_lock);
if (NULL == item) {
int i;
/* Allocate a bunch of items at once to reduce fragmentation */
item = malloc(sizeof(CQ_ITEM) * ITEMS_PER_ALLOC);
if (NULL == item)
return NULL;
/*
* Link together all the new items except the first one
* (which we’ll return to the caller) for placement on
* the freelist.
*/
for (i = 2; i < ITEMS_PER_ALLOC; i++)
item[i - 1].next = &item[i];
pthread_mutex_lock(&cqi_freelist_lock);
item[ITEMS_PER_ALLOC - 1].next = cqi_freelist;
cqi_freelist = &item[1];
pthread_mutex_unlock(&cqi_freelist_lock);
}
return item;
}
/*
* Frees a connection queue item (adds it to the freelist.)
*/
static void cqi_free(CQ_ITEM *item) {
pthread_mutex_lock(&cqi_freelist_lock);
item->next = cqi_freelist;
cqi_freelist = item;
pthread_mutex_unlock(&cqi_freelist_lock);
}
/*
* Creates a worker thread.
*/
static void create_worker(void *(*func)(void *), void *arg) {
pthread_t thread;
pthread_attr_t attr;
int ret;
pthread_attr_init(&attr);
if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) {
fprintf(stderr, “Can’t create thread: %s”,
strerror(ret));
exit(1);
}
}
/*
* Pulls a conn structure from the freelist, if one is available.
*/
conn *mt_conn_from_freelist() {
conn *c;
pthread_mutex_lock(&conn_lock);
c = do_conn_from_freelist();
pthread_mutex_unlock(&conn_lock);
return c;
}
/*
* Adds a conn structure to the freelist.
*
* Returns 0 on success, 1 if the structure couldn’t be added.
*/
bool mt_conn_add_to_freelist(conn *c) {
bool result;
pthread_mutex_lock(&conn_lock);
result = do_conn_add_to_freelist(c);
pthread_mutex_unlock(&conn_lock);
return result;
}
/*
* Pulls a suffix buffer from the freelist, if one is available.
*/
char *mt_suffix_from_freelist() {
char *s;
pthread_mutex_lock(&suffix_lock);
s = do_suffix_from_freelist();
pthread_mutex_unlock(&suffix_lock);
return s;
}
/*
* Adds a suffix buffer to the freelist.
*
* Returns 0 on success, 1 if the buffer couldn’t be added.
*/
bool mt_suffix_add_to_freelist(char *s) {
bool result;
pthread_mutex_lock(&suffix_lock);
result = do_suffix_add_to_freelist(s);
pthread_mutex_unlock(&suffix_lock);
return result;
}
/****************************** LIBEVENT THREADS *****************************/
/*
* Set up a thread’s information.
*/
static void setup_thread(LIBEVENT_THREAD *me) {
if (! me->base) {
me->base = event_init();
if (! me->base) {
fprintf(stderr, “Can’t allocate event base”);
exit(1);
}
}
/* Listen for notifications from other threads */
event_set(&me->notify_event, me->notify_receive_fd,
EV_READ | EV_PERSIST, thread_libevent_process, me);
event_base_set(me->base, &me->notify_event);
if (event_add(&me->notify_event, 0) == -1) {
fprintf(stderr, “Can’t monitor libevent notify pipe”);
exit(1);
}
cq_init(&me->new_conn_queue);
}
/*
* Worker thread: main event loop
*/
static void *worker_libevent(void *arg) {
LIBEVENT_THREAD *me = arg;
/* Any per-thread setup can happen here; thread_init() will block until
* all threads have finished initializing.
*/
pthread_mutex_lock(&init_lock);
init_count++;
pthread_cond_signal(&init_cond);
pthread_mutex_unlock(&init_lock);
return (void*) event_base_loop(me->base, 0);
}
/*
* Processes an incoming “handle a new connection” item. This is called when
* input arrives on the libevent wakeup pipe.
*/
static void thread_libevent_process(int fd, short which, void *arg) {
LIBEVENT_THREAD *me = arg;
CQ_ITEM *item;
char buf[1];
if (read(fd, buf, 1) != 1)
if (settings.verbose > 0)
fprintf(stderr, “Can’t read from libevent pipe”);
item = cq_peek(&me->new_conn_queue);
if (NULL != item) {
conn *c = conn_new(item->sfd, item->init_state, item->event_flags,
item->read_buffer_size, item->is_udp, me->base);
if (c == NULL) {
if (item->is_udp) {
fprintf(stderr, “Can’t listen for events on UDP socket”);
exit(1);
} else {
if (settings.verbose > 0) {
fprintf(stderr, “Can’t listen for events on fd %d”,
item->sfd);
}
close(item->sfd);
}
}
cqi_free(item);
}
}
/* Which thread we assigned a connection to most recently. */
static int last_thread = -1;
/*
* Dispatches a new connection to another thread. This is only ever called
* from the main thread, either during initialization (for UDP) or because
* of an incoming connection.
*/
void dispatch_conn_new(int sfd, int init_state, int event_flags,
int read_buffer_size, int is_udp) {
CQ_ITEM …