ENGR9881 Computer Networks GE
CN Workshop05: Monitoring
Due: Friday, 14 May 2019
Lab 5 is comprised of 4 tasks; Task 4 is worth 2 checkpoints, each other task is worth 1 checkpoint with,
each checkpoint contributing 1% towards your topic grade. Following completion of the checkpoints, a
copy of the source code and output for each task should be compiled into a single text file and uploaded to
the relevant submission box on FLO for moderation. Speak to your demonstrator if you are unsure how to
do this.
While discussion of programming tasks with fellow students in labs is welcome and encouraged, please be
mindful of the University's Academic Integrity policy and refrain from simply copying another student's
code (even if they agree!). You will gain a much greater understanding of the material if you take the time to
work through your own solutions, and you will be expected to be able to explain your code before being
awarded checkpoints. Remember, the demonstrators are there to help if you get stuck.
Objectives
Upon completing they lab, you should be able to:
Profile C programs to explore run time information to observe the time that programs take to run
Use optional debug statements to enable/disable debugging code
Use system functions (stat) to access file metadata to observe the time and date fields that are used by
files in the file system
Learning out comes;
LO2: Demonstrate the design and implementation of simple process and network level programs
Version 1.3 Page 1 of 5 2019/Jan/10
Talk 1
Write a program that calculates and prints the elapsed time of a loop to an accuracy of microseconds.
1. Use the structure timeval to store times form the following C header file;
#include <sys/time.h>
3. In a loop that executes 100 times
Use gettimeofday() to get the time
Iterate through a loop 1,000,000 times
Use gettimeofday() to get the clasped time
4. Calculate and print the time taken in microseconds for each loop and entire program
execution
See;
man 2 gettimeofday
Version 1.3 Page 2 of 5 2019/Jan/10
Task 2
Write a program that uses the function getopt() to parse command line options, print provided
options.
1. Call a function usage() if there are no command line arguments
2. With in a suitable loop (refer to the getopt man page for example code
1. Use getopt() to parse the command line options
2. With in a switch/case statement
1. Process arguments and print each option and argument
2. In the switch control structure set the default case and ‘?’ to call usage() if
incorrect arguments are provided
void usage () {
printf(“Usage: programname -f filename”);
}
See;
man 3 getopt
Version 1.3 Page 3 of 5 2019/Jan/10
Task 3
Write a program that uses stat() to collect the metadata about a file and print out each of the
fields, display all the relevant time and/or date related fields.
1. Use getopt() to provide a the command line argument of the file name
2. Use the following C header file;
#include <sys/stat.h>
3. Use stat() and the structure in the stat man page to find the metadata about the provided file
4. Print each of the time related elements of the structure with the appropriate format and
description;
struct stat {
dev_t st_dev; /* device inode resides on */
ino_t st_ino; /* inode's number */
mode_t st_mode; /* inode protection mode */
…
}
5. Using the above comments in the structure in the man page;
printf(“device inode resides on: ‘%d’\n”, file->st_dev);
printf(“inode’s number: ‘%d’\n”, file->st_ino);
printf(“inode’s number: ‘%d’\n”, file->st_ino);
…
6. How can the time stamp on files be used to determine performance and timing?
See;
man 2 stat
Version 1.3 Page 4 of 5 2019/Jan/10
Task 4
Copy the source code for task 1 and add DEBUG statements around the timing functionality to
allow optional use of timing.
Part 1
1. add a #define statement at the top of your code for DEBUG 1 for true and DEBUG 0 for
false
2. Use an if statement to allow for conditional execution of the timing functions
if (DEBUG) {
gettimeofday();
}
3. Collect run time information in the shell with the command time, for each of DEBUG 1
and DEBUG 0
? Part 2
4. add a #define statement at the top of your code for DEBUG
5. Use an #ifdef and #endif statement to allow for conditional compilation and execution of
the timing functions
#ifdef DEBUG
gettimeofday()
#endif
6. Collect run time information in the shell with the command time, for each of DEBUG 1
and DEBUG 0
Part 3
7. Is it possible to measure any difference between using the techniques in part 1 and part 2?
8. Is it possible to measure the difference in execution time with the command line time?
See;
man 2 stat
Version 1.3 Page 5 of 5 2019/Jan/10
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。