Question
Purpose: to understand mode switch penalty and the cost difference among system calls. Your task is to write a C program that measures the latencies
Purpose: to understand mode switch penalty and the cost difference among system calls. Your task is to write a C program that measures the latencies of various system calls. In particular, you want to know 1) the cost of CPU mode switch by measuring a light-weight system call which does very little thing in the kernel, and 2) the cost of heavier system calls which triggers a lot of activities inside the kernel. Your program should measure the latencies of three system calls: getpid(), open(), and read(). getpid() represents lightweight call, and open()/read() represent heavy system calls. For high-resolution latency measurements, you should use gettimeofday() system call. See the man page for details. Because individual calls are too fast to accurately measure with the resolution provided with gettimeofday(), you need to measure the execution time of repetition of system calls. You then divide the time taken with how many times to arrive the individual system call latency. Note that getpid() value may be cached by C runtime library you may need to explicitly invoke system call. (see man page of getpid). For open(), you need to use /dev/null as the target. Beware that there is a limit in the number of open files, so you have to manage it carefully when repeating the calls. You need to close() the files in case you are nearing the limit, but you dont want to include the cost of close() call in your measurement. For read(), you need to use /dev/zero special file as the source file. Measurements should take a few seconds for each of the system calls. After measurement, your program must print out the result to standard output using following format: Syscall open(): read(): --------------------------------------------------------------------------getpid(): repetitions xxxxx xxxxx xxxxx time elapsed (micro sec) latency (nano sec) xxxxxxxxx xxxxxxxxx xxxxxxxxx N.B., the latency should be time elapsed divided by repetitions.
Make sure that you check for error condition for open() and read(). open() or read() may return very quickly when kernel discovers obvious error condition early on. The number of open files is limited per process. So when you repeat open(), you have to close() file descriptors so that you don't go over the limit. Otherwise, subsequent open() calls will return error. When you close, make sure you exclude the time taken for close() calls. As specified above, you have to use '/dev/null' for open() measurements, and '/dev/zero' for read() measurements. This means that for read() measurement, you need to open '/dev/zero' file once, and then use the resulting file descriptor for subsequent read() operations you want to measure time. What to turn in: - Your source code named prob2_
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started