Ejemplo n.º 1
0
void find_bcd_device() {
	DIR *dirp;
	struct dirent *dp;
	dirp = opendir("/sys/bus/usb/devices/");
	while ((dp = readdir(dirp)) != NULL) {
		if (dp->d_type != DT_LNK) continue;
		struct dirent *sub_dp;
		DIR *sub_dirp;
		char *dir_path = join_char(join_char("/sys/bus/usb/devices/", dp->d_name), "/");
		sub_dirp = opendir(dir_path);
		int pid_ok = 0;
		int vid_ok = 0;
		while ((sub_dp = readdir(sub_dirp)) != NULL) {
			if (strcmp(sub_dp->d_name,"idProduct") == 0) {
				int file = open(join_char(dir_path, sub_dp->d_name), O_RDONLY);
				char spid[4];
				read(file, spid, 4);
				if (spid[0] == '0' && spid[1] == '0' && spid[2] == '0' && spid[3] == '8') {
					pid_ok = 1;
				}
				close(file);
			}
			if (strcmp(sub_dp->d_name,"idVendor") == 0) {
				int file = open(join_char(dir_path, sub_dp->d_name), O_RDONLY);
				char svid[4];
				read(file, svid, 4);
				if (svid[0] == '2' && svid[1] == '0' && svid[2] == 'b' && svid[3] == '1') {
					vid_ok = 1;
				}
				close(file);
			}
		}
		if (pid_ok && vid_ok) {
			int file = open(join_char(dir_path, "bcdDevice"), O_RDONLY);
			char bcdD[5];
			read(file, bcdD, 4);
			bcdDevice = atoi(bcdD);
			close(file);
			break;
		}
	}
	printf("bcdDevice = %d\n", bcdDevice);
	closedir(dirp);
}
Ejemplo n.º 2
0
//有多少个词条就开多少个线程来模拟分叉树算法,选出词条最多的一个或几个作为分词结果
int _tree_cut_words(struct TREE_CUT_WORDS_DATA *parent_cut_words_data) {

    english_word_cut((*parent_cut_words_data).cut_word_result, &((*parent_cut_words_data).remainder_words),
                     (*parent_cut_words_data).words_weight);

    //取出第一个汉子,查询redis词条,for循环生产不同的父级分词结果,然后传给新建的子进程,进入递归
    char *first_chinese_word;
    first_chinese_word = (char *) malloc(chinese_char_byte);
    memset(first_chinese_word, 0, chinese_char_byte);
    if (!first_chinese_word) {
        //todo.每一个可能的错误都要做处理或日志记录.not only malloc
        printf("Not Enough Memory!/n");
    }

    strncpy(first_chinese_word, parent_cut_words_data->remainder_words, chinese_word_byte);
    strcat(first_chinese_word, "\0");
    char *get_word_item_cmd = join_char(redis_get_common, first_chinese_word);

    //用完释放内存
    free(first_chinese_word);

    redisContext *redis_connect;
    get_redis_connect(&redis_connect);

    redisReply *redis_reply = redisCommand(redis_connect, get_word_item_cmd);
    char *match_words_json_str = redis_reply->str;
    free(get_word_item_cmd);
    cJSON *match_words_json = cJSON_Parse(match_words_json_str);

    //是否已经到最后一个词
    bool NEED_not_match_word_end_cut;

    //有多少词条是否包含在搜索词里
    int8_t match_words_item_number = 0;

    //判断是否查找到redis词条
    if (redis_reply->type == REDIS_REPLY_STRING) {
        //pullwords_result_number要先计算好有多少种结果。
        for (int tree_arg_index = 0; tree_arg_index < cJSON_GetArraySize(match_words_json); tree_arg_index++) {
            cJSON *subitem = cJSON_GetArrayItem(match_words_json, tree_arg_index);
            //判断词条是否包含在搜索词里
            if (strstr(parent_cut_words_data->remainder_words, subitem->valuestring) != NULL) {
                match_words_item_number++;
            }
        };
    } else {
        NEED_not_match_word_end_cut = false;
    }


    if (match_words_item_number == 0) {
        NEED_not_match_word_end_cut = true;
    }

    if (NEED_not_match_word_end_cut == false) {
        int8_t add_result_number = 0;
        //pullwords_result_number要做原子操作,递增,支持并发
        add_result_number = match_words_item_number - 1;
        __sync_fetch_and_add((*parent_cut_words_data).pullwords_result_number, add_result_number);
        for (int tree_arg_index = 0; tree_arg_index < cJSON_GetArraySize(match_words_json); tree_arg_index++) {
            cJSON *subitem = cJSON_GetArrayItem(match_words_json, tree_arg_index);
            //判断词条是否包含在搜索词里,如果在,进入树分词算法
            if (strstr(parent_cut_words_data->remainder_words, subitem->valuestring) != NULL) {

                //复制一份分词结果,生出父级分词结果
                struct TREE_CUT_WORDS_DATA *tree_cut_words_data = (struct TREE_CUT_WORDS_DATA *) malloc(
                        sizeof(struct TREE_CUT_WORDS_DATA));
                memset(tree_cut_words_data, 0, sizeof(struct TREE_CUT_WORDS_DATA));
                init_tree_cut_words_data(tree_cut_words_data);

                strcpy(tree_cut_words_data->cut_word_result, parent_cut_words_data->cut_word_result);
                strcpy(tree_cut_words_data->remainder_words, parent_cut_words_data->remainder_words);
                *tree_cut_words_data->words_weight = *(parent_cut_words_data->words_weight);

                //todo 把pullwords主线程的data封装成一个指针,减少指针创建,.
                tree_cut_words_data->pullwords_result_queue = parent_cut_words_data->pullwords_result_queue;
                tree_cut_words_data->pullwords_result_queue_write_lock = parent_cut_words_data->pullwords_result_queue_write_lock;
                tree_cut_words_data->pullwords_cond = parent_cut_words_data->pullwords_cond;
                (*tree_cut_words_data).pullwords_result_number = (*parent_cut_words_data).pullwords_result_number;
                (*tree_cut_words_data).pullwords_result_finish_number = (*parent_cut_words_data).pullwords_result_finish_number;

                chinese_word_cut(tree_cut_words_data->cut_word_result, &(tree_cut_words_data->remainder_words),
                                 tree_cut_words_data->words_weight, subitem->valuestring);

                //还有词没分完,进入递归分词
                if (strlen(tree_cut_words_data->remainder_words) > 0) {
                    pthread_t pthread_id;
                    int pthread_create_result;
                    pthread_create_result = pthread_create(&pthread_id, NULL,
                                                           (void *) _tree_cut_words, tree_cut_words_data);
                    if (pthread_create_result != 0) {
                        //todo,deal the error,.
                        printf("Create pthread error!\n");
                    }
                } else {
                    //创建线程最终分词结果,写入主线程队列
                    struct TREE_CUT_WORDS_RESULT *last_tree_cut_words_result = (struct TREE_CUT_WORDS_RESULT *) malloc(
                            sizeof(struct TREE_CUT_WORDS_RESULT));
                    memset(last_tree_cut_words_result, 0, sizeof(struct TREE_CUT_WORDS_RESULT));
                    strcpy(last_tree_cut_words_result->pullwords_result, tree_cut_words_data->cut_word_result);
                    last_tree_cut_words_result->words_weight = *(tree_cut_words_data->words_weight);


                    //分词结束,加锁,写入分词结果到主线程的分词队列
                    pthread_mutex_lock(tree_cut_words_data->pullwords_result_queue_write_lock);

                    EnQueue((*tree_cut_words_data).pullwords_result_queue, last_tree_cut_words_result);
                    *((*parent_cut_words_data).pullwords_result_finish_number) =
                            (*((*parent_cut_words_data).pullwords_result_finish_number)) + 1;

                    //加锁比较pullwords_result_finish_number跟pullwords_result_number,如果全部作业完成,通知主进程
                    if (*((*parent_cut_words_data).pullwords_result_finish_number) ==
                        *((*parent_cut_words_data).pullwords_result_number)) {
                        pthread_cond_signal((*parent_cut_words_data).pullwords_cond);
                    }
                    pthread_mutex_unlock(tree_cut_words_data->pullwords_result_queue_write_lock);
                }
            }

        }
    } else {
        //首汉子没有匹配到字典词条 OR 没有查找到redis词条,选取剩下的字作为一个半词
        not_match_word_end_cut(parent_cut_words_data->cut_word_result, &(parent_cut_words_data->remainder_words),
                               parent_cut_words_data->words_weight);
        //创建线程最终分词结果,写入主线程队列
        struct TREE_CUT_WORDS_RESULT *last_tree_cut_words_result = (struct TREE_CUT_WORDS_RESULT *) malloc(
                sizeof(struct TREE_CUT_WORDS_RESULT));
        memset(last_tree_cut_words_result, 0, sizeof(struct TREE_CUT_WORDS_RESULT));
        strcpy(last_tree_cut_words_result->pullwords_result, parent_cut_words_data->cut_word_result);
        last_tree_cut_words_result->words_weight = *(parent_cut_words_data->words_weight);



        //分词结束,加锁,写入分词结果到主线程的分词队列
        pthread_mutex_lock(parent_cut_words_data->pullwords_result_queue_write_lock);
        EnQueue((*parent_cut_words_data).pullwords_result_queue, last_tree_cut_words_result);
        *((*parent_cut_words_data).pullwords_result_finish_number) =
                (*((*parent_cut_words_data).pullwords_result_finish_number)) + 1;


        //加锁比较pullwords_result_finish_number跟pullwords_result_number,如果全部作业完成,通知主进程
        if (*((*parent_cut_words_data).pullwords_result_finish_number) ==
            *((*parent_cut_words_data).pullwords_result_number)) {
            pthread_cond_signal((*parent_cut_words_data).pullwords_cond);
        }
        pthread_mutex_unlock(parent_cut_words_data->pullwords_result_queue_write_lock);
    }


    free(parent_cut_words_data->cut_word_result);
    free(parent_cut_words_data->words_weight);
    free(parent_cut_words_data->origin_remainder_words);
    free(parent_cut_words_data);


    return 0;
}
Ejemplo n.º 3
0
/*
 * The function may product many thread to deal the participle.
 * I use recursion
 * */
int _tree_cut_words(struct TREE_CUT_WORDS_DATA *parent_cut_words_data)
{

    not_chinese_word_cut((*parent_cut_words_data).cut_word_result, &((*parent_cut_words_data).remainder_words),
                     (*parent_cut_words_data).words_weight);

    char *first_chinese_word;
    first_chinese_word = (char *) malloc(chinese_char_byte);
    memset(first_chinese_word, 0, chinese_char_byte);
    if (!first_chinese_word) {
        //todo.deal the error,not just printf.
        printf("Not Enough Memory!/n");
    }

    strncpy(first_chinese_word, parent_cut_words_data->remainder_words, chinese_word_byte);
    strcat(first_chinese_word, "\0");
    char *get_word_item_cmd = join_char(redis_get_common, first_chinese_word);

    //free Memory
    free(first_chinese_word);

    redisContext *redis_connect;
    get_redis_connect(&redis_connect);

    redisReply *redis_reply = redisCommand(redis_connect, get_word_item_cmd);
    char *match_words_json_str = redis_reply->str;
    free(get_word_item_cmd);
    cJSON *match_words_json = cJSON_Parse(match_words_json_str);

    //The variable is check the remain words is the end or not
    bool NEED_not_match_word_end_cut;

    /*
     * count the number,how many words items that in dictionary is also a path in the remain words.
     * */
    int8_t match_words_item_number = 0;

    //check that, if one or more word item in dictionary has this fhinese chinese word.
    if (redis_reply->type == REDIS_REPLY_STRING) {
        for (int tree_arg_index = 0; tree_arg_index < cJSON_GetArraySize(match_words_json); tree_arg_index++) {
            cJSON *subitem = cJSON_GetArrayItem(match_words_json, tree_arg_index);
            //check the valuestring is in the remain words or not.
            if (strstr(parent_cut_words_data->remainder_words, subitem->valuestring) != NULL) {
                match_words_item_number++;
            }
        };
    } else {
        NEED_not_match_word_end_cut = false;
    }


    if (match_words_item_number == 0) {
        NEED_not_match_word_end_cut = true;
    }

    if (NEED_not_match_word_end_cut == false) {
        int8_t add_result_number = 0;
        add_result_number = match_words_item_number - 1;
        __sync_fetch_and_add((*parent_cut_words_data).pullwords_result_number, add_result_number);
        for (int tree_arg_index = 0; tree_arg_index < cJSON_GetArraySize(match_words_json); tree_arg_index++) {
            cJSON *subitem = cJSON_GetArrayItem(match_words_json, tree_arg_index);
            //check the valuestring is in the remain words or not.
            if (strstr(parent_cut_words_data->remainder_words, subitem->valuestring) != NULL) {

                /*
                 * copy the participle result.send it to the children thread.
                 * */
                struct TREE_CUT_WORDS_DATA *tree_cut_words_data = (struct TREE_CUT_WORDS_DATA *) malloc(
                        sizeof(struct TREE_CUT_WORDS_DATA));
                memset(tree_cut_words_data, 0, sizeof(struct TREE_CUT_WORDS_DATA));
                init_tree_cut_words_data(tree_cut_words_data);

                strcpy(tree_cut_words_data->cut_word_result, parent_cut_words_data->cut_word_result);
                strcpy(tree_cut_words_data->remainder_words, parent_cut_words_data->remainder_words);
                *tree_cut_words_data->words_weight = *(parent_cut_words_data->words_weight);

                //todo,include the five point to one point.
                tree_cut_words_data->pullwords_result_queue = parent_cut_words_data->pullwords_result_queue;
                tree_cut_words_data->pullwords_result_queue_write_lock = parent_cut_words_data->pullwords_result_queue_write_lock;
                tree_cut_words_data->pullwords_cond = parent_cut_words_data->pullwords_cond;
                (*tree_cut_words_data).pullwords_result_number = (*parent_cut_words_data).pullwords_result_number;
                (*tree_cut_words_data).pullwords_result_finish_number = (*parent_cut_words_data).pullwords_result_finish_number;

                chinese_word_cut(tree_cut_words_data->cut_word_result, &(tree_cut_words_data->remainder_words),
                                 tree_cut_words_data->words_weight, subitem->valuestring);

                //not finish,create another thread to deal with it.
                if (strlen(tree_cut_words_data->remainder_words) > 0) {
                    pthread_t pthread_id;
                    int pthread_create_result;
                    pthread_create_result = pthread_create(&pthread_id, NULL,
                                                           (void *) _tree_cut_words, tree_cut_words_data);
                    if (pthread_create_result != 0) {
                        //todo,deal the error,.
                        printf("Create pthread error!\n");
                    }
                } else {
                    //participle is finished.
                    //create the last participle result;
                    struct TREE_CUT_WORDS_RESULT *last_tree_cut_words_result = (struct TREE_CUT_WORDS_RESULT *) malloc(
                            sizeof(struct TREE_CUT_WORDS_RESULT));
                    memset(last_tree_cut_words_result, 0, sizeof(struct TREE_CUT_WORDS_RESULT));
                    strcpy(last_tree_cut_words_result->pullwords_result, tree_cut_words_data->cut_word_result);
                    last_tree_cut_words_result->words_weight = *(tree_cut_words_data->words_weight);

                    //lock
                    pthread_mutex_lock(tree_cut_words_data->pullwords_result_queue_write_lock);
                    //write the result to the queue.
                    EnQueue((*tree_cut_words_data).pullwords_result_queue, last_tree_cut_words_result);
                    *((*parent_cut_words_data).pullwords_result_finish_number) =
                            (*((*parent_cut_words_data).pullwords_result_finish_number)) + 1;

                    //campare the need finish and had finish number. If equal.notify the first parent thread.
                    if (*((*parent_cut_words_data).pullwords_result_finish_number) ==
                        *((*parent_cut_words_data).pullwords_result_number)) {
                        pthread_cond_signal((*parent_cut_words_data).pullwords_cond);
                    }
                    pthread_mutex_unlock(tree_cut_words_data->pullwords_result_queue_write_lock);
                }
            }
        }
    } else {
        /*
         * use the first chinese word to find the word item in dictionary.If find nothing,It is end.finish
         * If find some word items,but no one in the remain words,It is end.finish
         * */
        not_match_word_end_cut(parent_cut_words_data->cut_word_result, &(parent_cut_words_data->remainder_words),
                               parent_cut_words_data->words_weight);
        //create the last participle result;
        struct TREE_CUT_WORDS_RESULT *last_tree_cut_words_result = (struct TREE_CUT_WORDS_RESULT *) malloc(
                sizeof(struct TREE_CUT_WORDS_RESULT));
        memset(last_tree_cut_words_result, 0, sizeof(struct TREE_CUT_WORDS_RESULT));
        strcpy(last_tree_cut_words_result->pullwords_result, parent_cut_words_data->cut_word_result);
        last_tree_cut_words_result->words_weight = *(parent_cut_words_data->words_weight);

        //lock
        pthread_mutex_lock(parent_cut_words_data->pullwords_result_queue_write_lock);
        //write the result to the queue.
        EnQueue((*parent_cut_words_data).pullwords_result_queue, last_tree_cut_words_result);
        *((*parent_cut_words_data).pullwords_result_finish_number) =
                (*((*parent_cut_words_data).pullwords_result_finish_number)) + 1;
        //campare the need finish and had finish number. If equal.notify the first parent thread.
        if (*((*parent_cut_words_data).pullwords_result_finish_number) ==
            *((*parent_cut_words_data).pullwords_result_number)) {
            pthread_cond_signal((*parent_cut_words_data).pullwords_cond);
        }
        pthread_mutex_unlock(parent_cut_words_data->pullwords_result_queue_write_lock);
    }

    //free the data
    free(parent_cut_words_data->cut_word_result);
    free(parent_cut_words_data->words_weight);
    free(parent_cut_words_data->origin_remainder_words);
    free(parent_cut_words_data);
    return 0;
}