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.
- Develop a domain name batch query program. The hosts file data.txt stores N domain names, one per 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
- File Content:
- Www.126.com
- Www.132321321.com
- Www. baIdU.com
- Output Format:
- Domain Name
- IP address count
- IP1
- IP2
- ...
- For example, the output result in the above example file is:
- Www.126.com
- 1
- 121.20.3.105
- Www.132321321.com
- 0
- Www. baiDu. Com
- 3
- 212.32.4.211
- 10.52.32.32
- 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:
- /*
- * System environment: Linux kernel 2.6.18 gcc 4.1.2 pthread. the dns server uses the/etc/resolv. conf configuration.
- * Compilation: gcc test1.c-o test1-lpthread
- Input parameters:
- Parameter 1: Data File Path, required
- Parameter 2: Number of threads. If this parameter is left blank, the default value is 4 and the minimum value is 1.
- * Run:./test1 dataFileName The default number of threads is 4.
- * Example:./test1 d.txt
- * Example:./test1 d.txt 3
- * Timeout *----------------------------------------------------------------------------------------------------
- * Copyright (c) 2010 by yifangyou
- * All rights reservEd
- * Timeout *----------------------------------------------------------------------------------------------------
- * A domain name batch query program is developed. The hosts file data.txt stores N domain names, one per line,
- * 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:
- * File content:
- * Www.126.com
- * Www.132321321.com
- * Www.baidu.com
- * The output format is:
- * Domain Name
- * Number of IP addresses
- * IP1
- * IP2
- *...
- * For example, the output result in the above example file is:
- *
- * Www.126.com
- * 1
- * 121.20.3.105
- * Www.132321321.com
- * 0
- * Www.baidu.com
- * 3
- * 212.32.4.211
- * 10.52.32.32
- * 102.31.0.91
- * Timeout *----------------------------------------------------------------------------------------------------
- * The data structure of this program is
- * Query_list_head
- *-> Query_list1 ----------------------------> query_list2 ------------------> query_list3
- *-> Domain1-> domain
- * Ip_num1 ip_num
- * Host_list_head
- *-> Ip1-> ip2-> ip3-> ip2_1-> ip2_2-> ip2_3
- */
- # INcLude /* For printf */
- # Include /* For malloc */
- # Include TrIng. h>/* for strlen, strdup */
- # Include /* For inet_ntoa */
- # Include /* For inet_ntoa */
- # Include /* For inet_ntoa */
- # Include/* for inet_ntoa */
- # Include /* For gethostbyname */
- # Include /* For pthread_create, pthread _ Join*/
- # Include DdEf. h> /*