コード例 #1
0
ファイル: multithread.c プロジェクト: caidongyun/backup
int main(int argc, char **argv)
{
    memset(&global_info, 0, sizeof(global_info_t));
    char *url = http_url;
    global_info.round = 1;

    int opt;
    while ((opt = getopt(argc, argv, "w:t:r:hs")) != -1) {
        switch (opt) {
            case 'w':
                global_info.work_num = atoi(optarg);
                break;

            case 't':
                global_info.thread_num = atoi(optarg);
                break;

            case 'r':
                global_info.round = atoi(optarg);
                break;

            case 's':
                url = https_url;
                break;

            case 'h':
            default:
                show_help();
                return EXIT_SUCCESS;
        }
    }

    if (global_info.work_num == 0 || global_info.thread_num == 0) {
        show_help();
        return EXIT_FAILURE;
    }

    setsignal(SIGINT, _sig_int);
    setsignal(SIGTERM, _sig_int);
    printf("get work num: %u, thread num: %u\n", global_info.work_num, global_info.thread_num);
    thread_info_t *thread_list = calloc(global_info.thread_num, sizeof(thread_info_t));

    if (thread_list == NULL) {
        printf("alloc threads error\n");
        return EXIT_FAILURE;
    }

    global_info.work_list = calloc(global_info.work_num, sizeof(work_info_t *));

    if (global_info.work_list == NULL) {
        printf("alloc work_list error\n");
        return EXIT_FAILURE;
    }

    int idx = 0;

    for (idx = 0; idx < global_info.work_num; ++idx) {
        global_info.work_list[idx] = calloc(1, sizeof(work_info_t));

        if (global_info.work_list[idx] == NULL) {
            printf("alloc work_list %u error\n", idx);
            return EXIT_FAILURE;
        }

        global_info.work_list[idx]->url = url;
        global_info.work_list[idx]->idx = idx;
    }

    /* Must initialize libcurl before any threads are started */
    curl_global_init(CURL_GLOBAL_ALL);
    init_locks();
    TS_INIT();
    mutexlock_init(global_info.rmtx);
    mutexlock_init(global_info.wmtx);
    TS_DECLARE(perf);

    for (idx = 0; idx < global_info.thread_num; ++idx) {
        thread_list[idx].global_info = &global_info;
        thread_list[idx].curl = curl_easy_init();
        thread_list[idx].multi_handle = curl_multi_init();

        if (thread_list[idx].curl == NULL || thread_list[idx].multi_handle == NULL) {
            printf("error when curl init\n");
            return EXIT_FAILURE;
        }

        //curl_easy_setopt(thread_list[idx].curl, CURLOPT_FORBID_REUSE, 1L);
        curl_easy_setopt(thread_list[idx].curl, CURLOPT_NOSIGNAL, 1L);

        if (url == https_url) {
            curl_easy_setopt(thread_list[idx].curl, CURLOPT_SSL_VERIFYPEER, 0L);
            curl_easy_setopt(thread_list[idx].curl, CURLOPT_SSL_VERIFYHOST, 0L);
        }

        thread_list[idx].idx = idx;
        set_share_handle(thread_list[idx].curl);
    }

    int error;
    TS_BEGIN(perf);

    for (idx = 0; idx < global_info.thread_num; ++idx) {
        error = pthread_create(&(thread_list[idx].tid),
                               NULL, /* default attributes please */
                               pull_one_url,
                               (void *) & (thread_list[idx]));

        if (0 != error) {
            fprintf(stderr, "Couldn't run thread number %d, errno %d\n", idx, error);
            return EXIT_FAILURE;
        }
    }

    /* now wait for all threads to terminate */
    for (idx = 0; idx < global_info.thread_num; idx++) {
        error = pthread_join(thread_list[idx].tid, NULL);
        //printf("[%u:%lu]Thread %d terminated\n", idx, time(NULL), idx);
    }

    TS_END(perf);

    for (idx = 0; idx < global_info.thread_num; idx++) {
        curl_easy_cleanup(thread_list[idx].curl);
        curl_multi_cleanup(thread_list[idx].multi_handle);
    }

    free(thread_list);
    unsigned long total_length = 0;

    for (idx = 0; idx < global_info.work_num; idx++) {
        if (global_info.work_list[idx]) {
            if (global_info.work_list[idx]->status == STAT_DONE) {
                total_length += global_info.work_list[idx]->data_len;
            }

            free(global_info.work_list[idx]);
        }
    }

    free(global_info.work_list);
    printf("RATE: worknum:%u total_length:%lu total_ms_diff:%lu, %.1fK\n",
           global_info.work_num, total_length, TS_MSDIFF(perf), (double)(total_length) / (double)(TS_MSDIFF(perf)));
    kill_locks();
    curl_global_cleanup();
    return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: props.c プロジェクト: pharmafirma/reconos
static void *property_fetcher(void *null)
{
	int first = 1, i, stop;
	FILE *fp;
	char buff[1024], *ptr, *nptr;
	char type[128];

	mutexlock_init(&lock);

	while (!sigint) {
		/* FIXME: HACK: extremly lame, do it better later on */

		mutexlock_lock(&lock);

		memset(table, 0, sizeof(table));
		elems = 0;
		first = 1;

		fp = fopen("/proc/net/lana/properties", "r");
		if (!fp)
			panic("LANA not running?\n");

		memset(buff, 0, sizeof(buff));
		while (fgets(buff, sizeof(buff), fp) != NULL) {
			buff[sizeof(buff) - 1] = 0;
			if (first) {
				first = 0;
				continue;
			}

			if (elems + 1 >= MAX_ELEMS)
				panic("Too many fblock instances!\n");

			memset(type, 0, sizeof(type));
			if (sscanf(buff, "%s [", type) != 1)
				continue;

			strlcpy(table[elems].name, type, sizeof(table[elems].name));

			i = stop = 0;
			ptr = strstr(buff, "[");
			ptr++;
			//XXX
			// prop_str_tab_put_idx(char*)
//			while (!stop && (j = strtoul(ptr, &nptr, 10))) {
			while (!stop) {
				nptr = ptr;
				while (*nptr != ' ' && *nptr != ']') {
					nptr++;
				}
				if (*nptr == ' ' || *nptr == ']') {
					int tmp;
					char foo;
					foo = *nptr;
					*nptr = '\0';
					table[elems].props[i++] = tmp = prop_str_tab_put_idx(ptr);
					ptr = nptr + 1;
					*nptr = foo;
				}
				if (*nptr == ']') {
					stop = 1;
					continue;
				}

//				if (!nptr)
//					break;
//				if (i + 1 >= MAX_PROPS)
//					panic("Too many properties!\n");
//				table[elems].props[i++] = j;
//				if (*nptr == ']')
//					stop = 1;
//				nptr++;
//				ptr = nptr;
			}

			table[elems].prio = strtol(ptr, &nptr, 10);
			memset(buff, 0, sizeof(buff));
			elems++;
		}

		fclose(fp);

		mutexlock_unlock(&lock);
		sleep(10);
	}

	mutexlock_destroy(&lock);
	pthread_exit(NULL);
}
コード例 #3
0
ファイル: call_notifier.c プロジェクト: vaskot/transsip
void init_call_notifier(void)
{
	call_notifier.head = NULL;
	mutexlock_init(&call_notifier.lock);
}