Esempio n. 1
0
/*
 * Function: int main(int argc, char args[])
 * Description: process main function
 * Input:  argc: parameter number
 *         args: parameter value array
 * Output: none
 * Return: function exit status
 * Others: none
 */
int main( )
{
	Queue	* q;
	Ele		* ele;

	q = create_queue( show_ele, delete_ele );

	ele = create_ele( 28, "liwei", 10, "hust++life" );
	in_queue( q, ele );

	ele = create_ele( 24, "lijia", 10, "hust++acon" );
	in_queue( q, ele );

	ele = create_ele( 26, "lijing", 10, "hust++acon" );
	in_queue( q, ele );

	queue_show( q );

	printf( "=============\n" );

	ele = out_queue( q );
	show_ele( ele );

	ele = out_queue( q );
	show_ele( ele );

	ele = out_queue( q );
	show_ele( ele );

	ele = out_queue( q );
	show_ele( ele );
}
Esempio n. 2
0
int main(int argc, const char *argv[])
{
	queue_t *sqp;
	int i;
	data_t buf;
	sqp = creat_queue(8);


	for(i = 0;i < 10;i ++){
		in_queue(sqp,&i);
	}

	print_queue(sqp);

	for(i = 0 ;i < 4;i ++){
		buf = -1;
		out_queue(sqp,&buf);
		printf("out : %d\n",buf);
	}
	print_queue(sqp);

	for(i = 11;i < 20;i ++){
		in_queue(sqp,&i);
	}
	print_queue(sqp);

	for(i = 0 ;i < 8;i ++){
		buf = -1;
		out_queue(sqp,&buf);
		printf("out : %d\n",buf);
	}
	print_queue(sqp);

	return 0;
}
int main(void)
{
    Queue q = {0};
    init(&q, kQueue_len);
    en_queue(&q, 1);
    en_queue(&q, 2);
    en_queue(&q, 3);
    en_queue(&q, 4);
    en_queue(&q, 5);
    en_queue(&q, 6);
    en_queue(&q, 7);
    en_queue(&q, 8);
    traverse_queue(&q);

    int pVal;

    if (out_queue(&q, &pVal)) {
        printf("出队成功,队列出队元素是%d\n", pVal);
    } else {
        printf("出队失败!\n");
    }

    traverse_queue(&q);

    return 0;
}
Esempio n. 4
0
int main(void)
{
	QUEUE Q;

	init(&Q);
	int val;
	en_queue(&Q,1);
	en_queue(&Q,2);
	en_queue(&Q,3);
	en_queue(&Q,4);
	en_queue(&Q,5);
	en_queue(&Q,6);  //若为长度为6的队列,只能放5个元素
	
	traverse_queue(&Q);
	
	if(out_queue(&Q,&val))
	{
		printf("出队成功!出队元素为:%d\n",val);
	}
	else
	{
		printf("出队失败!\n");
	}
	traverse_queue(&Q);
	return 0;
}
Esempio n. 5
0
//free data in queue
static void clear_queue(Queue *q)
{
    while(!is_queue_empty(q))
    {
        Customer *c = out_queue(q);
        if(c)
            free(c);
    }
}
Esempio n. 6
0
void main()
{
	int arr[LEN] = {0};

	in_queue(arr, 9);
	in_queue(arr, 5);
	in_queue(arr, 2);
	in_queue(arr, 7);
	queue_for_each(arr);

	out_queue(arr);
	out_queue(arr);
	queue_for_each(arr);

	in_queue(arr, 3);
	queue_for_each(arr);

}
Esempio n. 7
0
int main(void)
{
    QUEUE Q;
    int val;
    
    init(&Q);
    en_queue(&Q, 1);
    en_queue(&Q, 2);
    en_queue(&Q, 3);
    en_queue(&Q, 4);
    en_queue(&Q, 5);
    en_queue(&Q, 6);
    en_queue(&Q, 7);
    traverse_queue(&Q);
    out_queue(&Q, &val);
    out_queue(&Q, &val);
    traverse_queue(&Q);
    return 0;
}
//*****************************************
//			main函数
//*****************************************
void	main()
{
	QUEUE queue;
	init(&queue);
	en_queue(&queue,1);
	en_queue(&queue,2);
	en_queue(&queue,3);
	en_queue(&queue,4);
	en_queue(&queue,5);
	traverse(&queue);
	int v;
	out_queue(&queue,&v);
	traverse(&queue);
	out_queue(&queue,&v);
	traverse(&queue);
	out_queue(&queue,&v);
	traverse(&queue);
	out_queue(&queue,&v);
	traverse(&queue);
}
int main()
{	//创建静态队列,并定义出队数据的保存在变量data_save中
	int data_save;
	PQUEUE pQueue = creat_queue();
	//将数据入队,并遍历输出队列中的数据
	enter_queue(pQueue,1);
	enter_queue(pQueue,2);
	enter_queue(pQueue,3);
	enter_queue(pQueue,4);
       	traverse_queue(pQueue);
	//将数据出队,并遍历输出队列中的数据
	out_queue(pQueue,&data_save);
       	traverse_queue(pQueue);

	return 0;
}
Esempio n. 10
0
// event handler
static void event_handle(Event* e)
{
    Component* component = component_list[e->component_id];
    switch(e->event_type)
    {
        case ARRIVAL:
        {
            Customer *c;
            double next_time = generate_customer(C2G(component),e->timestamp,&c);
            Event* departure_event = event_router(c,C2G(component)->D);
            if(departure_event)
            {
                schedule_event(departure_event);
            }
            Event *arrival_event = e;//reuse
            e->timestamp = next_time;
            schedule_event(arrival_event);
        }break;
        case DEPARTURE:
        {
            Queue* queue = C2Q(component);
            Customer *c = out_queue(queue);
			c->time_serve = e->serve_time;
            double next_time = simulation_serve(c,queue);
            Event* departure_event = event_router(c,queue->D);
            if(departure_event)
            {
                schedule_event(departure_event);
            }
            if(!is_queue_empty(queue))
            {
                Event* next_departure_event = e;
                next_departure_event->timestamp = next_time;
				next_departure_event->serve_time = next_time - now_time;
                schedule_event(next_departure_event);
            }else
                free(e);
        }break;
        default:
            printf("event type error\n");
            exit(-1);
    }
}
int write_shm_queue(void* handle, const char* to_write_buf, int buf_len)
{
	
	int ret;
	shm_queue_handle_t *shmq_handle = (shm_queue_handle_t*)handle;
	
	if(NULL == shmq_handle)
	{
		return SHM_HANDLE_NULL;
	}
	
	if(shmq_handle->op_mode != OP_MODE_WRITE)
	{
		return SHM_OP_WRONG;
	}

	if(shmq_handle->pool_size < buf_len)
	{
		return SHM_BUF_SHORT;
	}
	
	pthread_rwlock_wrlock(&(shmq_handle->shm_que_info->rwlock));
	int f_ret;
	while((f_ret = is_full(shmq_handle->que,shmq_handle->para_queue_size)) > 0 
		|| shmq_handle->shm_que_info->pool_left_size < buf_len)
	{
		block_info_t *head_block;
		int head_offset = sizeof(shm_queue_info_t) + sizeof(circular_array_t) + sizeof(block_info_t) * (shmq_handle->que->head);
		head_block = (block_info_t*)((char*)(shmq_handle->shm_que_info) + head_offset);

		if(shmq_handle->who_prior == READ_PRIO)
		{
			int i = 0;
			for( i = 0; i < MAX_PROC_NUM; i++)
			{
				if(shmq_handle->shm_que_info->read_flags[i] == FLAG_TRUE)
				{
					while(head_block->flags[i] == FLAG_FALSE)
					{
						/*
							i think a clock will be here when i have more time
						*/
					}
				}
			}
		}

		else if(shmq_handle->who_prior == WRITE_PRIO)
		{
			int i = 0;
			for(i = 0; i < MAX_PROC_NUM; i++)
			{
				if(shmq_handle->shm_que_info->read_flags[i] == FLAG_TRUE && head_block->flags[i] == FLAG_FALSE)
				{
					shmq_handle->readers_miss[i].miss_bytes += head_block->block_size;
					shmq_handle->readers_miss[i].miss_block_num++;
				}				
			}			
		}		

		
		ret = out_queue(shmq_handle->que,shmq_handle->para_queue_size);
		if(ret == SHM_OK)
		{			
			shmq_handle->shm_que_info->pool_left_size +=  head_block->block_size;
		}
		
		else
		{
			pthread_rwlock_unlock(&(shmq_handle->shm_que_info->rwlock));
			return SHM_OUT_QUEUE_FAIL;
		}	
	}

	ret = is_full(shmq_handle->que,shmq_handle->para_queue_size);  
	
	if(ret < 0 && shmq_handle->shm_que_info->pool_left_size >= buf_len)
	{   
		write_block(shmq_handle,to_write_buf,buf_len);
	}      
	
	else
	{
		pthread_rwlock_unlock(&(shmq_handle->shm_que_info->rwlock));
		return SHM_NO_SPACE;
	}           
	
	pthread_rwlock_unlock(&(shmq_handle->shm_que_info->rwlock));
	return SHM_OK;
}