cpu_bound.c
as follows:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#define TOTAL_ITERATION_NUM 100000000
struct process_switch_info
{
unsigned int counter;
struct timeval time;
};
main()
{
int a;
unsigned int i=0;
struct timeval start, end;
gettimeofday(&start, NULL); //total time of existence - begin
for(i=1;i<=TOTAL_ITERATION_NUM;i++)
++a;
/*===========================================================================*/
/* prototype of the new system call is as follows */
/* void get_process_switch_info(struct process_switch_info *) */
/*===========================================================================*/
get_process_switch_info(&ps_info); //new system call
gettimeofday(&end, NULL); //total time of existence - end
printf("The process spent %ld uses in the system after it stated its execution.\n",
((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
temp_ul=(ps_info.time.tv_sec * 1000000 + ps_info.time.tv_usec);
printf("The process has made %ld process switches\n", ps_info.counter);
printf("This process has idle %ul usecs\n", temp_ul);
}
io_bound.c
as follows:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
struct process_switch_info
{
unsigned int counter;
struct timeval time;
};
main()
{
FILE *out;
char c;
int a;
struct timeval start, end;
gettimeofday(&start, NULL); //total time of existence - begin
if((out=fopen("io_bound.data","w"))!=NULL)
{
c=38;
for(a=0; a<100000;a++)
{
putc(c,out);
if(++c>126)
c=38;
}
fclose(out);
}
else
{
printf("Cannot open file.\n");
exit(0);
}
/*===========================================================================*/
/* prototype of the new system call is as follows */
/* void get_process_switch_info(struct process_switch_info *) */
/*===========================================================================*/
get_process_switch_info(&ps_info); //new system call
gettimeofday(&end, NULL); //total time of existence - end
printf("The process spent %ld uses in the system after it stated its execution.\n",
((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
temp_ul=(ps_info.time.tv_sec * 1000000 + ps_info.time.tv_usec);
printf("The process has made %ld process switches\n", ps_info.counter);
printf("This process has idle %ul usecs\n", temp_ul);
}
struct task_struct
to record related info.
The initial values of these fields are 0;
copy_process()
or copy_thread()
to set the initial values of the above four fields as 0.
schedule()
makes a process switch from
process 1
to
process 2
, you need to add the
number of process switches of process 2
by 1.
schedule()
makes a process switch from
process 1
to
process 2
, process 1
begins idle
until next time
schedule()
makes a process switch to give CPU control
to process 1
again. During the exectuiont of a
process, the process may experience process switch several times. Hence the total idle time
of a process is the sum of all the above individual idle time.
do_gettimeofday()
to get a microsecond-resolution timestamp, or
getnstimeofday()
for nanosecond resolution.