Multi-thread domain name resolution-one party has

Source: Internet
Author: User
This is the question that I received during the competition. A Domain Name batch query program stores N domain names in the domestic file data.txt, one in each line. The program is required to read the domain name data from the file and then obtain the IP address list corresponding to the domain name. for example, the output format of the file is www.126.comwww.132321321.comwww.baidu.com.

This is the question I got from the competition.

 

 
 
  1. Develop a domain name batch query program. The hosts file data.txt stores N domain names, one per line,
  2. The program is required to read the domain name data from the file and then obtain the IP address list corresponding to the domain name. For example
  3. File Content:
  4. Www.126.com
  5. Www.132321321.com
  6. Www. baIdU.com
  7. Output Format:
  8. Domain Name
  9. IP address count
  10. IP1
  11. IP2
  12. ...
  13. For example, the output result in the above example file is:
  14. Www.126.com
  15. 1
  16. 121.20.3.105
  17. Www.132321321.com
  18. 0
  19. Www. baiDu. Com
  20. 3
  21. 212.32.4.211
  22. 10.52.32.32
  23. 102.31.0.91
In order to speed up the query, I adopted the multi-thread method (the maximum number of threads is controlled by parameters). below is the program process I designed (taking three threads as an example ):

 

 

The following is my design and save results. Because I use multiple threads, I use a linked list to save the results. This will not cause the problem of modifying the same variable between threads at the same time, so no locks are needed, you only need to pass in the linked list pointer. The linked list I designed is that each node on a linked list saves another linked list (storing multiple ip addresses for each domain name)

 

 

Source code:

 

 
 
  1. /*
  2. * System environment: Linux kernel 2.6.18 gcc 4.1.2 pthread. the dns server uses the/etc/resolv. conf configuration.
  3. * Compilation: gcc test1.c-o test1-lpthread
  4. Input parameters:
  5. Parameter 1: Data File Path, required
  6. Parameter 2: Number of threads. If this parameter is left blank, the default value is 4 and the minimum value is 1.
  7. * Run:./test1 dataFileName The default number of threads is 4.
  8. * Example:./test1 d.txt
  9. * Example:./test1 d.txt 3
  10. * Timeout *----------------------------------------------------------------------------------------------------
  11. * Copyright (c) 2010 by yifangyou
  12. * All rights reservEd
  13. * Timeout *----------------------------------------------------------------------------------------------------
  14. * A domain name batch query program is developed. The hosts file data.txt stores N domain names, one per line,
  15. * The program is required to read domain name data from the file and then obtain the IP address list corresponding to the domain name. For example:
  16. * File content:
  17. * Www.126.com
  18. * Www.132321321.com
  19. * Www.baidu.com
  20. * The output format is:
  21. * Domain Name
  22. * Number of IP addresses
  23. * IP1
  24. * IP2
  25. *...
  26. * For example, the output result in the above example file is:
  27. *
  28. * Www.126.com
  29. * 1
  30. * 121.20.3.105
  31. * Www.132321321.com
  32. * 0
  33. * Www.baidu.com
  34. * 3
  35. * 212.32.4.211
  36. * 10.52.32.32
  37. * 102.31.0.91
  38. * Timeout *----------------------------------------------------------------------------------------------------
  39. * The data structure of this program is
  40. * Query_list_head
  41. *-> Query_list1 ----------------------------> query_list2 ------------------> query_list3
  42. *-> Domain1-> domain
  43. * Ip_num1 ip_num
  44. * Host_list_head
  45. *-> Ip1-> ip2-> ip3-> ip2_1-> ip2_2-> ip2_3
  46. */
  47. # INcLude /* For printf */
  48. # Include /* For malloc */
  49. # Include TrIng. h>/* for strlen, strdup */
  50. # Include /* For inet_ntoa */
  51. # Include /* For inet_ntoa */
  52. # Include /* For inet_ntoa */
  53. # Include/* for inet_ntoa */
  54. # Include /* For gethostbyname */
  55. # Include /* For pthread_create, pthread _ Join*/
  56. # Include DdEf. h> /*
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.