예제 #1
0
파일: queue.c 프로젝트: Drealdal/utils
int queue_distroy(queue_t *queue)
{
    int ret = 0;
    lock_queue(queue);

    if( !is_empty_queue(queue) )
    {
	debug(DEBUG_UTILS,
		"Distroy not empty queue:%s",queue->name);
	ret = -1;
	goto d_end;
    }

    free(queue->name);

    if( queue->need_free == 1 )
    {
	unlock_queue(queue);
	free(queue);
	return 0;
    }
    queue->tail =(queue_body_t *) QUEUE_TAIL_NULL;
    queue->head =(queue_body_t *) QUEUE_HEAD_NULL;
    queue->maxLength = 0;
d_end:
    unlock_queue(queue);
    return ret;

}
예제 #2
0
int HuffManCode(HuffMan * root, FILE  * fp){
    HuffMan * current = NULL;
    Queue * queue = NULL;
    queue = creat_Empty_Queue();
    enter_Queue(queue, root);

    while ( ! is_empty_queue(queue)){
        current = delete_Queue(queue);
        if ( current -> right_child == NULL && current -> left_child == NULL && current != root) {
            printf("%c %d %s \n", current -> ch, current -> weight, current -> code);
            
            fprintf(fp, "%c %s\n", current -> ch, current -> code);
        }
        if ( current -> right_child == NULL && current -> left_child == NULL && current == root) {
            strcpy(current -> code, "0");
           printf("%c %d %s \n", current -> ch, current -> weight, current -> code);
            fprintf(fp, "%c %s\n", current -> ch, current -> code);
        }
        if (current -> left_child) {
                strcpy(current -> left_child -> code, current -> code);
            strcat(current -> left_child -> code, "0");
            enter_Queue(queue, current -> left_child);
        }
        if (current -> right_child) {
                strcpy(current -> right_child -> code, current -> code);
            strcat(current -> right_child -> code, "1");
            enter_Queue(queue, current -> right_child);
        }
    } 
    return 0;
}
예제 #3
0
파일: queue.c 프로젝트: Drealdal/utils
static queue_body_t * __del_queue(queue_head *queue)
{
    struct queue_body * re = NULL;
    lock_queue(queue);
    if( is_empty_queue(queue) )
    {
	debug(DEBUG_UTILS,
		"Queue:%s is empty",queue->name);
	goto out_pos;
    }

    re = queue->head;
    queue->head = queue->head->next;

    if(queue->head == QUEUE_TAIL_NULL)
    {
	queue->tail =(queue_body_t *) QUEUE_TAIL_NULL;
	queue->head =(queue_body_t *)  QUEUE_HEAD_NULL;
    }

    queue->curLength--;
out_pos:
    unlock_queue(queue);
    return re;
}
예제 #4
0
bool delete_from_queue(SqQueue * queue) {
    if(is_empty_queue(queue))
        return false;
    else {
        queue->front = (queue->front + 1) % MAX_SIZE;
        return true;
    }
}
예제 #5
0
파일: queue.c 프로젝트: annnca/PTprob
void print(int *a, int *sq, int *eq)
{
    if(!is_empty_queue(&sq, &eq)){
        int i;
        for (i = *sq; i <= *eq; ++i)
        {
            printf("%d ", a[i]);
        }
        printf("\n");
    } else {
        printf("empty list");
    }
}
예제 #6
0
int out_queue(Queue *q, int *pVal)
{
    if (is_empty_queue(q)) {
        return 0;
    }

    *pVal = q->pBase[q->front];

    // 出队,front加1
    q->front = (q->front + 1)  % kQueue_len;

    return 1;
}
예제 #7
0
파일: queue.c 프로젝트: Trigork/haspie
/*Add the given element at the top of s*/
void add_queue(queue*s, elem_type e) { 
	pqnodos inodos = malloc (sizeof(qnodos));
	inodos->elem = e;
	inodos->next = NULL;
	
	if (is_empty_queue(*s)) {
		s->first = inodos;
		s->last = inodos;
	} else {
		s->last->next = inodos;
		s->last = inodos;
	}
	s->size++;

} 
예제 #8
0
int pop_link_queue(Link_queue *my_link_queue)
{
    Link p;
    p = (Link)malloc(sizeof(struct link));
    is_malloc(p);

    if(is_empty_queue(my_link_queue) == EMPTY_OK)
    {
        return POP_NO;
    }
    
    p = my_link_queue->front;
    my_link_queue->front = my_link_queue->front->next;

    return p->queue;
}
struct BFS_Result* breadth_first_search(struct Graph* graph, int source_vertex,
		int target_vertex) {
	int visited[MAX_VERTICES], i, j, prev_vertex, max_capacity = INT_MAX;
	struct Node *node;
	struct BFS_Result *bfs_result = malloc(sizeof(struct BFS_Result));
	int **path = allocate_2D_matrix(MAX_VERTICES, 2);

	for (i = 0; i < MAX_VERTICES; i++)
		memset(visited, 0, sizeof(int) * MAX_VERTICES);

	for (i = 0; i < MAX_VERTICES; i++) {
		path[i][0] = -1;
		path[i][1] = INT_MAX;
	}

	struct Queue * queue = create_queue(MAX_VERTICES);
	enqueue(queue, source_vertex);
	path[source_vertex][0] = -2;
	path[source_vertex][1] = INT_MAX;

	while (is_empty_queue(queue) != TRUE) {
		i = dequeue(queue);
		node = graph->list[i].next;
		while (node) {
			if (path[node->vertex][0] == -1) {
				enqueue(queue, node->vertex);
				path[node->vertex][0] = i;
				path[node->vertex][1] = node->weight;
			}
			if (node->vertex == target_vertex)
				break;
			node = node->next;
		}
	}
	bfs_result->path = path;
	i = target_vertex;
	while (path[i][0] != -2) {
		if (path[i][1] < max_capacity)
			max_capacity = path[i][1];
		i = path[i][0];
	}

	bfs_result->max_capacity = max_capacity;
	return bfs_result;
}
예제 #10
0
파일: FileUtil.c 프로젝트: Zziwei/MotionNet
void write_queue_to_file(char * fileName, SqQueue * queue) {
	if(is_empty_queue(queue))
		return;
	else {
		FILE *stream = fopen(fileName, "w+");
		fprintf(stream, "Number\tAccX\t\tAccY\t\tAccZ\t\tGyroX\t\tGyroY\t\tGyroZ\t\tMagX\t\tMagY\t\tMagZ\r\n");

		int i = queue->front;
		while(i != queue->rear) {
			fprintf(stream, "%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\r\n", i,
			        queue->accXData[i], queue->accYData[i], queue->accZData[i],
			        queue->gyroXData[i], queue->gyroYData[i], queue->gyroZData[i],
			        queue->magXData[i] , queue->magYData[i],  queue->magZData[i]);
			i = (i + 1) % MAX_SIZE;
		}
		fclose(stream);
	}
}
예제 #11
0
/* dequeue(...)
 *	Removes the head of the queue. The value of the head is stored
 *		in the second parameter of the function.
 *
 *	Returns 0 if user attempts to dequeue from an empty queue. 
 *	Returns 1 otherwise.
 */
int dequeue(struct queue_int_s * queue, int * data)
{
	if(is_empty_queue(queue))
		return 0;

	struct queue_node_s *nextHead = queue->head->next_node;

	*data = queue->head->data;

	/* Deallocate memeory of the head */
	struct queue_node_s *oldHead = queue->head;
	queue->head = nextHead;
	free(oldHead);

	if(queue->head == NULL)
		/* The queue now is empty */
		queue->tail = NULL;
	return 1;
}
예제 #12
0
int main(int argc, char *argv[])
{
	int bossSocket, numWorker, clientSocket, notOverload, flagSignal;
	struct sockaddr_in serverAddr, clientAddr;
	socklen_t clientLen;
	in_port_t serverPort;
	pthread_t workerArr[MAX_WORKER];
	int threadId_arr[MAX_WORKER];

	if(argc < 2)
		print_user_error("In-line arguments", "You must specify the port number");
	serverPort = atoi(argv[1]);

	/* Create a socket */
	bossSocket  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(bossSocket < 0)
		print_system_error("socket() fails");
	
	/* Construct local address structure */
	memset(&serverAddr, 0, sizeof(struct sockaddr_in));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	serverAddr.sin_port = htons(serverPort);
	/* Bind boss socket to local address */
	if(bind(bossSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr) ) < 0)
		print_system_error("bind() fails");

	/* Initialize client queues, mutexes and conditions */
	clientQueue = create_queue();
	pthread_mutex_init(&queue_mutex, NULL);
	pthread_cond_init(&queue_has_client, NULL);

	/* Create the pool of workers */
	if(argc < 3)
		numWorker = DEFAULT_NUM_WORKER;
	else
	{
		numWorker = atoi(argv[2]);
		if(numWorker > MAX_WORKER)
			numWorker = MAX_WORKER;
	}

	printf("Hi\n");
	create_worker(workerArr, threadId_arr, numWorker);
	printf("Hi there!\n");
	
	/* Listening */ 
	if(listen(bossSocket, MAX_WAIT) < 0)
		print_system_error("listen() fails");

	/* Handling request */
	while(1)
	{
		/* Accpet connection to a client */
		clientLen = sizeof(struct sockaddr_in);
		clientSocket = accept(bossSocket, (struct sockaddr *) &clientAddr, &clientLen);
		if(clientSocket < 0)
			print_system_error("accept() fails");

		/* Put that client to the queue, waiting for worker threads to handle it */
		pthread_mutex_lock(&queue_mutex);
		{
			flagSignal = 0;
			if(is_empty_queue(clientQueue))
				/* There must be workers waiting, so send signal */
				flagSignal = 1;
			notOverload = enqueue(clientQueue, clientSocket);
			if(flagSignal)
				pthread_cond_broadcast(&queue_has_client);
		}	
		pthread_mutex_unlock(&queue_mutex);

		if(!notOverload)
		{
			/* Kindly refuse the request of this client */
		}	
	}
}
예제 #13
0
void * handle_client_request(void * workerId)
{
	int id = *((int *) workerId); 

	char * request = NULL;
	int clientSocket, parseSuccess;
	struct http_request_s * request_obj;
	struct http_response_s * response_obj;
	char *response = NULL;
	printf("Hullo in %d\n", id);
	while(1)
	{
		clientSocket = -1;
		printf("Go to loop in %d\n", id);
		pthread_mutex_lock(&queue_mutex);
		{
			if(is_empty_queue(clientQueue))
			{
				pthread_cond_wait(&queue_has_client, &queue_mutex);
			}
			else 
				/* Take 1 client out of the queue */
				dequeue(clientQueue, &clientSocket);
		}
		pthread_mutex_unlock(&queue_mutex);
		printf("Worker with id = %d handles socketId = %d\n", id, clientSocket);
		if(clientSocket >= 0)
		{
			/* Initalize for new request handling */
			request_obj = create_request_struct();
			response_obj = create_response_struct();
			response = NULL;
			
			/* Handle the request of the client */
			request = read_request(clientSocket);
/*			ssize_t numByte = recv(clientSocket, m_request, 1000, 0);
			m_request[numByte] = '\0'; */
			printf("OK reading request\n"); 
			/* Parse request */
			parseSuccess = parse_http_request(request_obj, request, response_obj);

			response_obj->version = copy_str_dynamic(SUPPORT_HTTP);

			if(parseSuccess)
			{
				/* Check HTTP version */
				if(strcasecmp(request_obj->version, SUPPORT_HTTP) != 0)
				{
					set_status_code_error(response_obj, 505, "505 HTTP Version Not Supported", "This server supports only HTTP/1.1");
				}
				else
				{
					/* Check file access security */
					if(count_occurence(request_obj->path, '/') > MAX_NUM_SLASH)
						set_status_code_error(response_obj, 401, "401 Unauthorized", "You are not authorized to access the requested file on this server");
					else
					{
						exec_http_request(request_obj, response_obj);
					}
				}
			} 
			/* Execute command and return output 
			sprintf(response, "Server worker thread with id = %d handles request: %s", id, request); */
			
			response = get_response_text(response_obj);
			send(clientSocket, response, strlen(response), 0);

			//recv(clientSocket, response, 100, 0);
			/* Close socket, free memory */
			close(clientSocket);
			free(request);
			free(response);
			delete_request(&request_obj);
			delete_response(&response_obj); 
		}
	}
}
예제 #14
0
int bfs(int from, int to)
{
	if (from == to)
		return 0;
	
	link_queue q;
	init_queue(&q);
	
	memset(visited, 0, sizeof(visited));
	
	queue_node p, node;
	node.num = from;
	visited[from] = 1;
	en_queue(&q, node);

	while (!is_empty_queue(q))
	{
		de_queue(&q, &p);
		int num = p.num;
		int n[4], i;
		for (i = 0; i < 4; i++)
		{
			n[i] = num % 10;
			num /= 10;
		}

		int x = p.num - n[0];
		for (i = x; i < x+10; i++)
		{
			if (i == to)
				return visited[p.num];
			if (is_prime[i] && visited[i] == 0)
			{
				visited[i] = visited[p.num] + 1;
				node.num = i;
				en_queue(&q, node);
			}
		}
		x = p.num - n[1]*10;
		for (i = x; i < x+100; i+=10)
		{
			if (i == to)
				return visited[p.num];
			if (is_prime[i] && visited[i] == 0)
			{
				visited[i] = visited[p.num] + 1;
				node.num = i;
				en_queue(&q, node);
			}
		}
		
		x = p.num - n[2]*100;
		for (i = x; i < x+1000; i+=100)
		{
			if (i == to)
				return visited[p.num];
			if (is_prime[i] && visited[i] == 0)
			{
				visited[i] = visited[p.num] + 1;
				node.num = i;
				en_queue(&q, node);
			}
		}
		
		x = p.num - n[3]*1000;
		for (i = x+1000; i < x+10000; i+=1000)
		{
			if (i == to)
				return visited[p.num];
			if (is_prime[i] && visited[i] == 0)
			{
				visited[i] = visited[p.num] + 1;
				node.num = i;
				en_queue(&q, node);
			}
		}
	}

	destroy_queue(&q);
	return 0;
}