First I compiled these two codes:
First one called cpu.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// Compute bound process
// Does irrelevant calculations in an infinite loop.
// Must ctrl-c to exit.
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned long i,j;
while(1) {
j = 1;
for(i = 1; i <= 10; i++) {
j = j*i;
}
}
}
|
Called cpu-print.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// I/O Bound process
// Does system calls in a loop and loops for a very long time...
// Can ctrl-c to exit.
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
unsigned int i;
int count = 0;
struct timeval tv;
while(1) {
for(i = 0; i < 1000000; i++) {
gettimeofday(&tv, NULL);
printf("%ld sec, %ld usec\n", tv.tv_sec, tv.tv_usec);
}
count++;
printf("round %d complete\n", count);
}
}
|
When executed these processes will run indefinitely (and will need to be stopped with the ctrl-c). This allows examining the executing process in real-time. I know that a running process can be interrupted for several reasons. When a process must stop running and give up the processor, it’s CPU state and registers are stored, and the state of another process is loaded. A process is said to have experienced a context switch when this happens. Context switches are of two types: voluntary and involuntary. Am I missing anything yet? A process can voluntarily decide to give up the CPU and wait for some event (e.g., disk I/O). A process can be made to give up its CPU forcibly (e.g., when it has run on a processor for too long), and must give a chance to other processes sharing the CPU. The former is called a voluntary context switch, and the latter is called an involuntary context switch.
What I'm trying to do is write a bash script to get and display the current voluntary and non-voluntary context switches, user time, system time, total time (user time + system time) and start time. The script should read the process name from the command line and include some error checking (i.e., for non-existent processes).
Then I have to execute and compare the programs (cpu and cpu-print) in terms of the number of voluntary and involuntary context switches and system time. Which program has mostly voluntary context switches and which has mostly involuntary context switches and why? Which program uses more system time and why? Any help would be appreciated, I have difficulties making bash scripts.
Some general information about the bash script I'm trying to make:
The /proc/{pid}/status and /proc/{pid}/stat files contains information about a currently executing process (by PID value). The pidof <processName> command can obtain the process ID value from the process name which will be the executable name (e.g., 'cpu' or 'cpu-print' here). The user time is the 14th value in the stat output. As such, the user time can be obtained as follows:
USRTIME=`echo "scale=2; $(cat /proc/$PID/stat | cut -d' ' -f14) / $(getconf CLK_TCK)" | bc`
|
The system time is the 15th value in the stat output. The total time is the user time and the system time added. Since these are real values, awk can be used to add them. The start time can be obtained using the ps command with the PID using the -o start option. The output will contain headers which can be eliminated in multiple ways (one of which is grep).
Below is an example output from the script.
./show cpu-print
Process 7737 Information
voluntary_ctxt_switches: 3632
nonvoluntary_ctxt_switches: 10421
User Time: 16.24
System Time: 44.09
Total time 60.33
Start Time: 15:23:43
|